Setting Up An Ethereum Development Environment With HardHat

Get your solidity local environment up and running

Setting Up An Ethereum Development  Environment With HardHat

Hardhat, as the name implies, might be Kind of hard to set up for beginners. I also struggled at first. I will work you through a simple way to get things up and running.

What is Hardhat?

Hardhat is an Ethereum development environment that helps you compile, test, debug, deploy and run your solidity smart contracts locally.

Before installing Hardhat, you must have node/npm installed in your pc. From version 16 upward. If not, head over here to download and install it with the latest version. Very easy!

Install Hardhat

Next, open your terminal( for Windows use the command prompt). Works better if you run as an administrator. cd into the directory you want to work in. Then make a new directory by creating your project folder like this :

mkdir your-project-folder-name

Next, change directory into the new project folder you just created, here:

cd your-project-folder-name

Then run this command:

npm init -y

This will create a new package.json file in your folder. Next, run this:

npm install --save-dev hardhat

The command to install hardhat. This might take a little while. You may see a message about some vulnerabilities, but not to worry, It's just a security check by npm. You can read more about it here.

Create a Hardhat Sample Project

When it's done installing we can now create a sample project. Run this:

npx hardhat

Choose the option to create a basic sample project and accept yes to everything.

$ npx hardhat
888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888 "Y888888 888     "Y88888 888  888 "Y888888  "Y888

Welcome to Hardhat v0.8.4

? What do you want to do? 
 Create a sample project
  Create an advanced sample project
  Create an advanced sample project that uses TypeScript
  Create an empty hardhat.config.js
  Quit

Install Hardhat-Waffle and Ethers

Now that we have our basic sample project we will need to install these other dependencies, hardhat-waffle and hardhat-ethers which will be needed in your project. Run this:

npm install  -D @nomiclabs/hardhat-waffle ethereum-waffle

to install waffle. This could take quite some time, and when it is done run this:

npm install  -D @nomiclabs/hardhat-ethers ethers

to install ethers. It is much easier to install them separately.

At this point, you can open your VSCode by typing:

code .

in the same directory in your terminal to open your newly created project folder. There you will see the sample project hardhat just created for you and the package.json file you created earlier.

Compile and Test Your Contract

To compile the code run:

npx hardhat compile

The compiled artifacts will be saved in the artifacts/ directory by default.

Here is the outcome

$ npx hardhat compile
         Compiling...
Compiled 1 contract successfully

So in order to run the test you will need to have chai installed. Run:

npm install --save-dev chai

It makes testing much easier.

When done run:

npx hardhat test

to test your code.

Here is the outcome

$ npx hardhat test
Compiling 1 file with 0.8.4
Compilation finished successfully


  Greeter
Deploying a Greeter with greeting: Hello, world!
Changing greeting from 'Hello, world!' to 'Say Hi, to Michy!'
    ✓ Should return the new greeting once it's changed (803ms)


  1 passing (805ms)

Using Openzeppelin

So now, incase you will be using Openzeppelin in your project just run this command to install it:

npm install @openzeppelin/contracts

Openzeppelin is a library that is used to develop secure smart contracts. It provides security, reliability, and risk management for ethereum projects. It also provides reusable Solidity components to build custom contracts and complex decentralized systems. If you want to learn more about it read here. And If you won't be needing it then skip this part.

Deploy Smart Contract

Lastly, to deploy the smart contract you will need a hardhat script. Hardhat already created one for you in the sample project. In your VSCode, check the script folder and you will find a sample-script.js file. Now let's deploy the contract, run:

npx hardhat run scripts/sample-script.js

And here is the outcome


$ npx hardhat run scripts/sample-script.js
Compiling 2 files with 0.8.4
Compilation finished successfully
Deploying a Greeter with greeting: Hello, from Mich!
Greeter deployed to: 0x218d3686d4d45e5ecaaab8b451a1cf13a93329ec

Yes! Now you should see your deployed contract printed on the terminal with the message and your smart contract address, yippee!! So what that means is that your local environment is all set up and you have run/deployed a smart contract to a local blockchain.

That's it! Thanks for reading.

Your comments and contributions are welcome.