Hello and welcome to the final lesson in our setup module!
In our previous lessons, we meticulously configured your professional development environment. You installed the Rust toolchain using rustup within WSL2 and then set up the Solana CLI, connecting it to the devnet and creating a wallet. You are now equipped with the fundamental, low-level tools for blockchain interaction.
Today, we'll install and explore the Anchor framework. Anchor is a powerful layer of abstraction that sits on top of the tools you've already installed. Its primary purpose is to make Solana program development faster, safer, and more efficient. For someone with your extensive frontend development experience, you can think of Anchor as being analogous to a framework like React or Vue. While you could build a complex web app with vanilla JavaScript and direct DOM manipulation, a framework provides structure, handles boilerplate, and lets you focus on the application's unique logic. Anchor does the same for Solana programs.
By the end of this lesson, you will have installed the Anchor framework, initialized your first Anchor project, and confirmed that your entire end-to-end development environment is fully operational.
What is the Anchor Framework?
Before we install it, let's briefly touch on what problems Anchor solves. Writing "native" Solana programs involves a lot of manual, repetitive, and error-prone work, such as:
- Manually deserializing instruction data from a byte array.
- Manually checking that all required accounts are provided and have the correct permissions (e.g., is this account a signer? Is it writable?).
- Manually serializing state back into account data buffers.
Anchor abstracts away most of this complexity using a system of Rust attributes (macros) and conventions. It automatically handles data serialization/deserialization, provides a powerful constraint system for validating accounts, and generates a client-side library (in TypeScript) for easy interaction with your program. This lets you focus on your program's core business logic.
Installing Anchor with AVM
The recommended way to install Anchor is through the Anchor Version Manager (AVM). This is a command-line tool, similar to nvm for Node.js or rustup for Rust, that allows you to install and switch between different versions of the Anchor CLI. This is crucial for maintaining reproducible builds and working on projects that may depend on different Anchor versions.
We will use cargo, which you installed in our first lesson, to install avm.
The official Anchor documentation provides the most reliable instructions for installation. We will use it to install the Anchor Version Manager (AVM) and then Anchor itself.
In your WSL2 terminal, follow the steps in the 'Install Anchor CLI' section. Use the recommended method via 'Anchor Version Manager (AVM)'. Run the commands to install avm, then install and select the latest version of Anchor. Finally, verify the installation with anchor --version.
To summarize the commands you will be running from the documentation:
cargo install --git https://github.com/coral-xyz/anchor avm --locked --force: Installs AVM.avm install latest: Downloads and installs the latest stable version of the Anchor CLI.avm use latest: Sets the newly installed version as the one to use in your current shell.anchor --version: Verifies that the installation was successful.
For a visual guide of this process, the following video is helpful.
Installing All You Need for Solana Development [Solana Basics Tutorial] - Nov 16th '23
For a visual guide on the installation process inside your WSL terminal, you can watch this segment. The creator walks through the exact commands you're running and provides some useful commentary on the process.
Watch the 'Installing Anchor Framework' section from 18:29 to 20:20. This covers installing avm via cargo and then using avm to install Anchor. You'll see the compilation output, which can take a few minutes.
A quick note on dependencies: Anchor's default testing framework is written in TypeScript. Therefore, to run the tests that are automatically generated with a new project, you will need Node.js and Yarn installed in your WSL2 environment. Given your frontend background, you are likely very familiar with these tools. If you don't have them installed in WSL2 yet, using nvm to install Node.js is the recommended approach.
Initializing Your First Anchor Project
With Anchor installed, you can now create a new project. The anchor init command scaffolds a complete boilerplate project with all the necessary files and directories.
Let's create a project and explore its structure.
A Beginner's Guide to Building Solana Programs with Anchor
This Helius guide clearly explains the anchor init command and provides an excellent description of the boilerplate project structure that gets generated.
Read the section 'Creating a new Project with a Local Anchor Setup'. In your terminal, execute the anchor init my-first-anchor-project and cd my-first-anchor-project commands. Then, review the guide's breakdown of the generated file structure (app, programs, tests, Anchor.toml).
To get a better feel for the new project, let's open it in VS Code (code . in the project directory) and take a quick visual tour.
Set Up Your Solana Development Environment | Step-by-Step Beginner Tutorial
This clip provides a quick visual tour of the generated project structure inside VS Code, which is a great way to solidify your understanding of where everything goes.
Watch from 06:24 to 08:19. This gives a walkthrough of the key files like Anchor.toml (Anchor's configuration file) and the basic program code in programs/my-first-anchor-project/src/lib.rs.
Verifying the Setup: Build and Test
Creating the project is the first step; the final proof that your environment is fully configured is to successfully build and test the boilerplate program. This end-to-end test validates that Rust, the Solana CLI, and Anchor are all working together correctly.
Here are the key commands:
anchor build: This command compiles your Rust program into BPF bytecode, which is what actually gets deployed to the Solana blockchain. It also generates the IDL (Interface Description Language)—a JSON file that describes your program's instructions and accounts. This IDL is what allows your client-side code (e.g., in TypeScript) to easily call the program without manually constructing transactions.anchor test: This is the master command. It automatically starts a local test validator, builds the program, deploys it to the local validator, and then runs the integration tests located in thetests/directory using Mocha and Chai.
Before you run anchor test, remember that the tests are in TypeScript. You must first install the node-module dependencies. In your project's root directory, run:
yarn install
Now, let's see the whole process in action and prepare for potential hiccups.
Installing All You Need for Solana Development [Solana Basics Tutorial] - Nov 16th '23
This part of the Solandy video is invaluable. It first shows a successful anchor build, and then walks through fixing the exact kind of Node.js/TypeScript errors that are common in a WSL environment when running anchor test. Pay close attention to the troubleshooting process, especially the part about yarn install.
First, watch from 23:15 to 24:30 to see the anchor build command in action. Then, watch the troubleshooting section from 32:45 to 33:48. This is the crucial part where the presenter realizes they forgot to run yarn install and then successfully runs the tests. This is a very common oversight.
Now it's your turn. In your project directory, run the following commands in order:
anchor buildanchor test
You should see a flurry of output as the test validator starts, the program deploys, and finally, a confirmation that the tests passed. A successful test run is the final milestone of our environment setup!
Test your understanding!
You've just used anchor test, which is a high-level command that does several things at once. Based on what you've learned, what are the three main, distinct actions that anchor test orchestrates behind the scenes?
Show answer
- Starts a local test validator: It spins up a local Solana cluster (
solana-test-validator) to provide a sandboxed environment for testing. - Builds and deploys the program: It runs the equivalent of
anchor buildto compile the code and then deploys the resulting program to the local test validator. - Runs the test suite: It executes the TypeScript test files found in the
tests/directory against the deployed program on the local validator.
Conclusion
Congratulations! You have successfully completed the entire environment setup for professional Solana development. This is a significant achievement.
In this lesson, you have:
- Understood the role of the Anchor framework in simplifying Solana development.
- Installed the Anchor Version Manager (
avm) and the Anchor CLI. - Initialized a new boilerplate Anchor project and explored its structure.
- Successfully built the Rust program and ran the TypeScript integration tests, confirming your entire toolchain is working perfectly.
Your machine is now a complete Solana development studio. You are ready to move from setup to actual coding.
In our next module, "Rust Fundamentals for Solana," we will dive into the Rust programming language itself. We'll start with the essentials—variables, data types, and functions—building the foundation you need to understand and write the on-chain logic inside your Anchor programs.
Can't find a good explanation? Sign up and we'll make it for you
Sign up