Ethereum development environment setup in Ubuntu 18

How to install Truffle and Ganache in Ubuntu 18.04

This article is an updated version of my 2017 guide to setting up a Ethereum development environment. In the time since the original article smart contracts have continued to grow in popularity and the range of available development tools have increased in sophistication. To facilitate rapid smart contract development in Ubuntu 18 this guide will describe 2 excellent tools from the Truffle suite: Truffle and Ganache.

  • Ganache - previously known as TestRPC, Ganache offers both desktop and command-line varieties. Ganache is a quick and easy way to run a personal blockchain for developing and deploying smart contracts.
  • Truffle - used to manage smart contract projects, testing, compiling, and migration.

For these steps I’ll be using Ubuntu 18.04 running in VirtualBox on Windows 10 Pro 64-bit.

Install Ganache

This guide will use the desktop version of Ganache. Ganache will provide a personal blockchain to be used for local development and testing of smart contracts.

  1. In Ubuntu, open a browser and navigate to https://github.com/trufflesuite/ganache/releases

  2. Download the latest Linux release which will be the *.AppImage file.

    For example ganache-1.3.0-x86_64.AppImage.

  3. Once the download is complete, open a new terminal and change into the directory with the *.AppImage file.

  4. Use chmod to make the file executable:

    chmod a+x ganache-1.3.0-x86_64.AppImage
    
  5. Now run the file

    ./ganache-1.3.0-x86_64.AppImage
    
  6. You will be prompted if you want to integrate the application into your system. For convenience, click Yes. This will allow you to launch Ganache later from Ubuntu Application menu.

    ganache install prompt

  7. Ganache will launch and prompt if you want to enable Google Analytics tracking to help the developers improve the software. Toggle this off if you wish, then click Continue.

    ganache analytics prompt

  8. The main Ganache window will open and you will notice there are already a number of addresses with a balance of 100 ETH each. Ganache provides a personal blockchain that you can start developing against immediately.

    ganache main interface

Ganache is now installed and running. I encourage you to review the official quickstart to learn more about the interface: https://truffleframework.com/docs/ganache/quickstart.

Install NodeJS and NPM

Truffle requires NodeJS v8.9.4 or later (at the time of writing), so the next step will be to install NodeJS from the Ubuntu 18.04 distribution repository. While this may not be the latest NodeJS version, it meets the requirements for Truffle. I have installed v8.10.0:

  1. First, ensure your system is up to date:

    sudo apt update
    
  2. Install NodeJS and it’s package manager, npm:

    sudo apt install nodejs npm
    
  3. Verify the install was successful by checking the installed version of NodeJS:

    nodejs -v
    

    nodejs version

  4. Also verify that npm was installed successfully by checking the version:

    npm -v
    

    npm version

Install Truffle

This section describes how to install Truffle in Ubuntu 18.04

  1. Once npm is install, you can install Truffle with this command:

    sudo npm install -g truffle
    
  2. Verify the installation by checking the truffle version:

    truffle version
    

    truffle version

Create a new truffle project

  1. Create a new directory for the Truffle project and change into it:

    mkdir myproject
    cd myproject
    
  2. Generate an empty Truffle project

    truffle init
    

    truffle init command

  3. The new project will include some folders and files:

    • contracts
    • migrations
    • test
    • truffle-config.js

    You can read about the default project structure in the Truffle documentation Creating a project.

Truffle also provides some example applications (called Truffle Boxes) which you can explore to learn more about the tool and developing smart contracts. Check out the Truffle documentation Creating a project to learn about these.

Using Truffle console

Truffle console is a convenient way to interact with the Ganache Ethereum client.

  1. First you need to configure Truffle to connect to Ganache. Open the truffle-config.js file for editing and locate the networks section (around line 38).

    Editing truffle-config.js

  2. You need to uncomment the development section and set the parameters to match Ganache settings.

    Ganache displays it’s host and port number in the main interface. For example:

    • Host: 127.0.0.1
    • Port: 7545

    You can also hit the Settings icon in the top right of the Ganache interface to view and edit these.

    Editing truffle-config.js

  3. In truffle-config.js uncomment the development section. Set the host and port values to match Ganache, then save the file.

    Editing truffle-config.js

  4. Now in a terminal window, ensure you are in your project directory (e.g. myproject) and run truffle console:

    truffle console
    

    You can read more about truffle console in the Truffle documentation: Using Truffle Develop and The Console.

  5. Now you can interact with Ganache. Truffle uses the Ethereum Javascript API Web3. You can discover more about the available web3 commands in the Web3 documentation: https://web3js.readthedocs.io/en/1.0/.

    For example, to list the accounts:

    web3.eth.getAccounts()
    

    truffle console web3

    You can also list the balance of an account. First, store the account array in a variable named accounts. Then get the balance of the first account:

    let accounts = await web3.eth.getAccounts()
    web3.eth.getBalance(accounts[0])
    

    This returns the current balance for the given address in wei, in this case 100000000000000000000.

    truffle console web3

  6. Close Truffle console by entering the .exit command, or hit Ctrl + C twice.

Congratulations, you have successfully setup a basic development environment for Ethereum smart contracts in Ubuntu 18 with Truffle and Ganache.