Sub topic 4
Fixing a missing linker or PATH problem
This chapter assumes you have run the rustup installer, or tried to, and can use a terminal and edit shell configuration files. It covers the two most common ways a fresh Rust install fails. In the first, cargo cannot be found. In the second, cargo build stops because the system has no linker.
Two different failures
The two problems look alike, since both happen right after install and both stop you from running a first program. They have different causes and different fixes.
- A PATH problem means your shell cannot find
rustc,cargo, orrustup. The tools are installed, but the shell does not know where to look. - A linker problem means the tools run, but compiling a program fails at its last step. Rust needs a system linker, and your machine does not have one.
Telling them apart is easy. If cargo --version fails, you have a PATH problem. If cargo --version works but cargo build or cargo run fails with a message about a linker, you have a linker problem.
Fixing a PATH problem
What the installer does
By default, rustup places the tools in a bin directory inside Cargo's home directory. That is ~/.cargo/bin on Linux and macOS, and %USERPROFILE%\.cargo\bin on Windows. The files rustc, cargo, rustup, rustfmt, and cargo-clippy in that directory are small proxies. Each proxy forwards to the right binary in the active toolchain.
The installer then tries to add that directory to your PATH. On Linux and macOS it edits shell profile files, such as ~/.profile, ~/.bashrc, or ~/.zshenv, so they source ~/.cargo/env. That file prepends the directory to PATH. A profile change only takes effect in shells started afterward. The terminal you used to run the installer does not see it.
Symptoms
The error text depends on your shell:
| Shell | Typical message |
|---|---|
| bash | `bash: cargo: command not found` |
| zsh | `zsh: command not found: cargo` |
| Windows Command Prompt | `'cargo' is not recognized as an internal or external command` |
| PowerShell | `cargo : The term 'cargo' is not recognized...` |
Diagnose it
First, check whether the tools exist on disk at all:
ls ~/.cargo/binIf the directory lists cargo, rustc, and rustup, the install worked and only your PATH is wrong. If the directory does not exist, the install did not finish. Rerun the installer and read its output for errors.
Next, check whether the directory is on your PATH:
echo "$PATH" | tr ':' '\n' | grep -n cargo
command -v cargoThe first command prints each PATH entry on its own line and keeps the ones containing cargo. The second prints the full path of the cargo your shell would run, or nothing if it finds none.
Fix it
The quickest fix applies to the current terminal only. Load the environment file the installer wrote:
. "$HOME/.cargo/env"
cargo --versionIf that works, the install is fine and only the current terminal was stale. Opening a new terminal usually repeats the same fix, because the profile edit is read at startup.
If a new terminal still cannot find cargo, the installer did not edit the profile file your shell actually reads. Add the line yourself. For bash or zsh:
echo '. "$HOME/.cargo/env"' >> ~/.bashrcReplace ~/.bashrc with ~/.zshrc if you use zsh. Fish users should add source "$HOME/.cargo/env.fish" to ~/.config/fish/config.fish instead.
On Windows, open "Edit the system environment variables", choose Environment Variables, and make sure the user Path contains %USERPROFILE%\.cargo\bin. Then open a new terminal, because existing terminals keep the old value. You can confirm with where cargo.
A different `cargo` is being found
Sometimes cargo is found, but it is the wrong one. A distribution package, Homebrew, or an older install can place its own cargo or rustc earlier on PATH. List every match:
type -a cargo rustc
rustup showtype -a prints each match in PATH order, and the first is the one that runs. If it is not under ~/.cargo/bin, either remove the other package or move ~/.cargo/bin earlier in PATH. Mixing a system rustc with rustup's cargo causes confusing version mismatches, so keep only one source.
Fixing a missing linker
Why Rust needs one
The compiler rustc turns your code into object files. A linker is a separate program that joins those files, plus the standard library and any system libraries, into one executable. Rust does not ship a linker for most targets. It calls one provided by your operating system's C toolchain. That is why a C toolchain matters for a language that is not C.
If no linker is available, even cargo run on the generated hello world fails at the final step.
Linux and macOS
On Linux and macOS, rustc runs the C compiler driver cc to do the linking. When it is missing, the error looks like this:
error: linker `cc` not found
|
= note: No such file or directory (os error 2)Confirm that cc is missing, then install a toolchain for your system:
cc --version| System | Install command |
|---|---|
| Debian, Ubuntu | `sudo apt install build-essential` |
| Fedora | `sudo dnf install gcc` |
| Arch Linux | `sudo pacman -S base-devel` |
| macOS | `xcode-select --install` |
On macOS the command installs Apple's command line developer tools, which include clang and the linker. A dialog asks you to confirm the download. When it finishes, cc --version should print a version string.
Windows
The default Windows toolchain targets MSVC, Microsoft's compiler ecosystem, and uses its linker, link.exe. The error is usually:
error: linker `link.exe` not found
|
= note: program not foundTo fix it, install Visual Studio Build Tools and select the "Desktop development with C++" workload. It includes the MSVC linker and the Windows SDK libraries. You do not need the full Visual Studio IDE. Open a new terminal afterward so the change is picked up. Rust finds the tools on its own, so you do not need to use a special "Developer Command Prompt".
An alternative is the GNU toolchain, which uses MinGW tools instead of MSVC:
rustup toolchain install stable-x86_64-pc-windows-gnu
rustup default stable-x86_64-pc-windows-gnuThis route requires a MinGW-w64 installation, so it moves the problem rather than removing it. The MSVC toolchain is the better choice for most Windows work, because many crates and tools assume it.
Confirm the fix
Rebuild a project to check that linking now succeeds:
cargo new linker_check
cd linker_check
cargo runSeeing Hello, world! means the compiler and linker both work.
Linker found, but linking still fails
If a linker error names a library, such as unable to find library -lssl, the toolchain is present but a system library is not. This usually appears later, when a dependency wraps a C library. Install the library's development package, for example libssl-dev on Debian and Ubuntu, and build again.
You can also point Cargo at a specific linker. Create .cargo/config.toml in your project or your home directory:
[target.x86_64-unknown-linux-gnu]
linker = "clang"The target triple must match your machine. rustc -vV prints it on the host: line. Only do this if you have a reason, such as preferring clang or a faster linker. The default setup needs no configuration.
Reading error output efficiently
Linker errors can be long, with many lines of linker flags. The useful part is usually the line starting error: and the note: right after it. A "not found" note names the missing program. A "cannot find" note names a missing library. Fix the first named item and rebuild before reading the rest.
Quick reference
| Symptom | Likely cause | Fix |
|---|---|---|
| `cargo: command not found` | `~/.cargo/bin` not on `PATH` | Run `. "$HOME/.cargo/env"` or open a new terminal |
| New terminals still fail | Profile file not edited | Add the `env` line to `~/.bashrc` or `~/.zshrc` |
| `cargo --version` shows an old version | Another `cargo` earlier on `PATH` | Use `type -a cargo`, remove or reorder |
| ``linker `cc` not found`` | No C toolchain on Linux or macOS | Install build tools for your system |
| ``linker `link.exe` not found`` | No MSVC build tools on Windows | Install Build Tools with the C++ workload |
| `cannot find -lname` | Missing system library | Install the library's development package |
Exercise
You install Rust on Ubuntu. In the same terminal, cargo --version prints "command not found", but ls ~/.cargo/bin lists cargo. What is wrong, and what is the fastest fix?
Show answer
The tools are installed, but the current shell's PATH does not include ~/.cargo/bin, because the profile edit only applies to shells started afterward. Run . "$HOME/.cargo/env" or open a new terminal. If new terminals also fail, add that line to the profile file your shell reads, such as ~/.bashrc.
Exercise
cargo run fails with `error: linker cc not found on a fresh Debian system, although cargo --version` works. Which command fixes it, and how do you confirm the fix?
Show answer
Run sudo apt install build-essential to install the C toolchain that provides cc. Confirm with cc --version, then run cargo run again in the project. A successful build that prints "Hello, world!" shows that compiling and linking both work.