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.
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.
In Ubuntu, open a browser and navigate to https://github.com/trufflesuite/ganache/releases
Download the latest Linux release which will be the
Once the download is complete, open a new terminal and change into the directory with the
chmodto make the file executable:
chmod a+x ganache-1.3.0-x86_64.AppImage
Now run the file
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 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.
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 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:
First, ensure your system is up to date:
sudo apt update
Install NodeJS and it’s package manager,
sudo apt install nodejs npm
Verify the install was successful by checking the installed version of NodeJS:
Also verify that
npmwas installed successfully by checking the version:
This section describes how to install Truffle in Ubuntu 18.04
npmis install, you can install Truffle with this command:
sudo npm install -g truffle
Verify the installation by checking the truffle version:
Create a new truffle project
Create a new directory for the Truffle project and change into it:
mkdir myproject cd myproject
Generate an empty Truffle project
The new project will include some folders and files:
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.
First you need to configure Truffle to connect to Ganache. Open the
truffle-config.jsfile for editing and locate the
networkssection (around line
You need to uncomment the
developmentsection 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.
developmentsection. Set the
portvalues to match Ganache, then save the file.
Now in a terminal window, ensure you are in your project directory (e.g.
myproject) and run
You can read more about
truffle consolein the Truffle documentation: Using Truffle Develop and The Console.
web3commands in the Web3 documentation: https://web3js.readthedocs.io/en/1.0/.
For example, to list the accounts:
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)
This returns the current balance for the given address in wei, in this case
Close Truffle console by entering the
.exitcommand, or hit
Ctrl + Ctwice.
Congratulations, you have successfully setup a basic development environment for Ethereum smart contracts in Ubuntu 18 with Truffle and Ganache.