---
# System prepended metadata

title: Deploying Contracts on Local Blockchain with Truffle and Ganche

---

# Deploying Contracts on Local Blockchain with Truffle and Ganche

## [Ganache Local Blockchain](https://trufflesuite.com/ganache/) (Download HERE)

Ganache is a blockchain server that is specifically designed for rapid Ethereum and Corda distributed application development. It can be used throughout the entire development process and provides a secure and predictable environment for the creation, deployment, and testing of dApps. Ganache is available in two forms: a user interface (UI) and a command-line interface (CLI).

## What is Truffle?

Truffle is a toolkit for building and deploying decentralized applications (DApps) on the Ethereum blockchain. It includes features like smart contract compilation and deployment, advanced debugging tools, and a scriptable framework for migrating and deploying DApps. Truffle also offers network management and an interactive console for communicating with contracts. Its goal is to make it easier for developers to build and deploy DApps on Ethereum.

# Installation
- Requirements
    - Node.js v14 - v18
    - Windows, Linux, or macOS

## Install Node.js

To install latest version of NPM you can run:
```
npm i -g npm
```

Truffle requires *node-gyp* for compiling native add-on modeules for Node.js. To install run the following:

```
npm install -g node-gyp
```

Use *nvm* to install Node.js. For example to download Node.js 18 you can run:

```
nvm install 18
```

## Install Truffle

Use NPM to install Truffle via the terminal:

```
npm install -g truffle
```

You can check the version with:

```
truffle version
```



# Creating First Project w/ Template
- Before you start using Truffle make a directory and open it at the root folder in your IDE.
    - For example:
```
mkdir MetaCoin
cd MetaCoin
```
- If you'd rather not use a template, you can create a bare Truffle project in that directory using:
```
truffle init
```

## Truffle Boxes

- After creating your directory you can utilize example projects and templates offered by [Truffle Boxes](https://trufflesuite.com/boxes/)
- After you cd into the directory you made you can setup the template with this command:

```
truffle unbox metacoin
```
- After you unbox your desired template your project directory will contain the following:
    1. contracts/: A directory for Solidity contracts
    2. migrations/: A directory for scriptable deployment files
    3. test/: A directory for test files to test your application and contracts
    4. truffle-conig.js: The Truffle configuration file

## Setup Config for Local Blockchain

- Open truffle-config.js and replace the content with the following:
```
module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*",
    },
  },
};
```

# Compiling & Deployment

## Launch Local Blockchain

1. Open Ganache UI, click QUICKSTART, and ADD PROJECT by selecting truffle-config.js, and click restart:

![](https://i.imgur.com/N1M0cdo.png)

![](https://i.imgur.com/LnGtIdF.png)

![](https://i.imgur.com/xvEZWId.png)


## Command
- To compile, change to the root directory and use the following command:
```
sudo truffle compile
```
This should be your output:
```
Compiling your contracts...
===========================
> Compiling ./contracts/ConvertLib.sol-bin. Attempt #1
> Compiling ./contracts/MetaCoin.sol
> Artifacts written to /Users/ronankearns/Documents/Chapman/Y3/Research/MetaCoin/build/contracts
> Compiled successfully using:
   - solc: 0.8.13+commit.abaa5c0e.Emscripten.clang
```

## Deployment

### Testing Smart Contract

The template test code has already been done for us at /metacoin/test.

- To run solidity test in CLI 
```
truffle test ./test/metacoin.js
```
You should recieve a similar Output:
```
Using network 'development'.


Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.


  Contract: MetaCoin
    ✔ should put 10000 MetaCoin in the first account (72ms)
    ✔ should call a function that depends on a linked library (94ms)
    ✔ should send coin correctly (324ms)


  3 passing (615ms)
```

### Migration/Deployment

Now you can migrate and deploy our smart contracts to the blockchain using the following command:

```
truffle migrate
```

This displays the transaction IDs and addresses of the deployed contracts, as well as a summary of the cost and real-time updates on the status.

Example Output:
![](https://i.imgur.com/fiveiAG.png)


Switch back to Ganache running in the background. Click the “BLOCKS”, “TRANSACTIONS”, and “CONTRACTS” to see the transactions that have been processed.
