Web Analytics

Installing Rust

Beginner ~10 min read

Installing Rust is straightforward thanks to Rustup, the official Rust installer and version manager. In this lesson, you'll learn how to install Rust on Windows, macOS, and Linux, set up your development environment, and verify everything is working correctly.

What is Rustup?

Rustup is the recommended way to install Rust. It's a toolchain installer that manages:

  • Rust compiler (rustc): Compiles your Rust code
  • Cargo: Rust's package manager and build tool
  • Standard library: Core Rust functionality
  • Toolchains: Different versions of Rust (stable, beta, nightly)
Why Rustup? Rustup makes it easy to:
  • Install and update Rust
  • Switch between Rust versions
  • Add cross-compilation targets
  • Install additional components (rustfmt, clippy)

Installation Instructions

Windows

On Windows, you'll need the Microsoft C++ build tools before installing Rust.

  1. Install Visual Studio C++ Build Tools:
    • Download from Microsoft's website
    • Run the installer and select "Desktop development with C++"
  2. Download Rustup:
    • Visit rustup.rs
    • Download and run rustup-init.exe
  3. Run the Installer:
    • Follow the prompts (default options are recommended)
    • The installer will add Rust to your PATH
Windows Tip: After installation, restart your terminal or command prompt to ensure the PATH is updated.

macOS

On macOS, you'll need Xcode Command Line Tools.

  1. Install Xcode Command Line Tools:
    xcode-select --install
  2. Install Rustup:
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  3. Follow the Prompts:
    • Press 1 for default installation
    • The installer will configure your PATH automatically
  4. Reload your shell:
    source $HOME/.cargo/env

Linux

On Linux, installation is similar to macOS.

  1. Install build essentials:
    # Ubuntu/Debian
    sudo apt update
    sudo apt install build-essential
    
    # Fedora
    sudo dnf install gcc
    
    # Arch
    sudo pacman -S base-devel
  2. Install Rustup:
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  3. Configure PATH:
    source $HOME/.cargo/env

Verify Installation

After installation, verify that Rust is installed correctly:

# Check Rust compiler version
rustc --version

# Check Cargo version
cargo --version

# Check Rustup version
rustup --version

You should see output similar to:

rustc 1.75.0 (82e1608df 2023-12-21)
cargo 1.75.0 (1d8b05cdd 2023-11-20)
rustup 1.26.0 (5af9b9484 2023-04-05)
Best Practice: Keep Rust updated regularly using rustup update. Rust releases a new stable version every 6 weeks with improvements and bug fixes.

Essential Rustup Commands

Here are the most useful Rustup commands you'll need:

Command Description
rustup update Update Rust to the latest version
rustup self uninstall Uninstall Rust and Rustup
rustup doc Open offline documentation
rustup component add rustfmt Install code formatter
rustup component add clippy Install linter for code quality
Recommended: Install rustfmt and clippy right away:
rustup component add rustfmt clippy

Common Mistakes

1. PATH not updated

Problem: Commands like rustc or cargo not found after installation

Solution: Restart your terminal or manually source the environment:

# macOS/Linux
source $HOME/.cargo/env

# Windows: Restart terminal or add to PATH manually
# %USERPROFILE%\.cargo\bin

2. Missing C++ build tools (Windows)

Problem: Compilation fails with linker errors

Solution: Install Visual Studio C++ Build Tools before Rust

3. Using outdated Rust version

Problem: Code examples don't work or new features unavailable

Solution: Update regularly with rustup update

Summary

  • Rustup is the official Rust installer and version manager
  • Installation requires C++ build tools on Windows, Xcode tools on macOS, and build-essential on Linux
  • Verify installation with rustc --version and cargo --version
  • Keep Rust updated with rustup update
  • Install rustfmt and clippy for better development experience
  • Use rustup doc to access offline documentation

What's Next?

Now that Rust is installed, you're ready to write your first Rust program! In the next lesson, we'll create a "Hello, World!" program, understand the basic structure of a Rust program, and learn how to compile and run it.