Deploying Programs
This section walks through the basic process of deploying a Solana program using the Solana CLI.
CLI Command Reference
Task | Command |
---|---|
Build program | cargo build-sbf |
Deploy new program | solana program deploy <path_to_program.so> |
Update existing program | solana program deploy <path_to_program.so> (same as deploy) |
Show program info | solana program show <program-id> |
Transfer program authority | solana program set-upgrade-authority <program-id> --new-upgrade-authority <path_to_keypair> |
Make program immutable | solana program set-upgrade-authority <program-id> --final |
Close program | solana program close <program-id> --bypass-warning |
Check wallet balance | solana balance |
Request airdrop (devnet/localhost) | solana airdrop 2 |
Key Concepts
Before diving in, let's clarify some terminology:
- Program ID: The on-chain address of your program. Users interact with your program by referencing the Program ID.
- Program Account: The on-chain account that stores your program's metadata. The program account's address is the Program ID. The program account also stores the address of the ProgramData Account.
- ProgramData Account: The on-chain account that stores your program's deployed executable code.
- Program Authority: The account that has permission to update or close the program. By default, this is your CLI wallet.
Prerequisites
Set Up Wallet
After installation, create a local keypair wallet. The address of this wallet is set as the default program authority when deploying programs:
$solana-keygen new
This creates a keypair at ~/.config/solana/id.json
by default.
Configure Cluster
Choose which Solana cluster to deploy to. Use the solana config get
command to
check your current configuration:
$solana config get
Switch between clusters as needed:
$solana config set --url mainnet-beta$solana config set --url devnet$solana config set --url localhost$solana config set --url testnet$solana config set --url "https://your-rpc-url.com"
Fund Wallet
You'll need SOL to pay for program deployments. The amount depends on your program size.
For devnet or localhost, fund your wallet using the solana airdrop
command:
$solana airdrop 2
Check your wallet balance:
$solana balance
Basic Operations
Build Program
To build a program, use the cargo build-sbf
command:
$cargo build-sbf
Example Program
Here is a minimal Solana program that prints "Hello, Solana!" to the program logs.
use solana_program::{account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey,};entrypoint!(process_instruction);pub fn process_instruction(_program_id: &Pubkey,_accounts: &[AccountInfo],_instruction_data: &[u8],) -> ProgramResult {msg!("Hello, Solana!");Ok(())}
Build Output
Build your program using the cargo build-sbf
command:
$cargo build-sbf
This creates two important files in target/deploy/
:
hello_world-keypair.json
: A keypair file whose public key will be used as your Program IDhello_world.so
: The compiled program executable
Check Program Size and Cost
Deploying a program requires SOL to be allocated to the program account based on the size of the program. Larger programs cost more SOL to deploy.
Check your program's size:
$wc -c < ./target/deploy/hello_world.so
Calculate the required SOL for this size (bytes):
$solana rent 18504
You'll need slightly more SOL than shown to cover deployment transaction fees.
Deploy Program
Use the solana program deploy
command to deploy your program:
$solana program deploy ./target/deploy/hello_world.so
The Program Id shown is your program's permanent address on the network.
To deploy with a specific Program ID (instead of the auto-generated one), use:
$solana program deploy ./target/deploy/hello_world.so --program-id ./custom-keypair.json
You can generate new keypairs using the solana-keygen
command:
$solana-keygen new -o ./custom-keypair.json
Update Program
Update your program using the same solana program deploy
command:
- Make changes to your program code
- Rebuild your program:
cargo build-sbf
- Deploy the update:
$solana program deploy ./target/deploy/hello_world.so
If your updated program requires more space (bytes) than currently allocated, the deployment will automatically extend the program account. This happens when your new program is larger than the previous version. The program account needs additional bytes to store the new program. The CLI will calculate the required SOL and deduct it from your wallet automatically.
You can also manually extend a program to allocate more bytes:
$solana program extend 7NxQjW8H8hVcqBKgXbVDWqGQCovHbqDW9p1SJJwUyTpE 1000
Program Management
Once your program is deployed, there are several common commands to manage the program account.
View Program Metadata
To check your program metadata, use the solana program show
command:
$solana program show 7NxQjW8H8hVcqBKgXbVDWqGQCovHbqDW9p1SJJwUyTpE
Example output:
$ solana program show 7283x8k8fyBcfaFLyPsxbd2VV1AMmZFP7FNoyTXVKJw7Program Id: 7283x8k8fyBcfaFLyPsxbd2VV1AMmZFP7FNoyTXVKJw7Owner: BPFLoaderUpgradeab1e11111111111111111111111ProgramData Address: Gqn7YQVCP8NtYV1qkEqR4Udhj8EJ3fkikvS7HskCNQ7cAuthority: 5kh6HxYZiAebF8HWLsUWod2EaQQ6iWHpHYCz8UcmFbM1Last Deployed In Slot: 6573Data Length: 18504 (0x4848) bytesBalance: 0.12999192 SOL
Transfer Program Authority
To transfer the program authority to a different account, use the
solana program set-upgrade-authority
command:
$solana program set-upgrade-authority 7NxQjW8H8hVcqBKgXbVDWqGQCovHbqDW9p1SJJwUyTpE --new-upgrade-authority ./new-authority-keypair.json
After transferring the program authority, you can no longer update the program unless you have access to the new authority keypair.
Make Your Program Immutable
To make your program immutable, use the solana program set-upgrade-authority
command with the --final
flag to remove the program authority:
$solana program set-upgrade-authority 7NxQjW8H8hVcqBKgXbVDWqGQCovHbqDW9p1SJJwUyTpE --final
Once a program is immutable, it can never be updated or closed.
Close Your Program
To close your program and reclaim the SOL allocated to the program account, use
the solana program close
command:
$solana program close 7NxQjW8H8hVcqBKgXbVDWqGQCovHbqDW9p1SJJwUyTpE --bypass-warning
Once closed, the Program ID can not be reused. You cannot deploy a new program to the same address.
Utility Commands
List All Programs
View all programs where your current wallet is the authority:
$solana program show --programs
Example output:
Program Id | Slot | Authority | Balance-------------------------------------------------|------------|------------------------------------------------|-------------7NxQjW8H8hVcqBKgXbVDWqGQCovHbqDW9p1SJJwUyTpE | 249885434 | 5kh6HxYZiAebF8HWLsUWod2EaQQ6iWHpHYCz8UcmFbM1 | 0.129991923KSGCk4m5hqJkTjPHBXUCPcqMwJnK3VmkqEsFUKKGJPK | 249883212 | 5kh6HxYZiAebF8HWLsUWod2EaQQ6iWHpHYCz8UcmFbM1 | 0.28654328
Download a Deployed Program
To download a deployed program, use the solana program dump
command:
$solana program dump 7NxQjW8H8hVcqBKgXbVDWqGQCovHbqDW9p1SJJwUyTpE ./downloaded_program.so
Advanced Options
Deployment Flags
When the Solana network is congested, use these flags to help with program deployment.
Example Usage:
$solana program deploy ./target/deploy/hello_world.so \--with-compute-unit-price 10000 \--max-sign-attempts 10 \--use-rpc
Options explained:
--with-compute-unit-price
: Set priority fee in micro-lamports (0.000001 SOL) per compute unit. Check Helius Priority Fee API for current rates. A priority fee is an additional fee that paid to the current leader to prioritize your transaction.--max-sign-attempts
: Number of times to retry with a new blockhash if transactions expire. (Default: 5)--use-rpc
: Send transactions to the configured RPC. This flag requires a stake-weighted RPC connection from providers such as Triton or Helius.
Is this page helpful?