Sub topic 2
Installing Rust with rustup
This chapter covers installing the Rust toolchain with rustup. It assumes you are comfortable running shell commands, know what a compiler and a package manager do, and have a Linux, macOS, or Windows machine with internet access. The chapter covers what the installer does, what it changes on your system, and the problems that most often break a first install. It is about 1,500 words long.
What the installer sets up
Rust is distributed through rustup, a toolchain manager. Rustup does not compile code. It downloads and manages the tools that do. A default install gives you three pieces:
- rustup installs, updates, and switches between versions of Rust.
- rustc is the Rust compiler. It turns source files into binaries or libraries.
- Cargo is the build tool and package manager. You call it for almost everything: creating projects, building, testing, and fetching dependencies.
You rarely call rustc directly. Cargo calls it for you with the right flags. Rustup sits in front of both: the rustc and cargo commands on your PATH are small proxy programs that rustup provides. When you run one, the proxy picks the active toolchain and forwards the call. That is why you can switch Rust versions without reinstalling anything.
The default install also adds the standard library, local documentation, and a few components such as Clippy and rustfmt. Later chapters use those two.
Where files go
Rustup keeps everything in your home directory, so it does not need administrator rights. Two directories matter.
| Directory | Default location | Contents |
|---|---|---|
| Cargo home | `~/.cargo` (Windows: `%USERPROFILE%\.cargo`) | The `bin` folder with the proxies, plus the download cache for crates |
| Rustup home | `~/.rustup` (Windows: `%USERPROFILE%\.rustup`) | The installed toolchains and rustup's settings |
You can move them before installing by setting the CARGO_HOME and RUSTUP_HOME environment variables. Most people keep the defaults.
Because the install is per user, each account on a shared machine has its own toolchain. A system package manager such as apt can also install Rust, but those packages usually lag behind the stable release and do not give you rustup's toolchain switching. For development, rustup is the recommended route.
Installing on Linux and macOS
Run the official installer script:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shThe flags matter. --proto '=https' and --tlsv1.2 make curl refuse anything other than HTTPS with TLS 1.2 or newer. -sSf keeps the output quiet, still shows errors, and fails on HTTP errors instead of piping an error page into the shell.
Piping a downloaded script into sh runs code you have not read. If you want to inspect it first, download it to a file, read it, then run it:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o rustup-init.sh
less rustup-init.sh
sh rustup-init.shThe script prints the planned changes and offers three choices:
- Proceed with the standard installation. This is the default and right for most people.
- Customize the installation. You can change the default toolchain, the profile, and whether to edit your shell configuration.
- Cancel.
Press Enter to accept option 1. The standard install uses the stable release channel and the default profile.
What the installer changes in your shell
The installer adds ~/.cargo/bin to your PATH by editing your shell's startup files. Recent versions of rustup also create an env file at ~/.cargo/env that the startup files load. The change only applies to new shells. In the terminal where you ran the installer, load it manually:
. "$HOME/.cargo/env"Alternatively, open a new terminal window.
Installing on Windows
On Windows, download and run rustup-init.exe from the official Rust website (rust-lang.org, "Install" page). It opens a console window with the same three options as the Linux script. Choose the standard installation.
Windows has one extra requirement. Rust's default Windows target is x86_64-pc-windows-msvc (on 64-bit Intel and AMD machines), which links with Microsoft's linker. The installer checks for it and, if needed, points you to the Visual Studio C++ Build Tools. Install the "Desktop development with C++" workload, or the build tools with the Windows SDK, before you continue. Without them, your first build fails at the link step.
You can also work inside WSL (Windows Subsystem for Linux). Inside WSL, follow the Linux steps. Rust installed in WSL and Rust installed on Windows are separate installs.
Installing from a non-interactive script
For scripts, containers, and CI, you can skip the prompts. Pass -y to the script through sh -s --:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -yYou can add options after --. For example, --default-toolchain stable names the channel, and --profile minimal installs only rustc, rust-std, and cargo. The minimal profile saves time and disk space on CI machines, and you can add components later with rustup component add.
Checking that the install worked
Open a new terminal, or load the env file as shown above, and run:
rustc --version
cargo --version
rustup --versionEach command prints a version string. The exact numbers will differ from machine to machine, and a typical rustc line looks like rustc 1.xx.x (hash date). If all three print a version, the toolchain is installed and on your PATH.
To see which toolchain is active and where it lives, run:
rustup showYou should see the default host triple (for example x86_64-unknown-linux-gnu), the installed toolchains, and the active one, normally stable.
Fixing a missing linker
Compiling Rust ends with a link step, where object files are joined into one executable. Rust does not ship its own linker for most targets. It uses the system's C toolchain. If none is installed, a build fails with an error mentioning linker 'cc' not found or link.exe not found.
The fix depends on the platform:
| Platform | Cause | Fix |
|---|---|---|
| macOS | Command line tools are missing | Run `xcode-select --install` |
| Debian or Ubuntu | No C compiler | Run `sudo apt install build-essential` |
| Fedora | No C compiler | Run `sudo dnf groupinstall "Development Tools"` (or `sudo dnf install gcc`) |
| Windows (MSVC) | Visual Studio C++ Build Tools missing | Install the build tools with the C++ workload |
After you install the linker, rerun the build. You do not need to reinstall Rust.
Some crates also need C libraries or a C compiler of their own, for example to build bindings to OpenSSL. If a later build fails inside a dependency, the error usually names the missing library, and the fix is a system package, not a change to Rust.
Fixing a PATH problem
The other common failure is command not found: cargo (or 'cargo' is not recognized on Windows). The tools are installed, but your shell cannot find ~/.cargo/bin. Work through these checks in order:
- Open a new terminal. PATH changes apply only to shells that start after the install.
- Look for the files. Run
ls ~/.cargo/bin. You should seecargo,rustc,rustup, and other proxies. If the directory is missing, the install did not finish, so rerun the installer. - Check your PATH. Run
echo $PATHand look for.cargo/bin. - Load the env file with
. "$HOME/.cargo/env". If that works, your shell startup file does not source it. Add that line to the file your shell reads, such as~/.bashrcor~/.zshrc. Which file applies depends on whether the shell is a login shell, so if one does not take effect, try the other. - Check for a shell you did not expect. The installer edits files for the shells it finds. If you use fish or another shell, add
~/.cargo/binto its PATH by hand.
If an old rustc from a system package appears before rustup's in your PATH, which rustc shows the wrong location. Either remove the system package or put ~/.cargo/bin earlier in PATH.
Behind a proxy or firewall
If the download fails, check whether your network needs a proxy. Rustup and curl honor the standard HTTPS_PROXY and HTTP_PROXY environment variables. Set them before running the installer:
export HTTPS_PROXY="http://proxy.example.com:8080"
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shReplace the address with your own proxy. Cargo reads the same variables later when it downloads crates.
Uninstalling
Rustup can remove itself and every toolchain it installed:
rustup self uninstallThis deletes ~/.cargo and ~/.rustup, so first check that nothing there holds files you want to keep. Your projects live elsewhere and are untouched.
Exercise
You installed Rust with rustup, and in the same terminal window cargo --version prints "command not found". ls ~/.cargo/bin lists cargo, rustc, and rustup. What is the most likely cause, and what are two ways to fix it?
Show answer
The install succeeded, but the terminal was already open and never loaded the PATH change, so it cannot find ~/.cargo/bin. Fix it by opening a new terminal, or by running . "$HOME/.cargo/env" in the current one. If new terminals also fail, add that line to your shell startup file such as ~/.bashrc or ~/.zshrc.
Exercise
A first cargo build on a fresh Ubuntu machine fails with "linker cc not found". Why does Rust need cc, and what single command fixes it?
Show answer
Rust compiles your code to object files and relies on the system's C toolchain to link them into an executable. Ubuntu has no C compiler by default on a bare install. Running sudo apt install build-essential installs one, and the build then works without reinstalling Rust.
Exercise
Why might you choose --profile minimal when installing Rust on a CI machine, and how would you add Clippy later?
Show answer
The minimal profile installs only rustc, the standard library, and Cargo, so the download is smaller and the setup is faster. When a job needs Clippy, run rustup component add clippy.