Deploying Solana Programs
Deploying to Solana is straightforward with Anchor, but there are important considerations for security and upgrades.
Building Your Program
anchor build
This compiles your program and generates:
target/deploy/my_program.so- The compiled programtarget/idl/my_program.json- The Interface Definition Language filetarget/types/my_program.ts- TypeScript types for your program
Deploying to Devnet
- Configure your CLI for devnet:
solana config set --url devnet
- Ensure you have SOL:
solana balance
solana airdrop 2 # If needed
- Deploy:
anchor deploy
Anchor will:
- Upload the program binary
- Create the program account
- Set you as the upgrade authority
Understanding Program IDs
Your program ID is declared in lib.rs:
declare_id!("YourProgramIDHere");
This ID is generated when you run anchor init and stored in target/deploy/my_program-keypair.json.
Important: Once deployed, the program ID is permanent for that deployment. If you want a new ID, you need to create a new keypair.
The IDL (Interface Definition Language)
The IDL is a JSON file describing your program's interface:
{
"version": "0.1.0",
"name": "my_program",
"instructions": [
{
"name": "initialize",
"accounts": [
{ "name": "myAccount", "isMut": true, "isSigner": true },
{ "name": "user", "isMut": true, "isSigner": true },
{ "name": "systemProgram", "isMut": false, "isSigner": false }
],
"args": []
}
],
"accounts": [
{
"name": "MyAccount",
"type": {
"kind": "struct",
"fields": [
{ "name": "data", "type": "u64" }
]
}
}
]
}
Clients use the IDL to interact with your program.
Uploading the IDL
anchor idl init -f target/idl/my_program.json <PROGRAM_ID>
This stores the IDL on-chain, making it easier for tools like Anchor Explorer to understand your program.
Update an existing IDL:
anchor idl upgrade -f target/idl/my_program.json <PROGRAM_ID>
Program Upgrades
Solana programs are upgradeable by default. The upgrade authority can replace the program code:
anchor upgrade target/deploy/my_program.so --program-id <PROGRAM_ID>
Checking Upgrade Authority
solana program show <PROGRAM_ID>
Output:
Program Id: YourProgramID
Owner: BPFLoaderUpgradeab1e11111111111111111111111
ProgramData Address: ...
Authority: YourWalletPublicKey
Last Deployed In Slot: 123456789
Data Length: 200000 bytes
Transferring Upgrade Authority
solana program set-upgrade-authority <PROGRAM_ID> --new-upgrade-authority <NEW_AUTHORITY>
Revoking Upgrade Authority (Immutable Program)
For maximum security, you can make a program immutable:
solana program set-upgrade-authority <PROGRAM_ID> --final
Warning: This is irreversible! No one can ever upgrade the program again.
Multisig Upgrade Authority
For production programs, use a multisig as the upgrade authority:
# Create a multisig with Squads Protocol
# Set multisig as upgrade authority
solana program set-upgrade-authority <PROGRAM_ID> --new-upgrade-authority <MULTISIG_ADDRESS>
This requires multiple team members to approve upgrades.
Verifiable Builds
Verifiable builds let anyone verify your deployed program matches your source code:
anchor build --verifiable
This builds your program in a Docker container with a deterministic environment.
Verify a deployed program:
anchor verify <PROGRAM_ID>
Deploying to Mainnet
- Switch to mainnet:
solana config set --url mainnet-beta
-
Fund your wallet (deploying costs ~2-5 SOL depending on program size)
-
Audit your code (get a professional audit for production programs)
-
Deploy:
anchor deploy
- Upload IDL:
anchor idl init -f target/idl/my_program.json <PROGRAM_ID>
- Set up multisig authority or revoke authority if appropriate
Best Practices
- Test on devnet first - Always deploy and test on devnet before mainnet
- Use verifiable builds - Builds trust with users
- Store keypairs securely - Never commit
target/deploy/*-keypair.jsonto Git - Plan your upgrade strategy - Multisig for most cases, immutable for critical infrastructure
- Monitor program accounts - Track rent, data size, and usage
- Document your IDL - Add comments to your Rust code; they appear in the IDL
Common Deployment Issues
Insufficient SOL
Error: Account <PROGRAM_ID> has insufficient funds
Solution: Airdrop more SOL (devnet) or fund your wallet (mainnet)
Program Already Deployed
Error: Program already exists
Solution: Use anchor upgrade instead of anchor deploy
Wrong Upgrade Authority
Error: Upgrade authority mismatch
Solution: Ensure the wallet you're using matches the upgrade authority
Next Challenge
You'll write a test that simulates a program execution scenario.