How to run an Ethereum testnet locally?

Hans-Helmut Kraus
Hans-Helmut Kraus
Ethereum smart contract auditor and security expert; 以太坊智能合约审计师与安全专家。

Hey there, friend! Want to set up an Ethereum test network on your own computer? No problem at all, it's much simpler than it sounds. Imagine it as playing a single-player online game where you can freely generate money and equipment (i.e., tokens), experiment however you like, all without spending a single real penny, and it runs incredibly fast. For learning and developing smart contracts, this is an invaluable tool.

Below, I'll introduce you to two of the most popular and convenient methods. You'll grasp them instantly after reading.


Method One: Using Hardhat (Preferred for Professional Development)

Hardhat is essentially the standard for Ethereum development today. It's not just a local test network; it's an all-in-one development toolkit that helps you compile, deploy, test, and debug smart contracts.

Imagine it as: A custom-built "Lego studio" equipped with not only infinite bricks (your local network) but also various tools for measuring, assembling, and testing.

Here are the steps:

  1. Preparation: Install Node.js You'll need to install Node.js on your computer first (v16 or higher recommended). This is the foundational environment for running Hardhat. Just download and install it from the Node.js official website.

  2. Create Project and Install Hardhat Find an empty folder, open your terminal (command-line tool), and type the following commands:

    # 1. Create a new folder and navigate into it
    mkdir my-eth-project
    cd my-eth-project
    
    # 2. Initialize a Node.js project
    npm init -y
    
    # 3. Install Hardhat
    npm install --save-dev hardhat
    
  3. Initialize Hardhat Project In the same terminal, run:

    npx hardhat
    

    It will ask you a few questions. For beginners, just press Enter to select the default options (to create a JavaScript project). Once done, your folder will contain some example contract, script, and configuration files.

  4. Start Your Local Test Network! This is the most crucial step. Run the following command:

    npx hardhat node
    

    Upon successful execution, you'll see output similar to this:

    Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/
    
    Accounts
    ========
    
    (0) 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 (10000 ETH)
    (1) 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 (10000 ETH)
    (2) 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC (10000 ETH)
    ... (many more follow)
    

    Congratulations! Your local Ethereum network is now up and running!

    • http://127.0.0.1:8545/ is your network address (RPC URL).
    • Listed below are 20 test accounts automatically generated for you. Each account has 10,000 virtual Ether, and their private keys are also provided for easy import into your wallet.

Method Two: Using Ganache (Graphical Interface, Beginner-Friendly)

If you're not a fan of command lines and prefer a more intuitive interface, then Ganache is perfect for you.

Imagine it as: An "Ethereum simulator" with a user interface. You can click around to see account details, balances, transaction records, and more, all very visually.

Here are the steps:

  1. Download and Install Go to the Truffle Suite official website to download Ganache. It supports Windows, Mac, and Linux. Install it just like any other software.

  2. One-Click Start Open the Ganache application, and you'll see a large "QUICKSTART" button.