Setting up the Go Ethereum (geth) environment in Ubuntu Linux

Priyal Walpita
7 min readJul 5, 2018

--

In one of my previous posts, I have described how to set up and create Smart Contracts using Azure cloud platform. In this post, I will describe how to set up the Ethereum development environment along with local private blockchain in Ubuntu ( 17.04).

In this post, I am walking you through how to install required software, start the Ethereum node and performs basic transactions.

The environment we are going to set up has three main tools as follows.

1. The Go Ethereum Implementation or GETH

2. TestRPC that we are going to test put Smart Contracts

3. Truffle Build framework

Apart from these we need to install the nodejs because most of the tools that we are going to use are based on Javascript. So the first step is to install the nodejs and the node package manager ( npm ).

Installing NodeJS and NPM

Please go to this link and install the nodejs for Debian distribution. After the installation, you can check the installation status by checking the installed versions as follows.

$node -v

$npm -v

The results should be as follows. ( The versions may differ ).

Image 1 : Check node and npm versions

Next , we need to install the Geth.

Installing Geth

Geth , also known as Go Ethereum is a command line interface which allows us to run a full Ethereum node. Geth is implemented in GO and it will allow us to mine blocks , generate ether, deploy and interact with smart contracts , transfer funds , block history inspection and create accounts etc..

Geth is not the only implementation for Ethereum. As an example other implementations are parity, Ethereum C++, Ethereumj ( Java) , Pyethereum ( python ) ,Nethereum (.Net) and Node-ethereum (NodeJs)

Geth can be used to get connected to the public Ethereum network as well apart from to create your own private network for development purposes.

So first of all we need to install the software properties using the following command.

$ sudo apt install software-properties-common

After that we need to add the Ethereum repository as follows.

$ sudo add-apt-repository -y ppa:ethereum/ethereum

Then , update the repositories.

$ sudo apt update

Then install the Ethereum.

$ sudo apt install ethereum

You can check the installation by using the $ geth version command as follows.

Image 2 : Check geth version

The net tool we need to install is Test RPC.

Installing Test RPC

Test RPC is an Ethereum node emulator implemented in NodeJS. The purpose of this Test RPC is to easily start the Ethereum node for test and development purposes. Please note that Test RPC is just an emulator and it is running as an in-memory process. as an example if you deploy a Smart Contract , it will not persist if you restart the Test RPC.

to install the Test RPC ;

sudo npm install -g ethereumjs-testrpc
Image 3 : List all localaccounts

You can see that it created 10 test accounts and displays respective private keys. We can use these accounts for our testing purposes. By default the Test RPC is running on port 8545.

The next thing we need to install is Truffle.

Installing Truffle

Truffle is a build framework and it takes care of managing your Contract artifacts so you don’t have to. Includes support for custom deployments, library linking and complex Ethereum applications.

To install ;

$ sudo npm install -g truffle

check the installation as follows.

Image 4 : check truffle version

Now the installation is done.

Setting up a Private Ethereum network

First of all create a folder to host the database and private accounts that we are going to create. In my case it is ~/ETH/pvt.

First of all we need to place the genesis block in our root (~/ETH/pvt). The file should be defined as genesis.json

{
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x400",
"alloc": {},
"coinbase": "0x0000000000000000000000000000000000000000",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0xffffffff",
"config": {
"chainId": 4224,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
}
}

The nonce mixed with the mixhash is used by the proof of work consensus mechanism to ensure that enough computation has been used to validate the block. Difficulty param is directly linked with the time that a miner needs to spend in order to generate a new block. For a private blockchain like what we are building here, it is recommended to have a lower value to this in order to save processing time. Alloc param is placed where we can pre allocate some funds to few wallets ( addresses ) . Faucets are using this param to distribute pre-coins in a fresh network.

Coinbase param is the address where it should get rewards when mining this particular block. For the genesis block, it should go no where. Timestamp is also used to adjust the level of difficulty. If the time stamp between two blocks are too small the difficulty is increased ( in BitCoin they maintain this to around 10 mins). Timestamp is also used to maintain the sequence order of the blocks. GasLimit is the maximum amount of gas that can spend on a block.
The config section is to define the params in the chain. ChainId is the network id.

To initialize the chain instance , we need to use following geth command.

$ geth --datadir ~/Eth/pvt init genesis.json

in this command the — datadir param specifies where we should save our network’s data. After initializing this, the root folder should contain something like following.

Image 5 : Network folder structure

So now our network is initialized and as the next step we need to add new accounts.

We can use the following geth command for this purpose.

$ geth --datadir ~/Eth/pvt/ account new

This will request a password for the account.

To list all created account lists you can use the account list command as below.

Image 6 : Account list

As the next step, we need to specify following startnode.sh file. This script will start the network with given params.

startnode.sh

geth --networkid 4224 --mine --datadir "~/Eth/pvt" --nodiscover --rpc --rpcport "8545" 
--port "30303" --rpccorsdomain "*" --nat "any" --rpcapi eth,web3,personal,net --unlock 0
--password ~/Eth/pvt/password.sec --ipcpath "~/Library/Ethereum/geth.ipc"

We need to create the password.sec file with the password of first account that we need to unlock. “ — unlock 0 — password ~/Eth/pvt/password.sec”

the ipcpath is the path to the ipc file which is some kind of a flag that is used later on by mist to detect that there is already a node running.

As the next step we need to start the node using the Startnode.sh

So now the network is started and it is performing the mining and transactions.

As the next step we need to log into the Geth console using the attach command as follows.

$ geth attach http://127.0.0.1:8545 --datadir /home/priyal/Eth/pvt

This will start the Geth console. To list all accounts we can use following command.

> eth.accounts

I have created three accounts and following is the result.

To get the balance of the first node we can use following command.

> eth.getBalance(eth.coinbase)

This will give the values in lowest denominator which is WEI. If we need to cast it to the Eth we need to use following command.

> web3.fromWei(eth.getBalance(eth.coinbase),"ether")

We are using the web3 library in this instance.

In order to perform a transaction from one account to another we need to use following command .

> eth.sendTransaction({from:eth.coinbase, to: eth.accounts[1], value: web3.toWei(100,"ether")})

Now if you check the account 1’s balance, you will see 100 ether.

Conclusion

In this post, you have learned how to configure a local Ethereum network in your Ubuntu Linux machine and perform basic transactions. You can use this local set up to deploy Smart contracts and testing smart contracts as well.

--

--

Priyal Walpita
Priyal Walpita

Written by Priyal Walpita

CTO @ ZorroSign | Seasoned Software Architect | Expertise in AI/ML , Blockchain , Distributed Systems and IoT | Lecturer | Speaker | Blogger

Responses (3)