Step-by-Step Guide: Setting Up Anchor on Windows for Solana Development
Table of contents
This article stems from my challenging experience installing Anchor on my Windows PC. The process took a full week to complete successfully. I'm grateful for the invaluable assistance provided by the Anchor Discord community, Claude AI, and the Twitter community, who helped me overcome the obstacles I encountered.
My aim in sharing this experience is to guide others who may face similar difficulties.
WSL Setup
For Windows users, the installation process involves two key steps. First, it's essential to set up the Windows Subsystem for Linux (WSL), which creates a Linux-compatible environment within your Windows operating system. This foundational step allows you to run Linux distributions directly on Windows, bridging the gap between the two operating systems.
To set up the Windows Subsystem for Linux (WSL) on your system, you'll need to execute a specific command. Open Windows PowerShell and enter the following instructions:
wsl --install
The install process will prompt you to create a default user account.
After completing the WSL installation, Ubuntu is typically set up as the default Linux distribution. To access the Linux environment, simply use the Windows Search feature and type "Ubuntu". This action will open a terminal window, providing you with a command-line interface to interact with your newly installed Linux subsystem.
The standard installation of WSL automatically includes Ubuntu as the default Linux distribution. To access your Linux environment, simply utilize the Windows Search function and type "Ubuntu". This action will launch a terminal window, granting you direct access to the Linux command-line interface within your Windows system.
For Visual Studio Code users, there's an integrated solution available. By installing the WSL extension in VS Code, you can seamlessly combine the functionality of the Windows Subsystem for Linux with your development environment. This integration allows for a smooth workflow between the Linux subsystem and your preferred code editor.
Launch Visual Studio Code and initiate a WSL terminal session within the integrated development environment. This allows you to access the Linux subsystem directly from your code editor interface.
Install Rust
With your environment set up, the subsequent phase in the process involves installing Rust, a systems programming language essential for the upcoming steps.
Solana's smart contracts, known as programs, are primarily developed using Rust, a powerful and efficient systems programming language. To begin working with Solana, you'll need to set up Rust on your system.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
After the installation process concludes successfully, it's necessary to update your system's PATH environment variable. This step ensures that your system can locate and utilize Cargo, Rust's package manager and build tool. To accomplish this, execute the following command in your terminal:
This action will refresh your PATH, incorporating Cargo's binary directory and allowing you to use Rust tools from any location in your terminal.
. "$HOME/.cargo/env"
To verify that the installation was successful, check the Rust version:
rustc --version
The next step is to install Solana CLI, by running the following commands
sh -c "$(curl -sSfL https://release.anza.xyz/stable/install)"
When installing the Solana CLI, you have the flexibility to choose specific versions or release channels. Instead of 'stable', you can specify a particular release tag (for example, 'v2.0.3') to install that exact version. Alternatively, you can opt for one of three symbolic channel names: 'stable' for the most recent stable release, 'beta' for preview releases, or 'edge' for the latest development build.
If you're setting up the Solana CLI for the first time on your system, you might encounter a notification. This message typically suggests adding a PATH environment variable, which is crucial for your system to locate and execute the Solana CLI tools from any directory in your terminal
//Close and reopen your terminal to apply the PATH changes or run the following in your existing shell:
export PATH="/Users/test/.local/share/solana/install/active_release/bin:$PATH"
To verify that the installation was successful, check the Solana CLI version:
solana --version
Install Anchor CLI
Anchor is a framework designed for building programs on the Solana blockchain. It uses Rust macros to streamline the development process, making it easier to write Solana programs.
You can install the Anchor CLI by running the following command
cargo install --git https://github.com/coral-xyz/anchor --tag v0.30.1 anchor-cli
To verify that the installation was successful, check the Ancor CLI version:
anchor --version
You should see output similar to the following:
anchor-cli 0.30.1
Proceed by executing the following command
avm use latest
If you get avm install
failures, it's likely due to the Rust v1.80 breaking the time
crate:
Quick Check
If you followed this tutorial, you likely installed Rust version 1.80.0, which introduced breaking changes to the time
crate.
Compiling time v0.3.29
error[E0282]: type annotations needed for `Box<_>`
--> /home/x/.cargo/registry/src/index.crates.io-6f17d22bba15001f/time-0.3.29/src/format_description/parse/mod.rs:83:9
|
83 | let items = format_items
| ^^^^^
...
86 | Ok(items.into())
| ---- type must be known at this point
|
help: consider giving `items` an explicit type, where the placeholders `_` are specified
|
83 | let items: Box<_> = format_items
| ++++++++
You can fix this in multiple ways:
Install from GitHub without the --locked
option
cargo install --git https://github.com/coral-xyz/anchor --tag v0.30.1 anchor-cli --force
Use a Rust version earlier than v1.80
rustup default 1.79.0 && avm install 0.30.1
This should resolve your initial issues. Then try and initiate a new anchor project by running the following command
anchor init <PROJECT_NAME>
Open the folder in your favorite code editor "VS CODE :)".
Run the following command to build your anchor project
anchor build
If you run anchor build
and encounter an error stating 'not a directory' similar to the following.
error: not a directory: '.../solana-release/bin/sdk/sbf/dependencies/platform-tools/rust/lib'
[2024-09-05T16:25:42.719543488Z ERROR cargo_build_sbf] Failed to install platform-tools: Unable to write "/root/.cache/solana/v1.41/platform-tools/tmp-platform-tools-linux-x86_64.tar.bz2": Custom { kind: Other, error: reqwest::Error { kind: Decode, source: TimedOut } }
This error suggests that the Solana CLI is having trouble downloading and installing the platform tools.
Try these solutions:
cargo build-sbf --force-tools-install
Clear the Solana cache
rm -rf ~/.cache/solana
Let's check if Solana is installed at all
solana --version
If this also returns "command not found", then Solana CLI is not installed on your system.
To install Solana CLI, you can use the following command
sh -c "$(curl -sSfL https://release.solana.com/v1.17.0/install)"
This will install the latest stable version of Solana. You can replace v1.17.0
it with the specific version you need if required.
After installation, you need to update your PATH. The installer should provide instructions, but typically you'll need to add something like this to your shell configuration file (e.g., ~/.bashrc
, ~/.bash_profile
, or ~/.zshrc
)
export PATH="/root/.local/share/solana/install/active_release/bin:$PATH"
After modifying your shell configuration file, either restart your terminal or run
source ~/.bashrc # or the appropriate file for your shell
Now, verify the installation:
solana --version
This should be able to solve all your problems "Hopefully :)". It solved mine by the way. Try running this command again
anchor build
This runs successfully, though it may take some time, so please be patient.
If you encounter any issues, feel free to reach out for support through the following channels:
Email Ofuzor Chukwuemeke at:
Join the official Anchor Discord channel for community support: https://discord.gg/PDeRXyVURd
Check the GitHub issues page for known problems and solutions: https://github.com/coral-xyz/anchor/issues
Don't hesitate to use these resources if you need assistance or want to stay updated on Anchor developments.
And you can also refer to the following articles:
Subscribe to my newsletter
Read articles from Ofuzor Chukwuemeke directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ofuzor Chukwuemeke
Ofuzor Chukwuemeke
I'm a full-stack Blockchain engineer