Create your own
Lesson illustration

Solana Program Deployment: Local & Devnet

Hello! Let's get your first Solana program running on the blockchain.

In our previous lessons, we've carefully assembled all the individual pieces of a native Solana program. You've learned how to define an entrypoint, deserialize instruction data, validate accounts, and, most recently, serialize state back to an account's data buffer. Conceptually, you have everything you need to write a stateful program.

Today, we transition from theory to practice. This lesson is all about answering the question: "How do I take my Rust code and make it a live program on the Solana network?" We will cover the final, crucial steps in the development lifecycle to meet our learning outcome: to build and deploy a native Solana program to a local ledger or devnet.

This process is conceptually similar to a standard web development workflow: first, you have a build step that compiles your source code into an executable format, and then you have a deploy step that places that executable on a server. For us, the "server" is the decentralized Solana network.

By the end of this lesson, you will have compiled your Rust code and deployed it to both a private local network and the public devnet, marking a major milestone in your journey as a Solana developer.

The Two-Step Process: Build and Deploy

Getting a Solana program on-chain involves two main phases:

  1. Build: Compiling your Rust source code into a specific binary format that the Solana virtual machine can execute. This format is called Solana Bytecode Format (SBF).
  2. Deploy: Uploading the compiled binary to the Solana network. This action creates a new program account on the blockchain, and the address of this account becomes your unique Program ID.

Let's break down each step.

Step 1: Building Your Program

To build a Solana program, we use a special Cargo command that compiles the Rust code into an SBF-compatible shared object (.so) file.

The primary command for this is cargo build-sbf. You might also see cargo build-bpf in older tutorials; SBF is simply the new name for the eBPF-based backend, so build-sbf is the modern command to use.

For this command to work correctly, your Cargo.toml file needs a specific configuration to tell the compiler to produce a dynamic system library, which is what the .so file is.

[lib]
crate-type = ["cdylib", "lib"]

This configuration ensures that when you build, Cargo generates the deployable artifact.

Deploying Programs

The official Solana documentation, "Deploying Programs," clearly explains the build process and its outputs. This will give us a solid foundation for the commands we're about to run.

Please read the sections titled "Build Program" and "Build Output". Focus on two key takeaways: The command used is cargo build-sbf. The build process creates a target/deploy/ directory containing two critical files: the program executable (<your-program-name>.so) and a keypair (<your-program-name>-keypair.json).

As the documentation highlights, the two outputs are:

  • The .so file: This is your compiled program. It's the executable code that will live on the blockchain.
  • The -keypair.json file: This is an automatically generated keypair. Its public key serves as the default address for your program—the Program ID. The private key proves you have the authority to update the program later.

Step 2: Preparing the Deployment Environment

Before you can deploy, you need a Solana network (or "cluster") to deploy to, and you need some SOL in your wallet to pay for the deployment transaction fees and the rent for the program account.

We'll start with a local network, which is the fastest and easiest way to test.

Complete Guide to Full Stack Solana Development (2024)

The video "Complete Guide to Full Stack Solana Development" provides an excellent walkthrough of setting up a local development environment. We'll watch the first part to see how to configure the Solana CLI for local development, start a local validator, and fund our wallet.

Watch the segment from 02:35 to 04:20. Pay attention to these three commands: solana config set --url localhost: This tells your CLI to communicate with a local network. solana-test-validator: This command starts a full Solana blockchain on your computer. You'll need to run this in its own terminal window and keep it running. solana airdrop 1000: This funds your default wallet with play money (on local or devnet only) to cover transaction fees.

To summarize the setup for local deployment:

  1. Open a terminal and set your configuration to localhost:
    solana config set --url localhost
    
  2. Start the local validator. This must remain running in its own terminal window.
    solana-test-validator
    
  3. Open a new terminal window and airdrop yourself some SOL to pay for the deployment.
    solana airdrop 2 
    # Airdrop a couple of SOL to be safe. You can request more anytime on local.
    

With your local network running and your wallet funded, you're ready to deploy.

Step 3: Deploying Your Program

Now for the main event. We will take the .so file we built and upload it to our running local validator.

The command is straightforward: solana program deploy.

Solana Smart Contract Development: Hello World Tutorial | Rust and Typescript walkthrough

Let's watch a demonstration of the full build-and-deploy workflow for a native "Hello World" program. This video from Josh's DevBox shows how all the pieces fit together.

Watch the section "Deploying the Smart Contract to a Local Validator" from 37:04 to 41:14. The video uses an npm script (npm run build-program-rust) as a convenience wrapper, but notice that under the hood it's just doing the cargo build. The key command to focus on is solana program deploy <path_to_file>.so. Observe the output, which confirms the deployment and gives you the final Program ID.

After running solana program deploy ./target/deploy/your_program.so, the CLI will output the Program ID. This public key is the permanent on-chain address of your smart contract. This is the address that clients will use to send instructions to your program.

Deploying to Devnet

Deploying to a public network like Devnet is almost identical. Devnet is a publicly accessible cluster run by the Solana Foundation that mimics the behavior of Mainnet, making it the perfect place for final testing before a real launch.

The steps are:

  1. Configure the CLI to target devnet.
    solana config set --url devnet
    
  2. Fund your wallet. Unlike the local validator, you can't request infinite SOL on devnet. There's usually a limit of 1-2 SOL per day per IP address/wallet.
    solana airdrop 2
    
  3. Deploy the program. The command is exactly the same.
    solana program deploy ./target/deploy/your_program.so
    

The program is now live on a public network, and anyone in the world with the Program ID can interact with it.

Complete Guide to Full Stack Solana Development (2024)

Let's revisit the "Complete Guide to Full Stack Solana Development" to see the devnet deployment process. This will solidify your understanding of how easy it is to switch between environments.

Watch the clip from 46:54 to 48:45. This segment covers switching the CLI config to devnet, airdropping SOL, and running the deployment. The video uses Anchor, but the core solana config set and deployment concepts are universal.

Test your understanding!

You have just finished writing the first version of your Solana program. You want to test it on the public Devnet. Your program's Rust project is named my_program.

List the sequence of solana and cargo commands you would run from your terminal to get it live on Devnet for the first time. Assume your Solana CLI is currently configured for localhost.

Show answer

Here is the correct sequence of commands:

  1. cargo build-sbf

    • This compiles your Rust code and creates target/deploy/my_program.so.
  2. solana config set --url devnet

    • This switches your CLI's target from localhost to the public Devnet.
  3. solana airdrop 2

    • This requests devnet SOL to your wallet to pay for the deployment costs.
  4. solana program deploy ./target/deploy/my_program.so

    • This uploads your compiled program to Devnet and assigns it a Program ID.

Conclusion

Excellent work! You've successfully navigated the path from source code to a live, on-chain program. This is a fundamental skill for any Solana developer and a huge step forward in the course.

Let's recap the key takeaways:

  • The build/deploy workflow consists of two main stages: building the code into an .so file and deploying that file to a network.
  • The cargo build-sbf command is used to compile a Rust project into the SBF bytecode that Solana validators can execute.
  • The solana-test-validator command is an indispensable tool for running a private, local blockchain for rapid testing.
  • You can switch between networks (clusters) like localhost and devnet using solana config set --url <cluster_name>.
  • The solana program deploy command uploads your compiled program to the currently configured network, making it live.

You now have a program on-chain, but it's just sitting there. In our next lesson, we will learn how to interact with a deployed program using the Solana CLI. We'll send transactions that invoke its instructions and see our Rust logic execute on the network.

Can't find a good explanation? Sign up and we'll make it for you

Sign up