Skip to main content

Install Anchor and scaffold your first program

Scaffold your first Solana program with Anchor and see what changes when you move from sending instructions to writing the code that receives them.
Install Anchor and scaffold your first program background
Challenge

Install Anchor and scaffold your first program

Arc theme: Your First Anchor Program

Web2 bridge: Programs are like serverless functions deployed to a shared runtime, and you test them just like you test any backend service

The Scenario

Close out of every Token-2022 script in your editor. For seven weeks you have been the one calling programs: minting, transferring, freezing, harvesting. The System Program signed off your transfers. The Token-2022 program enforced your fees. You were the client.

That ends today. Web2 made you fluent in deploying serverless functions to someone else’s runtime: write a handler, push it up, hit the endpoint, watch the logs. A Solana program is the same shape. It is code that lives at an address, gets invoked by clients, runs in a shared runtime, and writes results back to a database (the accounts model you already know). The runtime is the Solana Virtual Machine instead of AWS Lambda, and the language is Rust instead of TypeScript, but the mental model carries over cleanly.

Anchor is the framework that makes that translation comfortable. It hides the byte-level account serialization, generates a typed client for you, and gives you a project layout that feels closer to a standard Rust workspace than to raw Solana boilerplate. Today you install it and let it scaffold your first program. You will not write a single line of business logic. You will earn the right to do that tomorrow by getting the toolchain green first.

The Challenge

What you’ll need

  • A working Solana CLI (you have used it for every previous day of this challenge)
  • A working Rust toolchain with cargo on your PATH
  • Node.js 18+ and either npm or yarn, because the scaffold ships a TypeScript test harness
  • A terminal and your editor of choice
  • About 10 to 15 minutes of patience for the first cargo install compile

Steps

  1. Confirm the prerequisites are present. If any of these print an error, fix that one first before continuing.
solana --version
rustc --version
cargo --version
node --version

  1. Install the Anchor Version Manager (AVM). AVM is to Anchor what nvm is to Node: it lets you install multiple Anchor CLI versions side by side and switch between them per project. You always want this instead of installing a single fixed Anchor binary, because real Solana projects pin specific Anchor versions in Anchor.toml.
cargo install --git https://github.com/solana-foundation/anchor avm --force

  1. Use AVM to install the latest stable Anchor CLI and select it as your active version.
avm install latest
avm use latest
anchor --version

You should see a version line like anchor-cli 1.0.x. The exact patch number does not matter for this challenge, only that the command answers.

  1. Move into a parent folder where you keep your Solana work (not inside any existing project), then scaffold a brand new Anchor project.
anchor init counter
cd counter

This creates a fresh directory with a complete, buildable Anchor workspace inside it.

Note: If the anchor command is not found or does not work right after installing, close your terminal/CLI completely and open a new one, then try again. A fresh session picks up the updated PATH.

  1. Take a tour of what just appeared. Open the project in your editor and look at each of these:
    • Anchor.toml: the project configuration. Notice the [programs.localnet] section, which maps your program’s name to a public key. That key is the on-chain address your program will deploy to.
    • Cargo.toml at the workspace root: a standard Rust workspace file that lists programs/* as members.
    • programs/counter/src/lib.rs: the actual program. Pay attention to three things:
      • declare_id!(...): the same address you saw in Anchor.toml, baked into the binary.
      • #[program]: the module that contains every instruction handler. Anchor expands this macro into the dispatcher that routes incoming transactions to your functions.
      • pub fn initialize(_ctx: Context<Initialize>) -> Result<()>: a single no-op instruction. The Context wraps the accounts the instruction receives.
    • programs/counter/tests/test_initialize.rs: a scaffolded Rust integration test that loads your compiled program into LiteSVM and calls the no-op initialize instruction. You will not run it today, and tomorrow you will replace it with your own test.
    • package.json: the JavaScript dependencies for Anchor’s client tooling.
  2. Compile the scaffolded program. This is the moment of truth for your toolchain.

Run it

anchor build

The first build takes a while because Anchor pulls down and compiles the Solana program SDK. Subsequent builds are seconds. When it finishes, you should see a fresh target/ directory containing a .so file (the compiled program) and an idl/ folder containing a JSON file (the Interface Definition Language description of your program). Both are the artifacts you will use for the rest of this arc.

What Just Happened

You crossed the line from program user to program author. The cargo install step put a version manager on your machine, AVM then put the actual Anchor CLI on top of it, and anchor init generated a complete project that already compiles. That last point matters: if you tried to assemble a Solana program from scratch you would spend hours wiring up the SBF target, the entrypoint macro, the account discriminators, and the IDL emitter. Anchor did all of that silently behind one command, the same way create-next-app or rails new hides the boilerplate behind a sensible default.

Look back at lib.rs with fresh eyes. The #[program] module is the equivalent of a router file in a backend framework. The initialize function is your first route handler. The Context<Initialize> argument is the typed request object, except instead of carrying a JSON body it carries a list of accounts (the database rows the handler is allowed to read or write). The arc you are starting now is about turning that empty handler into something that actually changes state.

The .so file Anchor produced is a Solana BPF binary. It is the artifact you would upload to the chain if you ran anchor deploy today. You will not deploy yet, because the program does nothing interesting, but the build succeeding means your environment is ready for everything that follows in this arc.

Resources

Submission

Submit a screenshot below of your terminal showing anchor --version followed by a successful anchor build on your freshly scaffolded project.

Submit your project