Sphere
Log in Join

Fixing `error: linker 'cc' not found` on first cargo build after installing Rust with rustup

I've installed Rust on a handful of machines and never thought much about it until a new Ubuntu 22.04 CI box refused to build even a generated cargo new project. I'd used the --minimal profile because it was faster and I figured I didn't need docs. Turns out that profile skips the system linker, and rustc needs one even for pure-Rust hello world. The whole thing cost me a morning because the error reads like a compiler bug and isn't.

## The exact error

Here is what cargo build printed, copied straight from the terminal:

$ cargo new scratch
Created binary (application) scratch package
$ cd scratch
$ cargo build
Compiling scratch v0.1.0 (/home/ci/scratch)
error: linker cc not found
|
= note: No such file or directory (os error 2)

error: aborting due to 1 previous error

No amount of re-running rustup update changed anything. That was my first wrong turn.

## What actually went wrong

rustup installs the toolchain: rustc, cargo, std. It does not install a C linker. When rustc gets to the final stage it calls out to cc to link the object files, and on a minimal Ubuntu install there simply is no cc. Pure Rust code still goes through this step, so "I'm not using any C" doesn't save you. The --minimal profile I'd chosen is documented as skipping rust-docs, rustfmt and clippy, but the more relevant detail is that a minimal base image has no build-essential either. Two things missing, one error message that only mentions the second.

I confirmed it the dumb way:

$ which cc
$ echo $?
1
$ rustup show
Default host: x86_64-unknown-linux-gnu
stable-x86_64-unknown-linux-gnu (default)

Toolchain was fine. Linker was not.

One more thing that confused me: rustup component list showed everything as installed, which made me trust the toolchain even more. Of course it did. Components are Rust components, not OS packages. There's no entry in that list for cc because rustup has nothing to do with it. I'd have saved time by running cargo build -v early, which prints the exact linker invocation and would have shown the failing cc command right there instead of one cryptic line.

## The fix

Install the compiler toolchain at the OS level, not through rustup:

sudo apt update
sudo apt install -y build-essential
$ cc --version
cc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0

Then the same cargo build completes. If you can't install packages system-wide, point Cargo at a linker you do have using a config file:

mkdir -p .cargo
cat > .cargo/config.toml <<'EOF'
[target.x86_64-unknown-linux-gnu]
linker = "clang"
EOF

That works if clang is present, but build-essential is the honest fix on an Ubuntu box you control.

## The part that tripped me up

CI had cached the rustup toolchain from an earlier job, so rustc --version reported 1.78.0 and everything looked installed. The cache hid the fact that the OS layer underneath had been rebuilt from a slimmer base image. Our Dockerfile had dropped build-essential three weeks earlier in an unrelated size-cleanup commit, and nobody noticed because the cached layer still had the compiler until it didn't.

If you're seeing this in CI rather than on your laptop, check the base image before you check Rust. If you know a cleaner way to make the error mention the missing package, I'm all ears.