Solana and Anchor Setup Guide: A Comprehensive Walkthrough
Published Feb 2022

Setting up a development environment for building and testing Solana programs using Rust and Anchor can seem daunting, but with this guide, you’ll have a complete roadmap to follow. Solana is a high-performance blockchain platform, and Anchor is a powerful framework that makes developing on Solana faster and more secure. This guide will help you install the necessary tools, set up your environment, and get started with writing your first Solana program using Anchor.
Prerequisites
Before diving into the installation process, ensure you have the following:
A Linux-based OS (Ubuntu, Fedora, etc.). Windows users will need WSL (Windows Subsystem for Linux) or a Linux VM.
A terminal with administrative privileges for installing dependencies.
An internet connection to download necessary tools.
Step 1: Install Rust
Rust is the programming language used to write Solana programs. Here’s how to install it:
**Install Rust: **Run the following command to install Rust via rustup, which is the official installer:
codecurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
**Check Rust Installation: **Verify that Rust is installed by checking its version:
rustc --version
If the installation was successful, you’ll see a version number.
Step 2: Install Solana CLI
The Solana CLI is essential for interacting with the Solana blockchain. Follow these steps to install it:
**Install Solana CLI Tools: **Run the following command to install the Solana CLI:
sh -c "$(curl -sSfL https://release.anza.xyz/stable/install)"
**Set Path for Solana: **Add the Solana installation directory to your PATH by running:
export PATH="$HOME/.local/share/solana/install/active_release/bin:$PATH"
**Verify Solana Installation: **Check the Solana version: If you see a version number, Solana is correctly installed.
solana --version
Step 3: Update Solana CLI
To ensure you’re using the latest version of the Solana CLI tools, update them by running:
agave-install update
For further information about Solana installation and troubleshooting, check the Solana installation guide.
Step 4: Test Solana Locally
To test Solana locally, use the Solana test validator, which runs a local version of the Solana blockchain on your machine.
**Start Solana Test Validator: **In a new terminal window, run the following:
cd ~
solana-test-validator
**Configure Solana to Use the Local Testnet: **Set Solana to use the local testnet:
solana address
solana account <address-from-above>
solana config set --url localhost
**Start the Test Validator Again: **Run:
solana-test-validator
Step 5: Setup Solana with Keypairs and Airdrop
Now that Solana is running, you need to configure it for use:
**Generate a New Keypair: **Run the following to generate a new Solana keypair:
solana-keygen new
**Check the Address: **After generating the keypair, check the Solana address:
solana address
**Set Configuration and Airdrop SOL: **
Configure your local Solana environment:
solana config set -ud
Request some test SOL tokens to your account:
solana airdrop 2
Check your balance:
solana balance
Step 6: Install Anchor
Anchor is a framework built to simplify the development of Solana programs. It provides a variety of tools to streamline your development process.
**Install Anchor CLI: **Use the following command to install Anchor via cargo:
cargo install --git https://github.com/coral-xyz/anchor avm --locked --force
**Install Latest Anchor Version: **Once the CLI is installed, run the following to install the latest version:
avm install latest
**Install Dependencies: **For Ubuntu, make sure the required dependencies are installed:For more details, refer to the Anchor installation guide.
sudo apt-get update && sudo apt-get upgrade && sudo apt-get install -y pkg-config build-essential libudev-dev libssl-dev
Step 7: Create a New Project with Anchor
Anchor simplifies Solana program development by providing boilerplate code, seamless interactions, and support for testing. To create a new project, use:
anchor init solana-app --javascript
This creates a new directory named **solana-app **with all necessary files for your Anchor project.
Step 8: Build and Test the Project
**Build the Project: **To compile the program, run:
anchor build
**Run Tests: **To run tests for your project:
anchor test
**Skip Local Validator During Tests: **If you wish to skip using the local validator during tests, run:
anchor test --skip-local-validator
Step 9: Create a Dynamically Generated Program ID
When developing Solana programs, you often need to generate a unique program ID for deployment.
**Generate a Keypair for the Program: **Use the following command to generate a keypair for the program:
solana address -k ./target/deploy/solana_app-keypair.json
**Get Program Address: **Get the program address with:
solana address -k ./target/deploy/solana_app-keypair.json
**Update the Program ID in Your Project: **In your program’s Rust file (src/lib.rs), update the program ID:
declare_id!("your-program-id");
In the Anchor.toml file, specify the program ID:
[programs.localnet]
mysolanaapp = "your-program-id"
Step 10: Clean Up the Project
If you need to clean up your project after development:
**Clean the Project: **Run the following command to remove unnecessary build files:
cargo clean
**Remove Target Directory: **Optionally, remove the target directory entirely:
rm -rf target
Important Notes
**Copy the IDL File: **To copy the Interface Definition Language (IDL) file to your frontend, run:
You can use this script to copy the idl file:
// copy-idl.js
const fs = require("fs");
const idl = require("./target/idl/solana_app.json");
fs.writeFileSync("./app/src/app/utils/idl.json", JSON.stringify(idl));
Run the script with command:
node ./copy-idl.js
**No Read Operations in Solana Programs: **Solana programs don’t support read operations. To read the state, request the account associated with the program and view its contents.
Conclusion
By following this guide, you’ve successfully set up a Solana development environment with Anchor. You now have a local Solana test validator running, and you can start building, testing, and deploying your Solana programs using Rust and Anchor.
For the complete code used in this guide, you can refer to the Solana App repository on GitHub.
Happy coding!