Skip to main content

Overview

The anchor init command creates a new Anchor workspace with all necessary files and directories, including program templates, test configurations, and package management setup.

Command Syntax

anchor init <name> [OPTIONS]

Arguments

name
string
required
Workspace name. Must be a valid Rust identifier (no reserved words, cannot start with a digit).

Options

--javascript
flag
default:"false"
Use JavaScript instead of TypeScript for test files
--no-install
flag
default:"false"
Don’t install JavaScript dependencies after initialization
--package-manager
enum
default:"yarn"
Package manager to use for installing dependenciesOptions: yarn, npm, pnpm, bun
--no-git
flag
default:"false"
Don’t initialize a git repository
--template
enum
default:"multiple"
Rust program template to useOptions:
  • single: Program with a single lib.rs file (not recommended for production)
  • multiple: Program with multiple files for instructions, state, etc. (recommended)
--test-template
enum
default:"mocha"
Test template to useOptions: mocha, jest, rust, mollusk
--force
flag
default:"false"
Initialize even if there are files in the directory

Examples

Basic Initialization

Create a new workspace with default settings:
anchor init my_project
Output:
my_project initialized

Using JavaScript with npm

anchor init my_project --javascript --package-manager npm

Custom Template Configuration

Use a single-file program template with Jest tests:
anchor init my_project --template single --test-template jest

Skip Dependency Installation

anchor init my_project --no-install

Generated Structure

The command creates the following directory structure:
my_project/
├── .anchor/
├── app/
├── programs/
│   └── my_project/
│       ├── src/
│       │   ├── lib.rs
│       │   ├── constants.rs (multiple template)
│       │   ├── error.rs (multiple template)
│       │   ├── instructions.rs (multiple template)
│       │   ├── instructions/
│       │   │   └── initialize.rs (multiple template)
│       │   └── state.rs (multiple template)
│       └── Cargo.toml
├── tests/
│   └── my_project.ts
├── migrations/
│   └── deploy.ts
├── target/
│   └── deploy/
│       └── my_project-keypair.json
├── Anchor.toml
├── Cargo.toml
├── package.json
├── tsconfig.json
├── .gitignore
└── .prettierignore

Configuration Files

Anchor.toml

The workspace configuration file is created with:
  • Default cluster set to localnet
  • Program deployment configuration
  • Test script configuration
  • Toolchain settings (package manager)

Cargo.toml

Root workspace manifest with:
  • Workspace members pointing to programs/*
  • Release profile optimizations
  • Overflow checks enabled

Notes

The workspace name is converted to snake_case for Rust identifiers and kebab-case for directory names.
The command will fail if a workspace already exists in the current directory, unless --force is specified.
Use the multiple template (default) for better code organization and maintainability in production applications.

Build docs developers (and LLMs) love