Installing Rust
10 min read
Let’s get Rust installed on your system. This guide covers all major platforms: macOS, Linux, and Windows.
Quick Overview
Section titled “Quick Overview”Rust installation is simple using rustup, the official Rust toolchain installer. Think of it like nvm for Node.js - it manages Rust versions and tooling.
Time required: 10-20 minutes
TypeScript/JavaScript Comparison
Section titled “TypeScript/JavaScript Comparison”Installing Node.js:
# Download installer from nodejs.org# Or use nvmcurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bashnvm install --lts # Installs current LTS (e.g. Node 22)nvm use --ltsInstalling Rust:
# Use rustup (official installer)curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shsource $HOME/.cargo/envKey similarities:
- Both use version managers (nvm/rustup)
- Both install command-line tools
- Both update your PATH
Key differences:
- The rustup download is larger (~200 MB) because it bundles the toolchain, but it’s a single step
- Rust includes everything (compiler, package manager, formatter, linter)
- No separate npm install - Cargo is built-in!
Installation by Platform
Section titled “Installation by Platform”Prerequisites
Section titled “Prerequisites”First, install Xcode Command Line Tools (if not already installed):
xcode-select --installThis provides the C compiler needed for Rust’s linker.
Install Rust
Section titled “Install Rust”# Download and run rustupcurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shWhat this does:
- Downloads rustup
- Installs the latest stable Rust
- Installs cargo (package manager)
- Adds Rust to your PATH
- Installs rustfmt (formatter) and cargo-clippy (linter)
Interactive options:
1) Proceed with installation (default)2) Customize installation3) Cancel installationJust press Enter for option 1 (default).
Configure Your Shell
Section titled “Configure Your Shell”# Add cargo to your PATHsource $HOME/.cargo/env
# Or restart your terminalFor zsh (default on macOS):
rustup automatically adds this line to ~/.zshrc:
. "$HOME/.cargo/env"For bash:
rustup adds this to ~/.bash_profile:
. "$HOME/.cargo/env"Verify Installation
Section titled “Verify Installation”# Check Rust compilerrustc --version# Output: rustc 1.96.0 (or newer)
# Check cargocargo --version# Output: cargo 1.96.0 (or newer)
# Check rustuprustup --version# Output: rustup 1.29.0 (or newer)Prerequisites
Section titled “Prerequisites”Install build essentials (C compiler and linker):
Debian/Ubuntu:
sudo apt updatesudo apt install build-essentialFedora:
sudo dnf groupinstall "Development Tools"Arch:
sudo pacman -S base-develInstall Rust
Section titled “Install Rust”curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shFollow the same steps as macOS.
Configure Your Shell
Section titled “Configure Your Shell”source $HOME/.cargo/envAdd to your shell rc file (~/.bashrc, ~/.zshrc, etc.):
. "$HOME/.cargo/env"Verify Installation
Section titled “Verify Installation”rustc --versioncargo --versionrustup --versionWindows
Section titled “Windows”Option 1: Native Windows (Recommended)
Section titled “Option 1: Native Windows (Recommended)”Prerequisites:
Install Visual Studio C++ Build Tools:
- Download from: https://visualstudio.microsoft.com/visual-cpp-build-tools/
- Run the installer
- Select “C++ build tools” workload
- Install (takes ~5-10 minutes)
Install Rust:
- Download rustup-init.exe from: https://rustup.rs/
- Run the installer
- Follow prompts (default options work fine)
- Restart your terminal
Verify:
rustc --versioncargo --versionOption 2: WSL2 (Windows Subsystem for Linux)
Section titled “Option 2: WSL2 (Windows Subsystem for Linux)”If you’re using WSL2, follow the Linux instructions above.
Why WSL2?
- Better compatibility with Unix-based tools
- Faster compilation in some cases
- More similar to Linux production environments
How to install WSL2:
# In PowerShell (Administrator)wsl --installThen follow Linux instructions inside WSL2.
Detailed Explanation
Section titled “Detailed Explanation”What Gets Installed?
Section titled “What Gets Installed?”When you run rustup, you get:
1. rustc - The Rust compiler
rustc main.rs # Compiles to executable2. cargo - Package manager and build tool
cargo new my_project # Create new projectcargo build # Build projectcargo run # Build and runcargo test # Run tests3. rustup - Toolchain manager
rustup update # Update Rustrustup default stable # Set default versionrustup doc # Open docs4. rustfmt - Code formatter
rustfmt main.rs # Format codecargo fmt # Format whole project5. clippy - Linter
cargo clippy # Run linter6. rust-docs - Offline documentation
rustup doc # Open in browserrustup doc --std # Standard library docsAll included! No separate installations needed (unlike npm, prettier, eslint, etc. in Node.js).
Directory Structure
Section titled “Directory Structure”Rust installs to:
macOS/Linux:
~/.cargo/ # Cargo home├── bin/ # Executables (cargo, rustc, rustfmt, etc.)├── registry/ # Downloaded crate metadata└── git/ # Git dependencies
~/.rustup/ # Rustup home├── toolchains/ # Installed Rust versions└── settings.toml # ConfigurationWindows:
C:\Users\<you>\.cargo\ # Cargo homeC:\Users\<you>\.rustup\ # Rustup homeEnvironment Variables
Section titled “Environment Variables”Rustup adds these to your PATH:
# macOS/Linuxexport PATH="$HOME/.cargo/bin:$PATH"
# Windows (added by installer)%USERPROFILE%\.cargo\binCompare to Node.js:
# Node.js (with nvm)export NVM_DIR="$HOME/.nvm"[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"Similar concept, different implementation.
Key Differences from Node.js
Section titled “Key Differences from Node.js”1. Single Installer
Section titled “1. Single Installer”Node.js ecosystem:
# Multiple separate installsnpm install -g typescriptnpm install -g ts-nodenpm install -g prettiernpm install -g eslintRust:
# All in one!curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh# You now have: compiler, package manager, formatter, linter, docs2. One-Step Installation
Section titled “2. One-Step Installation”Node.js:
- Download size: ~50-100 MB
- Installation time: 2-5 minutes
- node_modules can be gigabytes per project
Rust:
- Download size: ~200 MB (includes the full toolchain, so the download is larger)
- Installation time: 2-5 minutes
- A single command sets up the compiler, package manager, formatter, and linter; project dependencies are compiled and cached under
~/.cargo
3. Version Management Built-In
Section titled “3. Version Management Built-In”Node.js:
# Need separate tool (nvm, n, fnm)nvm install --ltsnvm use --ltsnvm listRust:
# Built into rustuprustup install stablerustup default stablerustup toolchain list4. Offline Documentation
Section titled “4. Offline Documentation”Node.js:
# Online only (mostly)# Browse to nodejs.org or devdocs.ioRust:
# Full docs offline!rustup doc # Opens local browserrustup doc --std # Standard libraryrustup doc --book # The Rust BookCommon Pitfalls
Section titled “Common Pitfalls”Pitfall 1: “command not found” After Installation
Section titled “Pitfall 1: “command not found” After Installation”Problem:
rustc --version# zsh: command not found: rustcSolution:
# Option 1: Restart your terminal
# Option 2: Source the cargo envsource $HOME/.cargo/env
# Option 3: Check PATHecho $PATH | grep cargo# Should see: /Users/you/.cargo/binWhy: Your shell needs to reload its configuration to see the new PATH.
Pitfall 2: “linking with cc failed”
Section titled “Pitfall 2: “linking with cc failed””Problem:
cargo build# error: linking with `cc` failedSolution:
macOS:
xcode-select --installLinux:
sudo apt install build-essential # Debian/UbuntuWindows: Install Visual Studio C++ Build Tools
Why: Rust needs a C compiler for linking. The Rust compiler produces object files, but needs a linker to create the final executable.
Pitfall 3: Old Version of Rust
Section titled “Pitfall 3: Old Version of Rust”Problem: Some examples don’t compile because you’re using an old Rust version.
Solution:
# Update to latest stablerustup update
# Check versionrustc --versionBest practice: Run rustup update periodically; a new stable release ships every 6 weeks.
Pitfall 4: Permission Errors on Linux
Section titled “Pitfall 4: Permission Errors on Linux”Problem:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh# Permission deniedSolution: Don’t use sudo! Install in your home directory:
# Correct (no sudo)curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Then install system packages with sudo if neededsudo apt install build-essentialBest Practices
Section titled “Best Practices”1. Keep Rust Updated
Section titled “1. Keep Rust Updated”# Update Rust (a new stable lands every 6 weeks)rustup update
# Check for updatesrustup checkNew versions come every 6 weeks with improvements and bug fixes.
2. Install rust-analyzer (VS Code)
Section titled “2. Install rust-analyzer (VS Code)”# In VS Code, install extension:# rust-lang.rust-analyzer
# Or from terminal:code --install-extension rust-lang.rust-analyzerWhat it provides:
- Code completion
- Inline errors
- Go to definition
- Refactoring tools
- Inline type hints
Compare to TypeScript:
// TypeScript has built-in language server// Rust needs rust-analyzer extension3. Configure Your Editor
Section titled “3. Configure Your Editor”VS Code settings.json:
{ "[rust]": { "editor.defaultFormatter": "rust-lang.rust-analyzer", "editor.formatOnSave": true }, "rust-analyzer.checkOnSave": true, "rust-analyzer.check.command": "clippy"}This enables:
- Format on save (like Prettier)
- Clippy linting on save (like ESLint)
4. Set Up Shell Completions
Section titled “4. Set Up Shell Completions”# For bashrustup completions bash >> ~/.bash_completion
# For zshrustup completions zsh > ~/.zfunc/_rustup
# For fishrustup completions fish > ~/.config/fish/completions/rustup.fishNow you can tab-complete Rust commands!
Real-World Example: Complete Setup
Section titled “Real-World Example: Complete Setup”Here’s a complete setup script for a new machine:
macOS/Linux:
#!/bin/bash
# 1. Install prerequisites# macOSxcode-select --install
# Linux (Ubuntu/Debian)# sudo apt update && sudo apt install build-essential
# 2. Install Rustcurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
# 3. Source cargo envsource $HOME/.cargo/env
# 4. Verify installationrustc --versioncargo --version
# 5. Install rust-analyzer (if using VS Code)code --install-extension rust-lang.rust-analyzer
# 6. Configure VS Codemkdir -p ~/.config/Code/Usercat > ~/.config/Code/User/settings.json << EOF{ "[rust]": { "editor.defaultFormatter": "rust-lang.rust-analyzer", "editor.formatOnSave": true }}EOF
echo "Rust installed successfully!"Compare to Node.js setup:
#!/bin/bash
# 1. Install nvmcurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
# 2. Install Node.js (current LTS)nvm install --ltsnvm use --lts
# 3. Install global toolsnpm install -g typescript ts-node prettier eslint
# 4. Verifynode --versionnpm --version
echo "Node.js installed successfully!"Further Reading
Section titled “Further Reading”Official Documentation
Section titled “Official Documentation”- Rustup Book - Complete rustup guide
- Installation Page - Official installation guide
- Rust Forge - Infrastructure documentation
Editor Setup
Section titled “Editor Setup”- rust-analyzer Manual - VS Code extension docs
- IntelliJ Rust - For IntelliJ/CLion
- Vim Rust - For Vim/Neovim
Platform-Specific
Section titled “Platform-Specific”Exercises
Section titled “Exercises”Exercise 1: Verify Your Installation
Section titled “Exercise 1: Verify Your Installation”Run these commands and verify the output:
# Should print version (1.96.0 or newer)rustc --version
# Should print versioncargo --version
# Should print versionrustup --version
# Should list one toolchain (stable)rustup toolchain list
# Should open documentation in browserrustup doc
# Should print help textcargo --helpExercise 2: Update Rust
Section titled “Exercise 2: Update Rust”# Check for updatesrustup check
# Update all componentsrustup update
# Verify new versionrustc --versionExercise 3: Install Additional Components
Section titled “Exercise 3: Install Additional Components”# Install nightly (for experimental features)rustup toolchain install nightly
# Install source code (for rust-analyzer)rustup component add rust-src
# List installed componentsrustup component list --installedExercise 4: Configure Your Editor
Section titled “Exercise 4: Configure Your Editor”- Install rust-analyzer for your editor
- Create a test file:
test.rs - Type:
fn main() { - Verify that auto-completion works
- Add a syntax error and verify that inline errors appear
Summary
Section titled “Summary”What you’ve learned:
- How to install Rust using rustup
- What tools come with Rust (rustc, cargo, rustfmt, clippy)
- How to verify your installation
- Common pitfalls and solutions
- Best practices for editor setup
What you have now:
- Rust compiler (rustc)
- Package manager (cargo)
- Formatter (rustfmt)
- Linter (clippy)
- Documentation (rustup doc)
- Version manager (rustup)
Compare to Node.js:
- Node.js requires separate tools (npm, prettier, eslint)
- Rust includes everything in one install
- Both use version managers (nvm/rustup)
- Rust has offline documentation built-in
You’re ready to write Rust!