Dec 30, 2018

Setup Dev Environment with a Local Blockchain and Deploy Smart Contract [Full Stack Ethereum Dapp Part-2]

In Part-1, we made Hello World contract in Remix. As we have to write code in the files and manage in repository for development perspective so in this blog post, we will setup our development environment, a private Ethereum blockchain on local machine and deploy our smart contract on the blockchain. I am using Ubuntu 18 for writing this post. But can be setup on Windows and MAC machines.


You can get instructions here. Here is command for Ubuntu:

sudo apt install git


Install Node.js on your local machine. You can get installers here. I am using it via NVM to manage multiple vesions of node.js. Here are the commands for Ubuntu

wget -qO- | bash
source ~/.profile
nvm install 10.14.1
nvm use 10.14.1


I am going to use Visual Studio Code(VSCode) editor. You can get it here or use following command for Ubuntu:

wget -q -O- | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] stable main"
sudo apt install code

You might want to install following VSCode extensions by searching in Extension tab:

Solidity: for solidity syntax highlighting

ES6 Mocha Snippets and Chai Snippets: for unit testing snippets


It is a development framework for Ethereum. It provides you with a project structure, files, and directories that make deployment and testing much easier and allows scriptable deployment and migrations. Let's install it by running following command:

npm install -g truffle

Setup Project

1. Open a terminal, create a directory (say HelloWorld) and setup truffle project.

mkdir helloworld
cd helloworld
truffle init

It will create basic folder structure.

2. Create a smart contract 'HelloWorld.sol' using following command:

truffle create contract HelloWorld

It will create HelloWorld.sol in contracts folder. Replace the code with our solidity code made in Part-1 of the series.

3. Use following command to build:

truffle compile

By default, the output of your compilation process is in build/contracts/HelloWorld.json file.

4. Create migration file using following command

truffle create migration HelloWorld

It will add 1546163703_hello_world.js like file in migrations folder. Open the file and update with following content:

var HelloWorld = artifacts.require('HelloWorld');

module.exports = function(deployer) {
  // Use deployer to state migration tasks.

Setup Blockchain

There are three famous options to setup a private Ethereum blockchain for development, you can follow any one.

Ganache: It gives you the ability to perform all actions you would on the main chain without the cost. It allows you to run tests, execute commands, and inspect state while controlling how the chain operates. you can quickly see how your application affects the blockchain, and introspect details like your accounts, balances, contract creations and gas costs.

Download ganache from here.

Put the downloaded file in proper path and give permission to execute in Ubuntu

chmod a+x ganache-1.1.0-x86_64.AppImage

then run it. You can click on Settings icon to get server host and port (default 7545). It will be used on truffle configurations.


Truffle Develop: Another option is to use truffle develop, a development blockchain built directly into Truffle. Truffle Develop helps you set up an integrated blockchain environment with a single command, no installation required. Run Truffle Develop by typing the following into a terminal:

truffle develop

This will run the client on It will display the first 10 accounts and the mnemonic used to create those accounts.

One more option is Ganache cli - headless, command line interfaces similar to truffle develop.

Personal Recommendation:

If you need visual interface, go for Ganache. If you need just to migrate and testing, go for truffle develop or ganache-cli. I face high CPU usage for Ganache which makes local machine very slow so personally go with truffle develop But you can go with anyone. What we need is just host and port to deploy in next step.

Truffle Configuration

Update truffle config file like below:

module.exports = {
  // See <>
  // to customize your Truffle configuration!
  networks: {
    "development": {
      network_id: "*",
      host: "",
      port: 9545

Here you need to update host and port based on blockchain configurations. Now, start the blockchain server and run following command to migrate:

truffle migrate

you will get output like below:

Starting migrations...
> Network name:    'development'
> Network id:      5777
> Block gas limit: 6721975


   Replacing 'Migrations'
   > transaction hash:    0x5781f512eddd6bb038809ce95505ea46d0bbbcb29ad7eae6b8059655475dfae9
   > Blocks: 0            Seconds: 0
   > contract address:    0xcB16f879f7dae9B8f749A00Cc44bc424217841AF
   > account:             0x61dbEBCB81c57280E0d8BF38A7CbA831379524Ea
   > balance:             99.97497038
   > gas used:            284908
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00569816 ETH

   > Saving migration to chain.
   > Saving artifacts
   > Total cost:          0.00569816 ETH


   Replacing 'HelloWorld'
   > transaction hash:    0x18747ce6b55a0221cd240965105fe80228a321f74cea47c2dce9f7b1de413398
   > Blocks: 0            Seconds: 0
   > contract address:    0x32509cf81185cd59AeDC5D2E0d550F65331F4c17
   > account:             0x61dbEBCB81c57280E0d8BF38A7CbA831379524Ea
   > balance:             99.9618816
   > gas used:            612405
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.0122481 ETH

   > Saving migration to chain.
   > Saving artifacts
   > Total cost:           0.0122481 ETH

> Total deployments:   2
> Final cost:          0.01794626 ETH

To test deployment, let's start truffle console and run our smart contract methods to verify.

truffle console
let instance = await HelloWorld.deployed()

It should return Hello World. Let's call setName method.

let accounts = await web3.eth.getAccounts()
instance.setName('Ram',{from: accounts[0]})

It should return Hello Ram means it is successfully deployed.



In this blog post, we worked to setup development environment, setup project & private Ethereum blockchain and deployed smart contract on blockchain successfully. In next post, we will work on unit testing.

Hope, It helps. Happy Blockchain !!