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
-
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 noneskips installing a toolchain — the project’srust-toolchain.tomlwill dictate which one to use. -
Drop a
rust-toolchain.tomlat 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 installingrust-docs(a few hundred MiB you do not need on a build server).components = [...]is the explicit list — anything not listed is excluded. -
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 -
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 -
Keep rustup itself current. Updates are user-level, no apt:
rustup self update rustup update
Operational notes
~/.cargoholds 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 withcargo cache --autocleanor by mounting it as an ephemeral volume.- The
stable,beta,nightlychannels float — every six weeksstablemoves 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. rustupandcargoare not the same binary.rustup updateupgrades toolchains;cargo updateupgrades dependencies inCargo.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-gnuprovides one, butcross(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 warningsover 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.