Sub topic 4
Fixing a missing linker or PATH problem
This chapter assumes you are comfortable in a terminal and know what a compiler and a package manager do. It also assumes you have already run the rustup installer from the previous chapter. Two problems account for most failed first installs: Cargo's bin directory is not on your PATH, and the system has no linker. Each has a distinct symptom, so you can tell them apart quickly.
How the two failures differ
A PATH problem stops you before Rust runs at all. A linker problem appears only when Rust tries to produce a program. The table shows what you will see.
| Symptom | Cause | Fix |
|---|---|---|
| `command not found: rustc` or `cargo` | `~/.cargo/bin` is not on `PATH` | Open a new terminal or load the Cargo environment file |
| `linker 'cc' not found` | No C compiler or linker installed | Install your platform's build tools |
| `link.exe not found` (Windows) | Visual Studio C++ build tools missing | Install the MSVC build tools |
Why a linker is needed
The Rust compiler, rustc, turns your code into object files. A linker then joins those object files, and any libraries they use, into one executable. Rust does not ship its own linker for most targets. It calls the one already on your system, usually cc on Linux and macOS and link.exe on Windows.
The installer places the tools in ~/.cargo/bin. It also edits your shell startup files so that directory is on PATH. A shell reads those files only when it starts, so a terminal that was already open does not see the change.
The diagram shows how to decide which fix applies.
Fixing a PATH problem
First, try the simplest fix. Close the terminal and open a new one, then run the version check again.
rustc --versionIf the command is still not found, check that the binaries exist and which directories are on your PATH.
ls ~/.cargo/bin
echo "$PATH"If ls lists cargo, rustc, and rustup, the install succeeded and only PATH is wrong. The installer writes an environment file that adds the directory. Load it in the current shell:
. "$HOME/.cargo/env"
rustc --versionTo make the change permanent, make sure your shell startup file loads that same file. For bash or zsh, append the line to ~/.bashrc or ~/.zshrc:
echo '. "$HOME/.cargo/env"' >> ~/.bashrcOn Windows, the installer adds %USERPROFILE%\.cargo\bin to your user PATH. Open a new Command Prompt or PowerShell window after installing. If it is still missing, add that directory in the system environment variable settings.
Fixing a missing linker
The error usually looks like this:
error: linker `cc` not found
|
= note: No such file or directory (os error 2)Install the build tools for your platform:
- Debian or Ubuntu:
sudo apt install build-essential - Fedora:
sudo dnf groupinstall "Development Tools" - macOS:
xcode-select --install - Windows: install the "Desktop development with C++" workload from the Visual Studio Build Tools installer. The rustup installer offers to do this for you.
After installing, build again. You do not need to reinstall Rust.
Confirm the fix
Create a throwaway project and build it. A successful build proves both that cargo is found and that linking works.
cargo new link_test
cd link_test
cargo runYou should see Hello, world! printed. If so, both problems are resolved.
Exercise
You run rustc --version right after installing and get "command not found". ls ~/.cargo/bin lists rustc, cargo, and rustup. Is this a PATH problem or a linker problem, and what do you run to fix it in the current terminal?
Show answer
It is a PATH problem. The tools are installed, but the open shell has not picked up ~/.cargo/bin. Run . "$HOME/.cargo/env" or open a new terminal. A linker problem would only appear later, as an error during cargo build.