Skip to main content

Deploy a contract using Truffle

In this tutorial, you'll create a simple smart contract and use Truffle to compile and then deploy the smart contract on the Sepolia network.



You can use MetaMask or similar to create an Ethereum wallet for testing purposes.


1. Install Truffle

Truffle is a smart contract development tool and testing framework for blockchain networks.

Install Truffle globally using the Node.js package manager:

npm install -g truffle

2. Fund your Ethereum account

Use the Infura faucet to load testnet ETH on your Ethereum account for the Sepolia network.

If using a network other than Sepolia, ensure you update your environment file with the network name.

3. Create a project directory

Create a new directory for your project. This can be done from the command line:

mkdir truffleProject

Change into the new directory:

cd truffleProject

4. Install Dotenv

Install the dotenv package to allow you to use a .env file to store private environment variables on your local machine.

npm install dotenv

This example uses the .env file to store your wallet's mnemonic, and the Infura API URL.

5. Create a Truffle project

Create a bare Truffle project which generates the required directory structure to test and deploy contracts:

truffle init

Truffle creates the following directory structure for your project:

6. Install hdwallet-provider

hdwallet-provider is a separate package that can sign transactions for addresses derived from a 12 or 24-word mnemonic. By default, the hdwallet-provider uses the first address that's generated from the mnemonic however, this behavior is configurable.

Refer to the Truffle hdwallet-provider repository for more usage instructions.


Infura does not manage your private keys, meaning it cannot sign transactions on your behalf.

Run the following command to install hdwallet-provider:

npm install @truffle/hdwallet-provider

You can also use the Truffle Dashboard to allow MetaMask to sign your transactions.

7. Create the .env file

Create a .env file in your project directory to store the project and Ethereum account details.

In the example the file includes your MetaMask secret recovery phrase. Refer to the MetaMask instructions on how to reveal a secret recovery phrase.

MNEMONIC = "<Your-MetaMask-Secret-Recovery-Phrase>"

Ensure you replace the following values in the .env file:

  • <Your-API-Key> with the API key of the Ethereum project.
  • <Your-MetaMask-Secret-Recovery-Phrase> with the mnemonic of your MetaMask wallet. This phrase is used by the Truffle hdwallet-provider to sign transactions.

Never disclose your secret recovery phrase. Anyone with your recovery phrase can steal any assets held in your wallet.

8. Create a smart contract

Using an editor, create a smart contract in the contracts/ directory. In this example, we'll create a basic contract called Demo.sol.

pragma solidity >=0.5.8;

contract Demo {
event Echo(string message);

function echo(string calldata message) external {
emit Echo(message);

9. Configure the Truffle settings

Configure the truffle-config.js file to use the HDWalletProvider and include the required configuration to deploy to the network (Sepolia in this example).\ \ You can find the truffle-config.js file in your project's root directory.

const HDWalletProvider = require("@truffle/hdwallet-provider");
const { INFURA_API_KEY, MNEMONIC } = process.env;

module.exports = {
networks: {
development: {
host: "",
port: 8545,
network_id: "*",
sepolia: {
provider: () => new HDWalletProvider(MNEMONIC, INFURA_API_KEY),
network_id: "11155111",
gas: 4465030,

Refer to the Truffle documentation for more information about configuring the Truffle settings.

10. Compile the smart contract

To compile the smart contract, navigate to the project's root directory and run the following:

truffle compile

If successful, you'll see output similar to:

Compiling your contracts...
> Compiling ./contracts/Demo.sol
> Artifacts written to /Users/myuser/truffleProject/build/contracts
> Compiled successfully using:
- solc: 0.5.16+commit.9c3226ce.Emscripten.clang

Artifacts of your compilation are placed in the build/contracts/ directory, relative to your project root. Refer to the Truffle documentation for more information about the compilation process.

11. Create the deployment script

Scripts to deploy smart contracts are located in the migrations/ directory and are numbered. In the migrations/ directory, create a file called 1_deploy_contract.js.

Add the following code to the 1_deploy_contract.jsfile:

const Demo_Contract = artifacts.require("Demo");

module.exports = function(deployer) {

12. Deploy the smart contract

You need to run the migration command to deploy the smart contract to the Ethereum network.

To deploy the contract, run the following command:

truffle migrate --network sepolia

If successful you'll see a response similar to the following:

Starting migrations...
> Network name: 'sepolia'
> Network id: 11155111
> Block gas limit: 30000000 (0x1c9c380)


Deploying 'Demo'
> transaction hash: 0xafb1fa110d77f1447434418539e3fd36a97c0f948cc83e793fa394c882a1fab1
> Blocks: 3 Seconds: 29
> contract address: 0x4Ee0845a9eBfB04Bcc225b70Dc78c4789C61c64E
> block number: 7588035
> block timestamp: 1663126548
> account: 0x9cE564c7d09f88E7d8233Cdd3A4d7AC42aBFf3aC
> balance: 0.088395204941580514
> gas used: 123421 (0x1e21d)
> gas price: 4.303203766 gwei
> value sent: 0 ETH
> total cost: 0.000531105712003486 ETH

> Saving artifacts
> Total cost: 0.000531105712003486 ETH

> Total deployments: 1
> Final cost: 0.000531105712003486 ETH

Use a block explorer like Etherscan to view the transaction details. For example, use the transaction hash or contract address to view the details.

Next steps

You can now make calls to the deployed contract. A contract call is a transaction that will consume gas on the public Ethereum network.