instalacion-rust-toolchain

Rust Toolchain: Install Rustup, Cargo and Rustc Easily

  • 5 min

The Rust toolchain is the set of tools we need to compile, run and manage Rust projects.

We already know what Rust is and why it’s so cool. Now it’s time to get our hands dirty and install these tools on our computer.

Unlike other languages where you download an installer and “cross your fingers,” in Rust, tool management is a delight thanks to Rustup.

In this article, we’ll see how to install Rust on Windows, Linux and macOS, and we’ll understand exactly what we are installing.

What are we going to install?

Before copying and pasting commands, let’s understand the three fundamental pieces we are going to install:

  1. Rustc: This is the compiler. It takes your source code and transforms it into an executable binary. (To be honest, we’ll rarely call it directly, but it’s the engine of everything).
  2. Cargo: This is the package and build manager. It’s Rust’s wonderful tool. With Cargo we download libraries, compile projects, run tests and generate documentation.
  3. Rustup: This is the installer and version manager. Rust evolves quickly (there is a new version every 6 weeks). Rustup allows us to update the compiler or switch between versions (stable, beta, nightly) with a single command.

We will mainly interact with Cargo for day-to-day tasks and with Rustup to keep the environment updated.

Installation on Windows

On Windows, the installation has a small prerequisite that is usually the cause of 90% of beginners’ problems: C++ build tools.

Prerequisites: Microsoft C++ Build Tools

The MSVC Rust toolchain needs a linker to create .exe files. By default, it uses Microsoft Visual Studio tools.

If you don’t have Visual Studio installed, you need to download the Microsoft C++ Build Tools.

  1. Download the installer from the official Microsoft website.
  2. During installation, make sure to check the “Desktop development with C++” option.
  3. Let it finish and, just in case, restart your computer.

If you skip this step, Rust will install, but when you try to compile you’ll get a “Linker not found” error.

The rustup-init installer

Once you have the Build Tools:

  1. Go to the official page rust-lang.org/tools/install.
  2. Download the rustup-init.exe file.
  3. Run it. A terminal will open.
  4. It will ask you what to do. Simply press 1 and Enter to proceed with the default installation.

Installation on Linux and macOS

On Unix-based systems, the installation is incredibly simple and is done through the terminal with an official script.

Open your terminal and run:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Copied!

This script will download and start rustup. It will show you information about the installation and ask for confirmation. As on Windows, option 1 (Proceed with installation (default)) is generally the correct one.

If you are a macOS user and use Homebrew, you could install it with brew install rust. However, for this course it is preferable to use the curl command above. Using rustup is the “canonical” way and will save you from problems with outdated versions in the future.

Configure the PATH

Once the installation finishes, you will see a message like: “Rust is installed now. Great!”.

For the terminal to recognize the cargo and rustc commands, it may be necessary to reload the environment variables. You can close and reopen the terminal, or on Linux and macOS, run the command suggested by the installer:

source "$HOME/.cargo/env"
Copied!

Verifying the installation

Regardless of your operating system, let’s check everything is in order. Open a terminal (or PowerShell on Windows) and run:

rustc --version
Copied!

You should see something similar to: rustc 1.xx.x (hash date)

And let’s also verify Cargo:

cargo --version
Copied!

If both commands return a version, congratulations! You have the base environment ready.

Keeping Rust Updated

As we said, Rust is updated frequently. Thanks to rustup, updating is as easy as typing:

rustup update
Copied!

And if one day you decide Rust is not for you (we hope that’s not the case) or you want to completely clean your installation:

rustup self uninstall
Copied!

Our first contact with Cargo

Although we will do the official “Hello World” in a future article explaining it line by line, let’s test that Cargo works correctly by creating an empty project.

Navigate to the folder where you keep your projects and run:

cargo new prueba_instalacion
Copied!

This will create a folder called prueba_instalacion. Enter it:

cd prueba_instalacion
Copied!

And now, the real test. To compile and run the default program, just type:

cargo run
Copied!

If you see “Hello, world!” in the console, your toolchain works perfectly. Cargo has downloaded dependencies (if any), invoked rustc and executed the resulting binary.

With the foundations laid, in the next article we will configure our editor. We’ll leave Notepad for emergencies and turn VS Code into a powerful IDE for Rust that helps us program and debug.