Deploying Smart Contracts Locally with Foundry + Anvil β€” A Beginner's Guide

Pratik AdhavPratik Adhav
3 min read

Hey Web3 fam πŸ‘‹ β€” I'm back again and this time, I explored how to host a blockchain locally and deploy my smart contract using Foundry + Anvil.

This is a beginner-friendly walk-through of how I managed to deploy a Solidity smart contract to a local blockchain using Foundry tools. If you're also exploring Foundry or want to practice contract deployment safely, this post is for you!

πŸ’‘ Why Use Foundry?

Foundry is a super fast, Ethereum development toolkit that gives you complete control over smart contract development, testing, and deployment. Along with forge, cast, and anvil, you get a powerful local setup.

🧱 What I Built

I wrote a simple Solidity smart contract and deployed it to a local test blockchain using:

  • Foundry (for compiling & scripting)

  • Anvil (to simulate a local Ethereum network)

  • .env (to securely store RPC URL and private key)

πŸ“œ Step-by-Step Deployment Process

1️⃣ Create a Deploy Script

Inside your Foundry project, I created a script file like:

script/DeploySimpleStorage.s.sol

This file contains the logic to deploy my contract to the blockchain.

This is where you write code to deploy the contract just like you’d do in Remix, but it’s automated!

2️⃣ Create .env File (for local testing only ⚠️)

I stored my private key and RPC URL in a .env file like this:

PRIVATE_KEY=your_test_wallet_private_key
RPC_URL=http://127.0.0.1:8545

❗ Never do this in production!
For real deployments, use Foundry’s cast key management or hardware wallets.

πŸ›‘ Reminder

While .env is okay for local testing, for production deployments:

βœ… Use cast wallet with password-protected private key encryption
βœ… Never push .env files to GitHub
βœ… Consider using a .gitignore to exclude them

3️⃣ Start Your Local Blockchain with Anvil

anvil

Anvil will spin up a local Ethereum testnet. It gives you:

I copied the RPC URL and one of the private keys from Anvil into the .env file.

4️⃣ Add this local blockchain address to your meta mask as localhost

5️⃣ Load Environment Variables

Now load your .env variables into the terminal session using:

source .env

You can also put it in .bashrc or .zshrc to make it permanent.

To confirm everything is loaded, run:

echo $PRIVATE_KEY
echo $RPC_URL

5️⃣ Deploy to the Blockchain 🎯

Now it's time to actually deploy:

forge script script/DeploySimpleStorage.s.sol --rpc-url $RPC_URL --broadcast --private-key $PRIVATE_KEY

Boom πŸ’₯ β€” this deploys your smart contract to the local blockchain!

πŸ§ͺ Interacting with the Contract

To read or send transactions, Foundry gives you cast.

  • cast call β€” for view functions (read-only)

  • cast send β€” for state-changing transactions

Example:

bashCopycast call <contract-address> "retrieve()(uint256)" --rpc-url $RPC_URL

πŸ”— GitHub Repo

πŸ“‚ View full code and deploy instructions:
πŸ‘‰ https://github.com/Pratik-Adhav/foundry-local-deploy

πŸ”— Connect With Me

πŸš€ Building in public | Web3, Solidity, Smart Contracts

0
Subscribe to my newsletter

Read articles from Pratik Adhav directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pratik Adhav
Pratik Adhav