Skip to content

Installing the Rust toolchain with rustup

Install rustup, pin the Rust version per project with rust-toolchain.toml, and add the targets and components a real build pipeline needs.

rustup is the canonical Rust installer because Rust ships a stable + beta + nightly trio and many projects need both stable for production and nightly for specific features. rustup also manages cross-compilation targets and extra components like clippy and rust-analyzer. This article installs rustup on Ubuntu, pins the toolchain per project, and adds the bits a real build needs beyond the default.

How to verify

Check the current Rust install:

rustup --version 2>/dev/null
rustc --version
cargo --version
rustup show
rustup component list --installed
rustup target list --installed

If Ubuntu’s apt install rustc is what is on the host, the rustc version is at least a year old. rustup is the only path that gives current Rust without fighting the distro.

What’s happening

rustup installs into ~/.rustup (a global toolchain store) and ~/.cargo (binaries and the build cache). rustup itself is a tiny binary that dispatches to whichever toolchain the directory or override file selects. Toolchains live as ~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/ and contain the rustc binary, the standard library for the host target, cargo, and optional components.

Pinning is done with a rust-toolchain.toml at the repo root. When you run cargo in that directory, rustup reads the file, ensures the requested toolchain is installed (downloading it transparently if not), and dispatches to it. Anyone who clones the repo gets the right Rust without thinking about it.

Targets are about cross-compilation. The default install includes the host target only; rustup target add aarch64-unknown-linux-musl adds the standard library for ARM64 musl-libc, which is what you need to produce static binaries that run on Alpine containers.

The procedure

  1. Install rustup as the deploy user (not root):

    sudo -iu deploy
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain none
    source "$HOME/.cargo/env"
    rustup --version

    --default-toolchain none skips installing a toolchain — the project’s rust-toolchain.toml will dictate which one to use.

  2. Drop a rust-toolchain.toml at the project root:

    [toolchain]
    channel = "1.83.0"
    components = ["rustfmt", "clippy", "rust-analyzer"]
    targets = ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu"]
    profile = "minimal"

    profile = "minimal" skips installing rust-docs (a few hundred MiB you do not need on a build server). components = [...] is the explicit list — anything not listed is excluded.

  3. Verify the toolchain auto-installs on the first cargo invocation:

    cd /var/www/app
    cargo --version
    # rustup downloads 1.83.0 + the listed components + listed targets
    rustup show
  4. For cross-compilation to musl static binaries (Alpine, distroless), add the target explicitly if not in targets:

    rustup target add x86_64-unknown-linux-musl
    cargo build --release --target x86_64-unknown-linux-musl
    file target/x86_64-unknown-linux-musl/release/myapp
    # ELF 64-bit LSB executable, x86-64, ... statically linked
  5. Keep rustup itself current. Updates are user-level, no apt:

    rustup self update
    rustup update

Operational notes

  • ~/.cargo holds the build cache and the binaries of installed cargo subcommands (cargo install cargo-edit). On CI hosts that build many crates, this directory grows large — bound it with cargo cache --autoclean or by mounting it as an ephemeral volume.
  • The stable, beta, nightly channels float — every six weeks stable moves forward. Pinning to "1.83.0" (or "stable-2024-12-01") makes the build deterministic. Don’t pin to "stable" on production CI unless you accept the churn.
  • rustup and cargo are not the same binary. rustup update upgrades toolchains; cargo update upgrades dependencies in Cargo.lock. Confusing the two is the most common newcomer mistake.
  • For aarch64-unknown-linux-musl, the linker may not be installed by apt — apt install gcc-aarch64-linux-gnu provides one, but cross (a wrapper that runs the build in a Docker container with the right tooling) is the smoother path for CI.
  • After a major rustc version bump, run cargo clippy --all-targets --all-features -- -D warnings over the repo. New compiler versions add new warnings, and unaddressed warnings become noise that masks real issues.

For deploying the resulting binaries, see Cargo build and deploy. Stack Harbor’s managed operations practice keeps Rust toolchains pinned per project in rust-toolchain.toml and updates them on our schedule — the speed and binary size benefits of Rust come with the obligation to keep the toolchain current, not stale.