This guide will help you set up your development environment for building and testing Solana programs using Rust and Anchor.
First, install Rust, the programming language used for writing Solana programs.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
rustc --version
Next, install the Solana CLI tools. These tools are essential for interacting with the Solana blockchain.
sh -c "$(curl -sSfL https://release.anza.xyz/stable/install)"
export PATH="$HOME/.local/share/solana/install/active_release/bin:$PATH"
solana --version
Update Solana to the latest version:
agave-install update
For more information, check the Solana installation guide.
To test Solana locally, you can use the Solana test validator. Note that this doesn't work in Windows mounted folders.
In a new terminal, run:
cd ~
solana-test-validator
Then, configure Solana to use the localhost testnet:
solana address
solana account <address from above>
solana config set --url localhost
solana-test-validator
Generate a new keypair and configure Solana:
solana-keygen new
solana address
solana config set -ud
solana airdrop 2
solana balance
Anchor is a framework for Solana's Sealevel runtime providing several developer tools.
Install Anchor CLI:
cargo install --git https://github.com/coral-xyz/anchor avm --locked --force
avm install latest
Install missing dependencies:
sudo apt-get update && sudo apt-get upgrade && sudo apt-get install -y pkg-config build-essential libudev-dev libssl-dev
For more information, check the Anchor installation guide.
Initialize a new Anchor project:
anchor init solana-app --javascript
Build the project:
anchor build
Run tests:
anchor test
To skip the local validator during tests:
anchor test --skip-local-validator
Generate a new keypair for your program:
solana-keygen new -o ./target/deploy/solana_app-keypair.json
solana address -k ./target/deploy/solana_app-keypair.json
Replace the program ID in your project:
In mysolanaapp/src/lib.rs
:
declare_id!("your-program-id");
In Anchor.toml
:
[programs.localnet]
mysolanaapp = "your-program-id"
Clean the project:
cargo clean
Remove the target directory if needed.
- To copy the idl file to the frontend, use:
node ./copy-idl.js
- There are no read operations in Solana programs. To read the contents of a program, request the account, and you can view all of the program's state.