Rust: linker cc not found when compiling
Rust compiles to object files and hands them to a linker. If cc is missing, cargo fails at the last step: install a C toolchain or pick another linker.

Introduction & Quick Summary
Compilation gets through every crate and fails at the very last step:
error: linker `cc` not found
|
= note: No such file or directory (os error 2)
error: could not compile `myapp` (bin "myapp") due to 1 previous errorerror: linking with `cc` failed: exit status: 127
= note: collect2: fatal error: cannot find 'ld'cc is the system C compiler, and rustc uses it as the linker for the x86_64-unknown-linux-gnu target. rustc compiles Rust to object files itself, then hands them to the platform linker together with the C runtime and system libraries. When no cc exists on the path, the build fails after all the Rust code has already been compiled successfully.
Where it appears:
- A slim or Alpine container with only
rustupinstalled and no build tools. - A CI image that installs Rust but not
build-essential,gcc, ormusl-dev. - A cross-compilation target whose linker is not installed or not named in config.
- A machine where
ccexists for one user but not in the service'sPATH. - A build script that needs a C compiler for a
-syscrate, failing earlier but with similar wording.
Quick answer: install a C toolchain for the target, or point rustc at a linker you already have such as rust-lld. Then confirm the build script crates compile too.
Core Mechanics, Signatures & Reproducible Setup
rustc resolves a linker in this order: -C linker on the command line, target.<triple>.linker in .cargo/config.toml, the CARGO_TARGET_<TRIPLE>_LINKER environment variable, then the target default, which is cc for the GNU Linux targets. It invokes that program with object files and library flags.
# a container that reproduces the failure
docker run --rm -it rust:1.90-slim bash -lc '
rustc --version;
command -v cc || echo "no cc";
cargo new demo && cd demo && cargo build;
'rustc 1.90.0
no cc
Compiling demo v0.1.0 (/demo)
error: linker `cc` not foundThe same crate with the toolchain installed:
FROM rust:1.90-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends build-essential pkg-config \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY . .
RUN cargo build --releaseLinking-adjacent errors and what each actually means:
| Error | Meaning | Fix direction |
|---|---|---|
linker 'cc' not found | the program is not on PATH | install or configure a linker |
exit status: 127 | the linker command did not run | same, often a missing ld |
undefined reference to 'X' | linker ran, a symbol is missing | add the library or a -sys crate |
cannot find -lssl | linker ran, library path is missing | install the dev package |
error: linking with 'cc' failed | generic wrapper once the linker ran | read the note under it |
note: collect2: fatal error | gcc found but binutils missing | install binutils |
Key Comparison Table
| Target | Default linker | Package that provides it |
|---|---|---|
x86_64-unknown-linux-gnu | cc (gcc or clang) | build-essential on Debian, gcc on Fedora |
aarch64-unknown-linux-gnu | aarch64-linux-gnu-gcc | gcc-aarch64-linux-gnu |
x86_64-unknown-linux-musl | musl-gcc or rust-lld | musl-tools |
x86_64-pc-windows-msvc | link.exe | Visual Studio Build Tools with the C++ workload |
x86_64-apple-darwin | cc (clang) | Xcode command line tools |
wasm32-unknown-unknown | rust-lld | nothing extra |
Linker choices for a Linux build host:
| Linker | Install | Trade-off |
|---|---|---|
GNU ld via cc | build-essential | most compatible, slowest link |
clang plus lld | clang lld | faster links, needs matching runtime flags |
rust-lld | ships with the toolchain | no extra package, good for CI and cross builds |
mold | separate package | fastest for large binaries, extra dependency |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. Install the smallest toolchain that links
gcc alone is not always enough: the C runtime headers and binutils are separate packages on minimal images.
# Debian and Ubuntu
apt-get update && apt-get install -y --no-install-recommends build-essential
# Alpine
apk add --no-cache build-base
# Fedora and RHEL
dnf groupinstall -y "Development Tools"command -v cc gcc ld
cc --version | head -1If cc reports a version but linking still fails with collect2, install binutils explicitly; some slim images include the compiler without the linker driver.
2. Use rust-lld when you cannot install a toolchain
rust-lld ships with the Rust toolchain and needs no system package. Point the target at it in the project's cargo config.
# .cargo/config.toml
[target.x86_64-unknown-linux-gnu]
linker = "rust-lld"cargo build --releaseThis works because rust-lld understands the ELF object format rustc produced. It does not remove the need for a C library at run time, so a -sys crate that links libssl still needs libssl-dev. Rust 1.90 changed the default linker for some targets to rust-lld, which is why a project pinned to an older toolchain and a project on 1.90 can behave differently on the same machine.
3. Configure a linker per target
A cross target needs its own linker, and the setting belongs in config rather than in a shell alias so CI and local builds agree.
# .cargo/config.toml
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"rustup target add aarch64-unknown-linux-gnu
apt-get install -y gcc-aarch64-linux-gnu
cargo build --release --target aarch64-unknown-linux-gnu# the environment variable wins over config when you need to override once
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=clang cargo build --target aarch64-unknown-linux-gnuCheck the chosen linker with cargo build -v; the first line of the rustc invocation shows -C linker=....
4. Build static binaries with musl
A statically linked binary removes the runtime C library dependency, but the musl target still needs a linker and static libraries.
apt-get install -y musl-tools musl-dev
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 pie executable, statically linkedA -sys crate that compiles C code needs a musl-targeted C compiler; the usual fix is CC_x86_64_unknown_linux_musl=musl-gcc, checked with the environment tool before the build to confirm the value actually reaches cargo.
5. Separate a build-script failure from a link failure
A cc-based crate compiled by cc-rs fails much earlier than the final link, and the message names the crate rather than the binary.
error: failed to run custom build command for `openssl-sys v0.9.104`
--- stderr
thread 'main' panicked at build/main.rs:120:
Could not find directory of OpenSSL installationapt-get install -y pkg-config libssl-dev
cargo clean -p openssl-sys
cargo buildpkg-config is the piece most often missing: the headers exist, but the build script cannot discover them. If the message mentions cc itself, the compiler is missing; if it mentions a library path, the development package is.
Common Compilation/Runtime Mistakes & Version Gotchas
Reading the second error line as the cause. linking with 'cc' failed is emitted after the linker ran and returned non-zero. The note under it names the real problem, such as a missing library or an undefined symbol.
Setting linker with a path and no quotes in config. TOML accepts a path without quotes only when it has no spaces; a Windows path with backslashes needs either forward slashes or escaped backslashes. A malformed config is ignored with a warning, and the build falls back to cc.
Installing the compiler but not the runtime. gcc without libc6-dev produces a compiler that cannot find Scrt1.o. Install the meta package (build-essential, build-base) rather than individual tools.
Assuming rust-analyzer or cargo check needs the linker. cargo check does not link, so a project can pass check and fail build. Run cargo build in CI, not only check, or the missing linker is discovered at release time.
Overriding CC for the wrong crate. CC affects C code compiled by cc-rs; the Rust link step uses the target linker. A build that needs both needs both settings, and mixing them produces confusing partial fixes.
Version differences worth knowing. Rust 1.90 moved several Linux targets to rust-lld by default, so images that relied on cc may now build without it while older toolchains still fail. -Z flags for alternate linkers are nightly-only; on stable, use the -C linker form or a config entry. Cross builds for musl changed their recommended flags across releases, so pin the toolchain in rust-toolchain.toml when a build must be reproducible.
Verifying the result. After installing tools, run cargo build --release from a clean target directory and compare the dependency tree with Diff if a lockfile changed. Copy the released binary into a plain scratch or alpine image and start it: a binary that needs a shared library at runtime fails there even though the build succeeded.
Reference material: Cargo configuration and rustc command-line arguments.
Official references
primary documentationThe Rust documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Rust work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Rust guides
All Rust guidesRust E0282: type annotations needed
E0282 means inference ran out of information. Annotate the binding, add a turbofish to collect or parse, or fix the signature the value flows into.
Rust E0463: can't find crate for std
The target triple has no standard library installed. Add the target with rustup, check the target string and link the right ABI.
Rust E0432: unresolved import
E0432 is a module graph problem: a missing mod declaration, a private parent or a wrong path. Fix the tree, not the dependency list.
Rust E0106: missing lifetime specifier
E0106 appears when a returned borrow has no input to borrow from. Name the lifetime in functions, structs and impls, or return owned data.
More in Rust
All Rust guidesThe guides filed next to this one in Rust, in library order.