Ethereum: Setting up a development environment

Update 16 Feb 2019: There is now an updated version of this article with Ubuntu 18, Truffle, and Ganache.

This article is a quick-start guide to setting up a Ethereum development environment. This setup can be used to develop smart contracts for both public and private blockchains. To facilitate rapid development we’ll be using ethereumjs-testrpc running in Ubuntu, and truffle to manage our projects, testing, compiling, and migration.

ethereumjs-testrpc is a Node.js based Ethereum client for testing and development. Read more about it here: https://github.com/ethereumjs/testrpc

truffle is a development environment, testing framework and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier. Read more about it here: https://github.com/ConsenSys/truffle

For those running Windows (like myself), one of my previous articles steps through running Ubuntu on Windows 10 Pro using VirtualBox. I’ve used a similar environment for the steps in this article.

Install Nodejs

We want to make sure we get the latest v7 to ensure we can run testrpc

sudo apt-get update

sudo apt install curl

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

sudo apt-get install -y nodejs

sudo apt-get install npm

Install ethereumjs-testrpc

Refer to https://github.com/ethereumjs/testrpc

sudo apt-get install build-essential

sudo apt-get install git

sudo npm install -g ethereumjs-testrpc

Now run testrpc

testrpc

Create a new nodejs project using Web3

  1. Open a new terminal window, and create a new folder then cd into it

    mkdir dapp && cd dapp
    
  2. Create pacakge.json file

    vi package.json
    
  3. Now hit i to go into insert mode, and type the following

    {
      "dependencies": {
        "web3": "0.17.0-alpha"
      }
    }
    

    This is declaring web3 as a dependency, which we’ll install using npm in a moment.

  4. To save and exit, hit esc, then : and type wq, then hit enter

  5. Use npm to install web3. Type the below in the same folder as the package.json

    npm install
    
  6. Finally, we need to verify that everything is working. Ensure testrpc is still running in the other terminal. Now launch the node console

    node
    
  7. Inside the node console, enter the following

    > var Web3 = require("web3")
    > var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
    > web3.eth.accounts
    

    This should return a list of accounts that matches those displayed in the testrpc window.

Install truffle and create a new project

  1. Install truffle. Note this takes a few minutes to complete.

    sudo npm install -g truffle
    
  2. We should be in the dapp folder created previously. If not, cd into it. Now create another folder for the truffle project and cd into it.

    mkdir project && cd project
    
  3. Run truffle init to generate a new truffle app

    truffle init
    

    This creates some new folders and files:

    • contracts
    • migrations
    • test
    • truffle.js

Explore truffle

Truffle gives us access to the power of Web3. Following the previous example of printing the testrpc accounts, we can do the same in truffle:

truffle console
web3.eth.accounts

Example output:

[ '0x5964af18b6226d04f262c1b0f5723642c8fae725',
'0x770068c65a6bf726a0e8cc475bafe54d87ee5270',
'0x4eb96de1983b7e85c1fde7e3e1c6e0a7e0b4f0c6',
'0x1a5579866f04bbe01311fa059063bba452faef09',
'0xadd14aa1c65db7f8303840debe288d2255d86d49',
'0x4634b5a336ec9c3d988381307c210250e5a0a0bd',
'0x89c829cad115fa8c0f2101bceeeb0d5d2cacfdde',
'0x113fd42a48e033f8c8200a43afdba6a845c45a5d',
'0xa9b6d90c7e5b3d5435e9a03fc2566a90e76d0d7a',
'0x046e6441d76ee3e675cf2e2eeb5b0682b8d47b09' ]

Wrapping up

This is where we wrap up for now. Truffle includes some example smart contracts that you can compile and play with using testrpc, or you can try writing your own.

truffle compile
truffle migrate

References