Setting private blockchain with Ethereum and geth.

Prashant Prabhakar Singh
Sofocle Technologies
3 min readNov 12, 2017

The objective of this tutorial is set up a private ethereum blockchain on your machine using geth in 5 minutes.

I won’t go in depth of terms used in this post. So without wasting time let’s get started.

Note: These steps are for a Linux machine only. But it’s not much different on Windows and Mac.

#Setting up private net:

  1. Installations:

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo add-apt-repository -y ppa:ethereum/ethereum-dev
sudo apt-get update
sudo apt-get install ethereum

2. Make a folder anywhere in your system where you want to have your blockchain data and accounts:

cd ~/
mkdir eth-private
cd ~/eth-private

3. Make a file named customGenesis.json and paste the following content:

{
"config": {
"chainId": 7584, // any random no ,accept 1,2,3,0
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0,
"ByzantiumBlock": 0
},
"alloc" : {},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}

4. Make a folder named ‘myPrivateNetwork’ within your current directory.

5. Run the following command to initialize blockchain:

geth --identity "TestChain1" --cache=1024 --rpc --rpcport "8013" --rpccorsdomain "*" --datadir "myPrivateNetwork" --port "30312" -networkid 1902 --nat "any" init "customGenesis.json" console

6. Run following command to connect to blockchain:

Note: Replace ipcpath “/home/prsingh/.ethereum/geth.ipc” with yourHomeDirectory/.ethereum/geth.ipc in below mentioned command

geth --identity "TestChain1" --cache=1024 --rpc --rpcport "8013" --rpccorsdomain "*" --rpcapi "web3,eth,admin,debug,miner" --datadir "myPrivateNetwork" --port "30312" -networkid 1902 --nat "any" --ipcpath "/home/prashant/.ethereum/geth.ipc" console

Note: Now every time you need to run the private network, you only need to run above commands. Rest of the commands above are one-time process only.

I suggest making a script with these commands and run the script whenever you wish to launch the private net.

7. After running above command, your private network is set-up and running.

8. You can connect to blockchain instance by running ‘geth attach’ on any other terminal.

9. Create a new account by using: personal.newAccount(), it will ask for passphrase, enter the passphrase which will be used to unlock this account.

10. You can see the keystore file in eth-private/myPrivateNetwork/keystore folder.

11. For different commands to play around in geth, see: JSON-RPC.

12. In geth console, type miner.start() to start mining and your coinbase account (the first account you created) will start receiving rewards in ether.

13. You can check the balance of an account by using the command: eth.getBalance(‘0xB02…..’);

# Deploying smart contract Using Ethereum-Wallet

  1. Download latest Ethereum-wallet from https://github.com/ethereum/mist/releases

2. Unzip and run ethereum-wallet.

3. If you are already running geth as described in previous sections. Ethereum-wallet will automatically connect to your private net running on port 8013.

4. If not, just run the command listed under the 6th point of the previous section to start your private blockchain.

5. Once the wallet is launched, it will ask you to create a new account (if you have not yet created an account with personal.newAccount() command). provide the passphrase and a new account will be created.

6. Once the wallet is successfully launched, go to Contracts tab.

7. Click on deploy contract.

8. In the contact code section, put your code.

9. From the sidebar choose the contract you want to deploy (normally when you inherit other contracts you need to choose which contract you want to deploy)

10. Fill the parameters of contract if any (the arguments that we need to supply to the constructor of contract).

11. Choose the account from which you want to deploy the contract from the upper left section of the wallet.

12. You can change the fee you want to supply for your this transaction by using the fee slider placed below the contract code section. For now, let’s keep everything default.

13. Click Deploy and ether the password of account from which you want to deploy the coin.

14. Start the miner to mine your transaction. You already know how to start a miner, right? Use miner.start() command in geth console to start miner and miner.stop() to stop the miner.

15. Once the transaction is mined you can see a new Contract in Contracts tab of your wallet.

16. Congratulations ! You just set-up a private net and deployed a smart contract using Ethereum-Wallet.

--

--

Prashant Prabhakar Singh
Sofocle Technologies

Team lead at @SofocleTechnologies (www.sofocle.com) #Ethereum Developer #Blockchain Enthusiast