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


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:
Local RPC URL (e.g., http://127.0.0.1:8545)
Funded private keys you can use for deployment
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
π§΅ Threads β @pratik_adhav26
π¦ X (Twitter) β @PratikAdhav26
πΌ LinkedIn β Pratik Adhav
Subscribe to my newsletter
Read articles from Pratik Adhav directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
