TL;DR Quick Fix
Install gcc (or your distro's C toolchain). Cargo needs a system linker to produce native binaries — cc is just a symlink to gcc or clang, and on a fresh system neither exists.
Ubuntu / Debian
sudo apt update
sudo apt install build-essential
Fedora / RHEL / CentOS
sudo dnf groupinstall "Development Tools"
Arch Linux
sudo pacman -S base-devel
Alpine Linux (Docker)
apk add --no-cache gcc musl-dev
Re-run cargo build. Done.
What's Actually Happening
Rust doesn't do its own linking. It compiles your code to object files (.o), then hands them off to the system linker — which Cargo invokes via cc. On Linux, cc is usually a symlink to gcc or clang, whichever is installed.
Fresh servers, CI runners, and minimal Docker images strip out everything non-essential. rustup installs Rust just fine, but the moment Cargo tries to link, it looks for cc on your PATH and finds nothing.
The full error looks like this:
error: linker `cc` not found
|
= note: No such file or directory (os error 2)
error: could not compile `your-crate` (bin "your-crate") due to 1 previous error
Fix Approaches
Option 1: Install the full build toolchain (recommended)
On Ubuntu/Debian, build-essential pulls in gcc, make, and the C standard library headers in one shot. It covers everything Rust projects typically need, on both dev machines and production build environments.
# Ubuntu/Debian
sudo apt update && sudo apt install -y build-essential
# Verify cc is now available
which cc
# /usr/bin/cc
Option 2: Install only gcc (minimal environments)
Trimming down a Docker layer? Install just gcc directly:
# Debian slim image
apt-get install -y gcc
# Alpine
apk add gcc musl-dev
Alpine uses musl instead of glibc, so you need musl-dev too — that's where the C standard library headers live. Don't skip it or you'll get a different linker error downstream.
Option 3: Use the musl target for fully static binaries
Want a binary that runs on any Linux box with zero shared library dependencies? The musl target produces fully static executables — perfect for scratch or distroless containers.
# Add the musl target
rustup target add x86_64-unknown-linux-musl
# Install musl toolchain (Ubuntu/Debian)
sudo apt install musl-tools
# Build
cargo build --release --target x86_64-unknown-linux-musl
The output lands in target/x86_64-unknown-linux-musl/release/. Copy it into an Alpine or scratch container and it runs without any libc on the host.
Option 4: Tell Cargo to use clang instead of gcc
Have clang installed but not gcc? Point Cargo at it via .cargo/config.toml:
# .cargo/config.toml (project-level)
[target.x86_64-unknown-linux-gnu]
linker = "clang"
Or skip the config file and set it per-build:
CC=clang cargo build
Dockerfile Example (Before and After)
This is probably the most common place people hit this error. The official rust:slim image drops gcc to cut image size — but that also means it can't link anything.
# BROKEN: rust:slim doesn't have gcc
FROM rust:1.78-slim
WORKDIR /app
COPY . .
RUN cargo build --release # fails: linker 'cc' not found
Quick fix — install gcc before the build step:
FROM rust:1.78-slim
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
libc6-dev \
&& rm -rf /var/lib/apt/lists/*
COPY . .
RUN cargo build --release
Better yet, use a multi-stage build. Compile in the fat rust:latest image, then copy just the binary into a slim runtime:
FROM rust:1.78 AS builder
WORKDIR /app
COPY . .
RUN cargo build --release
FROM debian:bookworm-slim
COPY --from=builder /app/target/release/your-binary /usr/local/bin/
CMD ["your-binary"]
The final image ends up around 80–100 MB instead of 1+ GB. No gcc in production, no unnecessary attack surface.
Verification
Three quick checks to confirm the fix worked:
# 1. Confirm cc exists
which cc && cc --version
# 2. Build your project
cargo build
# 3. Or a release build
cargo build --release
Clean output looks like this:
Compiling your-crate v0.1.0 (/app)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 3.42s
No linker errors means you're good.
Quick Reference by Distro
- Ubuntu/Debian:
sudo apt install build-essential - Fedora:
sudo dnf groupinstall "Development Tools" - CentOS/RHEL 8+:
sudo dnf install gcc - Arch:
sudo pacman -S base-devel - Alpine:
apk add gcc musl-dev - Void Linux:
sudo xbps-install base-devel

