Sub topic 2
Installing Rust with rustup
This chapter covers installing the Rust toolchain with rustup on Linux, macOS, and Windows. It assumes you can run shell commands and know what a compiler and a package manager do. It also assumes your computer can reach the internet.
What the installer sets up
rustup is the official installer and toolchain manager for Rust. When you run it, it downloads and installs three things:
rustc, the compiler that turns Rust source files into programs.cargo, the build tool and package manager that most Rust projects use.- The standard library and documentation for your platform.
By default rustup installs the stable toolchain, which is the release channel most people use. It places the tools under ~/.cargo/bin (on Windows, %USERPROFILE%\.cargo\bin) and keeps the toolchain files under ~/.rustup. It installs for your user only, so it does not need administrator rights.
The files in ~/.cargo/bin are small proxies. When you run rustc or cargo, the proxy forwards the call to the active toolchain. This is how rustup can switch toolchains later without changing your commands.
Installing on Linux and macOS
Open a terminal and run the official installer script:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shThe flags make curl accept only HTTPS and TLS 1.2 or newer, fail on HTTP errors, and stay quiet except for errors. The script is piped to sh, so it is worth reading it first if you prefer. You can open https://sh.rustup.rs in a browser to do that.
The installer prints a summary and offers three choices:
- Proceed with the standard installation. This is the default and the right choice for most people.
- Customize the installation, for example to pick a different default toolchain or skip the PATH change.
- Cancel the installation.
Press Enter to accept the default. When it finishes, the installer asks you to load the new environment into your current shell, or to open a new terminal. A new terminal is the simplest option.
Installing on Windows
On Windows, download rustup-init.exe from the official Rust website (https://rustup.rs) and run it. Rust on Windows needs a linker, which is a program that joins compiled pieces into one executable. The default target uses the MSVC linker from the Visual Studio C++ Build Tools. If those are missing, the installer points you to them and can guide you through installing them. Accept the default installation once the build tools are in place.
Windows Subsystem for Linux (WSL) is another option. Inside a WSL terminal, follow the Linux steps above.
What the installer changes
The installer modifies your shell profile files so that ~/.cargo/bin is on your PATH. The PATH is the list of directories your shell searches when you type a command. Depending on your shell, the installer edits files such as ~/.profile, ~/.bashrc, or ~/.zshenv. It also writes a small script, ~/.cargo/env, that adds the directory to PATH when loaded.
Because shell profiles are read only when a shell starts, a terminal that was already open does not see the change. To use Rust right away in that terminal, load the script:
. "$HOME/.cargo/env"Common mistakes
- Using the existing terminal. If
rustcis "command not found" right after installing, you probably need a new terminal or theenvscript above. - Installing through a system package manager as well. A distribution's
rustpackage can sit earlier onPATHand hide therustupversion. Pick one source. - Missing C toolchain on Linux. Rust needs a linker, and Linux systems usually provide one through a C compiler package such as
build-essentialon Debian and Ubuntu. Later chapters cover diagnosing this. - Running the installer with
sudo. The installer is meant for a normal user account. Usingsudocan leave files in your home directory owned by root.
Uninstalling
If you need to remove everything, rustup can uninstall itself and all toolchains:
rustup self uninstallThis removes the ~/.cargo and ~/.rustup directories, so back up anything you keep there first.
Exercise
After the installer finishes, you type rustc --version in the same terminal and see "command not found". What are two ways to fix it?
Show answer
Open a new terminal, or load the environment script with . "$HOME/.cargo/env". Either one puts ~/.cargo/bin on the PATH of that shell. The installer only edited your profile files, and an already open shell does not re-read them.
Exercise
Why are the files in ~/.cargo/bin called proxies, and what does that let rustup do?
Show answer
Each file forwards your command, such as cargo or rustc, to the currently active toolchain. This lets rustup switch between toolchains and channels without you changing the commands you type.