Skip to content

How to Setup Private local Ethereum Blockchain

Setup Private local Ethereum Blockchain

Ethereum can be deployed locally to run as a complete independent blockchain which may be needed for some organisation or a project. Local Ethereum blockchain can also be setup as a development platform. You can create Decentralized applications for Web3 and test them locally before deploying them on the actual Ethereum Mainnet. In this manner, you achieve complete ease of mind and also save the cost of gas fees for testing your application or smart contract.

The tutorial will be covered in two parts.

  • Part-1 Installing Geth on Ubuntu.
  • Part-2 Setting up Ethereum Nodes and connecting Remix to local Blockchain Nodes.

Part-1 Installing Geth on Ubuntu

Proper setting up Ethereum node on Linux node is essential to avoid future issues. It may seem like a cumbersome process to set up your own Ethereum node. But this guide will help you to set up your Ethereum node easily in a step by step process.

What is an Ethereum Node?

An Ethereum node in simple terms is a computer that participates as a component for ensuring the security and immutability of the Ethereum Blockchain. It stores a complete copy of the blockchain and helps in verifying transactions and blocks before they are made part of the blockchain.

Why to Setup your own Ethereum Node?

The Ethereum node helps to interact with the blockchain and helps to deploy our decentralized applications on the blockchain.  While there are other services like Infura that allow you to deploy your Daap without deploying an Ethereum node. But they suffer from the same problems as Web2.0. They can shut down their service, modify the data or simply refuse to offer the service. Infura has already shut down its services in the areas as per US law directions.

Once an Ethereum node is up and running, you can use the libraries like web3.js, Web3.py or Ether.JS to interact with the node. These libraries can use any of the following services to interact with nodes.

  • HTTP or Websockets
  • IPC
  • RPC

How to setup Ethereum Node

what is Geth

Geth is an Ethereum node that is written in Golang (Go) language and helps to interact with the Ethereum blockchain. Geth is officially supported by the Ethereum foundation and is maintained by a group of core Ethereum developers. It supports web3.js for interaction with web3 applications.

Geth supports the following Ethereum blockchains

  • Ethereum mainnet as a full node or light node.
  • Local Private Ethereum development Blockchains.
  • Ropsten testnet (Proof-of-work).
  • Rinkeby testnet: (Proof-of-authority) test network.
  • Görli testnet: (Proof-of-authority) test network.

Technical Requirement for installing Geth

Geth can be installed over for a variety of Operating systems and environments. Geth can be installed over the following operating systems.

  • Linux/ Ubuntu and other flavours.
  • macOS
  • FreeBSD
  • Archlinux
  • Windows (from source code)

If you are planning to run Geth full node, 8GB of RAM and 500GB of the hard disk are the minimum technical requirements. In this tutorial, we will see Geth installation over Ubuntu that can be followed for other Linux versions as well.

Basic Dependencies for setup Ethereum node

We are going to install the following software to get our Geth Node running.

  1. Prerequisite software and updates.
  2. Installing Nodejs.
  3. Installing Geth with PPAs (Personal Package Archives).
  4. Installing Golang and Geth from source code (Alternate method)

1. Prerequisite Software and updates.

Update and upgrade Linux

The first step is to update and upgrade our Linux distro. This can be done with the following commands.

sudo apt update
sudo apt upgrade
Install curl, GIT and build-essential

These are a few dependencies we need to get our Geth and nodejs working.

sudo apt-get install git
sudo apt-get install curl
sudo apt-get install build-essential

2. Installing NodeJS

NodeJs is a backend javascript platform that will help our application talk to the Ethereum node.

sudo apt-get install nodejs
sudo apt-get install npm
sudo apt update npm
Install nodejs on Kali
Install npm on kali linux

You must have upgraded npm repository. So, must update afterwords. Sometimes, we can get an error that nodejs or npm not found. then we need to go for the manual installation. You can go for the following code if you encounter any problems.

sudo apt-get install python-software-properties
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install nodejs
Install software-properties-common on kali

After you have installed nodejs, you can check the version of your nodejs by the following command

nodejs --version

3. Installing Geth

The easiest way to install go-Ethereum on Ubuntu-based distributions is with the built-in launchpad PPAs (Personal Package Archives)

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum
Install Ethereum node with PPA
Install geth node on Ethereum

You can check if the geth is installed properly by the following command.

geth version
Check geth version on linux

4. Installing Golang and Geth from source code (Alternate method)

If the Linux version is not supporting PPAs or you want to install a particular version of geth node, you can manually install the Golang language and then compile the geth node from the source code.

Installing Golang Go

Go language can be installed with the following command

sudo apt-get install golang-go

Now you need to setup paths in profile file. Open profile file with nano editor

sudo nano ~/.profile

Add the following lines to the end of the file.

export PATH=$PATH:/usr/local/go/bin

Now save the file with ctrl + o and press enter. Now exit the editor and refresh the source file. and check the go version.

source ~/.profile
go version
Installing Geth manually

Now geth can be installed manually with following commands.

git clone https://github.com/ethereum/go-ethereum.git 
cd go-ethereum 
make geth 

you can check if geth installed properly with the following command

geth version

Part-2 How to configure local Geth Nodes

Geth Nodes can be configured on a local machine by following the under mentioned steps for configuring as a local Ethereum blockchain.

  • Create Geth node directories to store blockchain data and Keystore.
  • Crate public-private key pairs for Geth nodes. If Geth node is used for mining, the same public key is used to get mining fees.
  • Create a Genesis file containing details about blockchain and block 0.
  • Initialize nodes with Genesis file.
  • Setup Password files for Geth Nodes.
  • Create a script file for running the Geth node automatically.
  • Running the nodes.
  • Connecting Geth node to Peers.

In this tutorial, to setup the local Ethereum blockchain, we will set up 3 Geth nodes locally. 2 Nodes will be configured as full nodes while the third node will be configured as lite node. We will then add them as peers to each other so that they can sync with each other and in the end, we will deploy a smart contract on the chain to check if we have configured everything perfectly.

Step-1 Creating Node Directories

To start let’s first check whether we have Geth installed properly by checking the Geth version.

geth version

If you get the version number as a reply, it means that the Geth node is working properly. Next is to create three node directories for our local Ethereum blockchain nodes.

mkdir -p ~/testNet/node1
mkdir -p ~/testNet/node2
mkdir -p ~/testNet/node3

This will create three directories under a directory “testNet”. “-p” flags tell Linux to create a parent directory if not available. “~/” refers to the home directory.

Make geth node directories

Step-2 Setup Geth node Pubic-Private key pairs.

The next step is to create accounts for all three nodes. This will create public/private keys which will be stored in the Keystore file in each node directory. Use the following commands to set up accounts.

geth --datadir ~/testNet/node1 account new
geth --datadir ~/testNet/node2 account new
geth --datadir ~/testNet/node1 account new

The commands will ask for the password that must be remembered. For the tutorial, we used the three passwords like node1Data, node2Data and node3Data for our three nodes.

Every node will have then a Keystore file under each node folder. Remember, Note down the public keys that will be displayed for each node. Write them separately as they will be used later. on.

Step-3 Create a Genesis file

The next step to creating a local Ethereum blockchain is to create a local genesis file. This file will contain all essential details about our local Ethereum blockchain and genesis block. to create a genesis file write the following command in the terminal.

sudo nano genesis.json
geth node genesis file

Now we will be using the following code as our genesis file code. You can configure the parameters as per your requirement.

{
  "config": {
    "chainId": 3611,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "ethash": {}
  },
  "alloc": { },
  "difficulty": "1",
  "gasLimit": "8000000",
  "timestamp": "0x00"
 
}

Step-4 Initialize Nodes with Genesis File

Now use the following commands to initialize the nodes with our genesis file.

geth --datadir ~/testNet/node1 init genesis.json
geth --datadir ~/testNet/node2 init genesis.json
geth --datadir ~/testNet/node3 init genesis.json
initialize geth node with genesis file

A Geth folder will now be automatically created under each node subdirectory.

Step-5 Setup Password file

Now we need to create a password.sec file in each node directory. This file will be used to automatically read the password once a Geth node is executed.

To create node 1 file create the file as under:-

sudo nano ~/testNet/node1/password.sec

Now save the following password which we created earlier.

node1Data
geth account password

Similarly, create password files for node2 and node 3.

sudo nano ~/testNet/node2/password.sec
node2Data
sudo nano ~/testNet/node3/password.sec
node3Data

Step-6 Creating Script files

The next step is to create script files for running the three nodes. For this, we need two things.

  • The public keys that we generated in step-2.
  • The Ip address of the Machine.

To check the Ip address of the machine, use the following command in the terminal.

ifconfig

Now to create the node 1 script file use the following command.

sudo nano ~/testNet/node1/startnode1.sh

Paste the following code in the file.

#!/bin/bash

geth --identity "node1" --networkid 3611 --syncmode "full" --datadir "~/testNet/node1" --nodiscover --ws --ws.port=8011 --ws.origins="*" --ws.addr "192.168.72.128" --http --http.corsdomain "*" --http.port "8041" --http.addr "0.0.0.0" --http.api "eth,net,personal,admin,miner,web3" --port "30303" --nat extip:192.168.72.128 --unlock 0 --password ~/testNet/node1/password.sec --ipcpath "~/testNet/node1/geth.ipc" --allow-insecure-unlock --mine --miner.threads=1 --miner.etherbase=0x23BCa221d46298cB79a7A39c02587cb346686AC4 --cache 2048 --log.debug console

Geth script

The complete explanation of the arguments can be seen by the command “get –help”. It can also be viewed online on the official Geth documentation page.

Geth has built-in support for JSON-RPC and clients can interact with nodes with RPS based APIs. They can be HTTP, WebSockets or IPC. IPC is enabled by default whereas HTTP and WebSockets have to be enabled.

In the above script, we have enabled HTTP with –HTTP command. Defined HTTP ports and WebSockets ports. we have also defined some APIs that we want to use line web3, admin etc. –http.corsdomain enables access from any web address like Remix IDE.

For node 2 create a script file.

sudo nano ~/testNet/node2/startnode2.sh

Now paste the following code. Just see, we have changed ports, and public keys.

#!/bin/bash

geth --identity "node2" --networkid 3611 --syncmode "full" --datadir "~/testNet/node2" --nodiscover --ws --ws.port=8012 --ws.origins="*" --ws.addr "192.168.72.128" --http --http.corsdomain "*" --http.port "8042" --http.addr "0.0.0.0" --http.api "eth,net,personal,admin,miner,web3" --port "30304" --nat extip:192.168.72.128 --unlock 0 --password ~/testNet/node2/password.sec --ipcpath "~/testNet/node2/geth.ipc" --allow-insecure-unlock --mine --miner.threads=1 --miner.etherbase=0x59Ce78257A39bB188b82cb3F5074bA1BA9df3B25 --cache 2048 --log.debug console

For node 3, the file can be created as:-

sudo nano ~/testNet/node3/startnode3.sh

Use the following script. just see we have removed the miner part from the script as 3rd node is being configured as a lite node only.

#!/bin/bash
geth --identity "node3" --networkid 3611 --syncmode "light" --datadir "~/testNet/node3" --nodiscover --ws --ws.port=8013 --ws.origins="*" --ws.addr "192.168.72.128" --http --http.corsdomain "*" --http.port "8043" --http.addr "0.0.0.0" --http.api "eth,net,personal,admin,miner,web3" --port "30305" --nat extip:192.168.72.128 --unlock 0 --password ~/testNet/node3/password.sec --ipcpath "~/testNet/node3/geth.ipc" --allow-insecure-unlock --cache 2048 --log.debug console
geth script

Now Make these script files as executable.

sudo chmod +x ~/testNet/node1/startnode1.sh
sudo chmod +x ~/testNet/node2/startnode2.sh
sudo chmod +x ~/testNet/node3/startnode3.sh

Step-7 Running the local Geth Nodes

Once we have setup the above things. The next step is to run our three nodes. Open three separate terminals and execute the script files as under.

sudo .~/testNet/node1/startnode1.sh
sudo .~/testNet/node2/startnode2.sh
sudo .~/testNet/node3/startnode3.sh

The nodes will execute and you will see that node 1 and node 2 will start mining the blocks as well. However, they will not be in sync. To sync nodes we need to add them as a peer to each other.

Step-8 Connecting Geth nodes as peers

To add a peer, we need its enode id. goto each terminal window and type the following command

admin.nodeInfo

Use the above command on all three nodes and then note their enode IDs.

Check geth node IDs

Now add node 2 and node 3 to node 1 as peers. similarly, add nodes to peer with each other so that their blockchains become synced. For this, you need to attach a console to get the node. To attach a console to node1, and use the following commands in a separate terminal.

geth attach http://127.0.0.1:8041

Now add peers to it with the following command. Note the enode ID will be sed which has been obtained above.

admin.addPeer(enodeID)
add peers to ethereum Node

Now, we will have our local Ethereum Blockchain synced as ready as a development platform. You can see that your all three nodes will reach at the same block height.

local ethereum blockchain

Setting up REMIX IDE with local Ethereum Blockcahin

REMIX is an online IDE for solidity. It can be linked to your local geth nodes , so that you are able to deploy your smart contracts directly on the local geth node.

Open REMIX IDE and open contracts. There are alresy a few contracts on REMIX IDE that we can use as a sample to deploy on our Geth node.

Remix Smart contracts

Now switch to compile tab and compile the contract.

compile smart contract on REMIX

Now, you can connect the REMIX to your node-1. Slectec web-3 provider and enter details of node 1. You will see that wallet address of node-1 will automatically appear in REMIX if it is connected fine.

local ethereum node and remix
node id in remix
wallet address of geth node

Now click Deploy, your contract will be deployed to the local Ethereum blockchain through node1.

contract deploy on local ethereum blockchain

FAQ

What is Geth?

Geth is an Ethereum blockchain node that is written in golang language and is maintained by ethereum developers

Can you run your local Ethereum Blockchain?

Yes, Ethereum nodes like Geth can be setup on local network and local ethereum blockchain can be deployed for use.

What is the Easiest way to install Geth on Ubuntu?

The easiest way to install Geth nodes is to install geth through PPA (Personal package archives. Geth can be installed in 3-4 commands by using this method.

What is a Genesis file in Ethereum?

Genesis file is the initialization file for Ethereum blockchain. It contains the block zero and other important details of the blockchain.

Hassan

Hassan is Telecommunications Engineer working in Web 3.0 Technologies. He has various IT certifications like CCENT, HCNA, HCNP and loves to write about new technologies

Leave a Reply

Your email address will not be published.