How to set up a private Ethereum blockchain and deploy a Solidity Smart Contract on the blockchain — in less than 20 mins!

From Zero to One: A step-by-step tutorial on setting up a private Ethereum blockchain and deploying Solidity smart contracts

Prashant Ram
Blockchain Bistro
8 min readAug 1, 2018

--

In the following tutorial we will,

  1. Install the requisite software packages to run a private Ethereum blockchain on a Mac.
  2. Run a private blockchain in a test environment, and “mine” dummy “ether” and “gas” ( to run smart contracts).
  3. Create a “Hello World” smart contract in Solidity, compile it using Truffle and deploy it to the private Ethereum network.
  4. Run the “hello world” smart contract within the truffle console.

And we will do all of this within 15 mins or less, so let’s begin!

Step 0: Pre-installation

Before you begin installation make sure you have latest versions of Homebrew and Node/npm.

To install Homebrew:

  1. go to https://brew.sh/
  2. Cut-paste the latest homebrew cURL within your terminal

Once homebrew installation is complete, verify you have the latest version.

To install Node and npm:

  1. Go to https://nodejs.org/en/ and install Node and NPM

Once node and npm are install verify the version number.

Step 1: Installing Ethereum, Solidify and Truffle

Next we will install the following,

  1. Geth (go-etheruem): This is command line client interface that allows one to connect to the Ethereum blockchain.
    (The other clients that can be used to connect to Ethereum are Eth (C++ implementation), and PyEthApp(Python implementation).
    All the clients have almost identical functionality and Geth is the most suited if you are familiar with javascript/web development language).
  2. Truffle: This is the Solidity Framework and IDE, which is used to compile, deploy and test your Smart contract.
  3. Solidity compiler: The Truffle framework will use the Solidity compiler.

Let’s continue,

  1. Installing Geth

2. Installing Truffle

3. Installing Solidity

Step 2: Setup and configuration

This step will include the following,

  1. Configuring the genesis block, and
  2. Initiating the chain data in the blockchain

Create a new project directory, and create the following genesis.json

1.Configuring the Genesis Block
You can use Visual Studio code or any other editor to cut-paste the following code in the genesis.json file.

Explanation of the config file:

  • chainId: A unique identifier of the new private blockchain
  • homesteadBlock: Homestead is the first production release of Ethereum and since the developers are already using this version the value of this parameter can be left as ‘0’.
  • eip155Block/eip158Block: EIP stands for “Ethereum Improvement Proposals”, these were implement to release Homestead. In a private blockchain development hard forks aren’t needed, hence the parameter value should be left as ‘0’.
  • difficulty: controls the complexity of the mining puzzle and a lower value enables quicker mining.
  • gasLimit: Establishes an upper limit for executing smart contracts.
  • alloc: allows allocation of Ethers to a specific address.

2. Creating the folder where your blockchain will reside

The second line initializes the blockchain and the blockchain data will be stored in the “blkchain” folder. This folder will grow in size as data is added to the blockchain, and if this folder is deleted the block chain will need to be reinitialized.

Step 3: Initiate and Run the Private Ethereum Blockchain

  1. Bring up the Private Ethereum blockchain!

To get the private Ethereum blockchain up and running, open up a new command terminal window and run the following in the “projects” folder.

Explanation of the command line above:

— networkid: identity of your Ethereum network for other peers to discover. You can use any random number of your choice(except the ones listed below) to create your own network and to prevent others from inadvertently connecting to your network.

The following network id’s are reserved for specific Ethereum networks.

  • 0: Olympic, Ethereum public pre-release testnet
  • 1: Frontier, Homestead, Metropolis, the Ethereum public main network
  • 1: Classic, the (un)forked public Ethereum Classic main network, chain ID 61
  • 1: Expanse, an alternative Ethereum implementation, chain ID 2
  • 2: Morden, the public Ethereum testnet, now Ethereum Classic testnet
  • 3: Ropsten, the public cross-client Ethereum testnet
  • 4: Rinkeby, the public Geth PoA testnet
  • 8: Ubiq, the public Gubiq main network with flux difficulty chain ID 8
  • 42: Kovan, the public Parity PoA testnet
  • 77: Sokol, the public POA Network testnet
  • 99: Core, the public POA Network main network
  • 7762959: Musicoin, the music blockchain
  • 61717561: Aquachain, ASIC resistant chain

— nodiscover: Disables the peer discovery mechanism (manual peer addition).

— datadir: indicates the data directory where your blockchain will reside.

— maxpeers: Maximum number of network peers (network disabled if set to 0) (default: 25)

— rpc: Enable the HTTP-RPC server

— rpcapi: this allows us to communicate with the Ethereum network using the web3js RPC methods in the Geth Javascript console.

2. Connect to the private Ethereum blockchain using the Geth Javascript console.

Open a new command terminal and navigate to the “projects” folder. Run the following to launch the Geth Javascript Console,

This will open the Geth Javascript console, that is connected to the private Ethereum blockchain network running on localhost:8543

3. Create an account and “mine” for dummy Ether.

The Geth Javascript console, provides web3js api’s to create a new account. Use the following Geth commands to create a new account and unlock it.

You can now start mining dummy “ether” using miner.start()

In your “Geth network terminal window”, you will now see “ether” being mined.

Screenshot of the Geth Terminal window running the Private Ethereum Network (Dummy Ether being mined)

To check your “ether” balance, use the following command in the Geth Console.

And to stop mining use,

Screenshot of Terminal running the Geth Javascript console.

Step 4: Create a “Hello World” Smart Contract in Solidity, and deploy it to the private Ethereum Blockchain

1.Initialize Truffle
Create a new folder in the “projects” directory to initialize truffle

Screenshot of creating truffle folder and truffle init

2. Create a Smart Contract.
Go to the “contracts” folder in the “truffle” directory, and create a new file called “Hello.sol”. You can create this file in Visual Studio, and cut paste the following code.

This is the smart contract written in Solidity. Notice that the Solidity smart contract code is very similiar in form to Javascript.

3. Configure the Truffle migrations folder to deploy the Solidity code.
Go to the “migrations” folder in the “truffle” folder, and create a new file called 2_deploy_contracts.js, with the following code.

Notice that the above code is Nodejs.

4. Update the truffle task runner with your account and network details. Update “truffle.js” with the following code.

Note that the “from” field should contain your account id, which was create in the Geth Javascript console when you ran “personal.newAccount(‘seed phrase’). In order to get this again you can use the following command in the Geth Javascript console.

5. Compile the smart contract in Truffle, and deploy it to the private blockchain

Screenshot of truffle compile
Screenshot of truffle migrate

Troubleshooting:

  • Ensure that you are in the “truffle” folder to run the commands above.
  • The deployment of the smart contract to the blockchain may take a few seconds.
    Ensure that you are mining blocks at the time of smart contract deployment, so that the bytecode is added to the newly mined block of the blockchain. In case you stopped mining before, you can restart mining using the following command in the Geth Javascript console.

Step 5: Execute the Smart contract on the private Ethereum blockchain

In this step we will execute our “Hello World” smart contract on the blockchain using the Truffle console.

Open a new Terminal window and navigate to the “truffle” folder. To launch Truffle console from the terminal shell,

Within the truffle console, run the following commands

You should see the following result,

Screenshot of Truffle Console running the Solidity Smart Contract on the Private Blockchain

Yay!!! You have now successfully created your very first private Ethereum blockchain and run your very first Solidity code on the Blockchain!

We covered a lot in this tutorial and following is a summary of all the steps.

Found this post useful? Hit the 👏 button below to show how much you liked it :)

Follow me on Medium for the latest posts on Blockchain!

--

--

Prashant Ram
Blockchain Bistro

Technologist, Author, Speaker-with a passion for learning new things every day. Specializing in helping Startups and Enterprises move to the modern web!