Rust Installation and Setup

How to install Rust on any operating system, verify your toolchain, write your first Rust program, and start using Cargo to build projects

Installing Rust is not just about getting a compiler on your machine. It is about setting up a complete toolchain that stays up to date, lets you switch between release channels, and ships with a build system that you will use every day. The official installer, rustup, handles all of this. Whether you are on Windows, macOS, or Linux, the steps are similar in spirit and the verification process is identical.

After installation you will meet two new programs: rustc, the Rust compiler, and Cargo, the package manager and build tool. This page covers everything from downloading rustup to running your first Cargo project, including a small example that fetches a dependency from the public crate registry.

Downloading and Installing Rust

rustup is a command-line tool that installs the Rust compiler (rustc), the Cargo build system, and the standard library for your platform. It also manages toolchain updates and allows you to install beta or nightly builds later. The installation method differs slightly depending on your operating system, but the outcome is the same.

What is rustup?:

rustup is the recommended way to install Rust. Package managers like apt or brew often carry Rust packages as well, but those may be outdated or lack toolchain management. rustup keeps everything in your home directory and makes uninstalling as easy as running rustup self uninstall.

On Unix-like systems (including Windows Subsystem for Linux) the entire setup runs in the terminal.

1

Download and run the installer

Paste the following command into a terminal. The script downloads rustup, checks its signature, and starts the interactive setup.

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

When asked, choose the default installation (press Enter). The installer places everything inside ~/.rustup and ~/.cargo.

2

Configure the PATH environment variable

The installer attempts to add ~/.cargo/bin to your shell’s PATH automatically. If your terminal still cannot find rustc after installation, you can source the environment file manually.

source "$HOME/.cargo/env"

To make the change permanent, add this line to your .bashrc, .zshrc, or equivalent shell config file.

3

Verify that everything works

Run the following two commands. Each should print a version number, confirming that the tools are accessible.

rustc --version
cargo --version

Command not found after installation?:

If rustc --version fails with "command not found" on any platform, the PATH variable was not updated successfully. Reopen your terminal, manually source the environment file, or check that ~/.cargo/bin (Unix) or %USERPROFILE%\.cargo\bin (Windows) is in your system PATH. This is the most common installation hiccup.

Everything is working:

If both version commands print a version number, Rust is installed correctly. You now have rustc, cargo, and rustup available globally.

Keeping Rust Up to Date

Rust releases a new stable version every six weeks. rustup makes updating straightforward:

rustup update

This command fetches the latest stable toolchain, along with any associated components like the standard library and documentation. Your existing projects will continue to compile without changes; Rust’s stability guarantee means that code that compiled on a previous stable release will still compile on the latest one.

Uninstalling Rust

If you ever need to remove Rust, a single command undoes everything rustup installed:

rustup self uninstall

This removes the toolchains, Cargo, and the rustup binary itself, leaving no trace behind.

Hello, World! and Hello, Cargo!

With the toolchain ready, it is time to write Rust code. Beginners often ask whether they should learn to use the raw compiler (rustc) or jump straight into Cargo. The answer is both, briefly. Writing a single file with rustc demystifies what Cargo does under the hood. Then you immediately switch to Cargo, because real Rust projects never consist of a single file that you compile by hand.

A Manual Hello, World!

Create a file called hello.rs and open it in any text editor. Type the following code:

hello.rs
fn main() {
    println!("Hello, world!");
}

Now compile it with the Rust compiler:

rustc hello.rs

On Windows this produces hello.exe; on Unix-like systems it produces an executable named hello. Run it from the terminal:

./hello   # on Unix
hello.exe # on Windows

The program prints Hello, world! to the terminal. That is the entire manual workflow: write a .rs file, call rustc, and execute the binary. No build configuration, no package manager — just a compiler. It works perfectly for learning, but as soon as you need a second file or an external library, manual rustc invocations become unsustainable.

A Hello, Cargo! Project

Cargo automates compilation, dependency management, testing, and packaging. To start using it, create a new project:

1

Generate a new Cargo project

In a terminal, navigate to the directory where you want to keep your Rust projects and run:

cargo new hello_cargo

Cargo creates a folder named hello_cargo with this structure:

hello_cargo/
├── Cargo.toml
└── src
    └── main.rs

Cargo.toml is the project manifest where you declare metadata and dependencies. src/main.rs is the entry point, already populated with the same "Hello, world!" program.

2

Build the project

Move into the project directory and run the build command:

cd hello_cargo
cargo build

Cargo downloads the standard library (if not already cached), compiles your code, and places the resulting binary in target/debug/hello_cargo. The debug directory indicates an unoptimized development build.

3

Run the project

Instead of finding the binary manually, use the cargo run command:

cargo run

Cargo will rebuild only if the source changed, then execute the binary. The terminal shows Hello, world! just as before, but now you did not touch a single compiler flag.

Build vs. run vs. check:

  • cargo build compiles your code once.
  • cargo run compiles and runs in one step, great for quick iteration.
  • cargo check (fast) verifies that the code compiles without producing an executable. Use it while writing code to catch errors without waiting for a full build.

Adding a Dependency with Cargo

A project that only prints "Hello, world!" does not showcase Cargo’s real value. The crate ecosystem is where Rust projects come alive. Crates are Rust packages, and they are hosted on crates.io. To demonstrate, add a small crate that renders a friendly message with Ferris, the unofficial Rust mascot.

First, open Cargo.toml and add the ferris-says dependency under the [dependencies] section. You can also use the cargo add command to do this automatically:

cargo add ferris-says

Your Cargo.toml now looks similar to this:

Cargo.toml
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]
ferris-says = "0.3.1"

Now replace the contents of src/main.rs with the following code:

src/main.rs
use ferris_says::say;
use std::io::{stdout, BufWriter};
fn main() {
    let stdout = stdout();
    let message = String::from("Hello fellow Rustaceans!");
    let width = message.chars().count();
    let mut writer = BufWriter::new(stdout.lock());
    say(&message, width, &mut writer).unwrap();
}

Run the project again:

cargo run

If everything is set up correctly, the terminal displays:

 __________________________
< Hello fellow Rustaceans! >
 --------------------------
        \
         \
            _~^~^~_
        \) /  o o  \ (/
          '_   -   _'
          / '-----' \

A few things happened behind the scenes that are worth understanding. Cargo fetched the ferris-says crate from crates.io, along with any of its own dependencies. It recorded the exact versions in a Cargo.lock file, so that anyone who clones this project later will get the same dependency tree. The use ferris_says::say; line imports the say function, and use std::io::{stdout, BufWriter}; brings in standard library types for output buffering. The unwrap() at the end signals that if printing fails, the program should panic — a deliberate choice for a tiny example.

Do not delete Cargo.lock:

Beginners sometimes delete Cargo.lock to clean up, believing it is a temporary file. It is not. The lock file guarantees reproducible builds across different machines. Commit it to version control for applications. For libraries, Cargo ignores it automatically when published.

You have a working Rust toolchain:

If Ferris appeared in your terminal, you have Rust installed, Cargo configured, and a dependency successfully pulled from the ecosystem. This is the full modern Rust workflow in miniature.