Rust is a systems programming language that combines memory safety with blazing performance. The language eliminates entire categories of bugs such as null pointer crashes, data races, and buffer overflows at compile time without requiring a garbage collector. Built-in package management through Cargo, zero-cost abstractions, and seamless C interoperability make Rust practical for systems programming, web services, command-line tools, and embedded development where both safety and performance matter.
This guide covers three installation methods on Ubuntu: rustup for the latest toolchains and flexible version management, APT for system-wide integration with Ubuntu’s package manager, and source compilation for custom build flags and offline deployments. You’ll install Rust, verify the toolchain works correctly, create a test project with Cargo, and learn to manage updates and additional toolchains. By the end, you’ll have a working Rust development environment and understand how to maintain it.
Choose Your Rust Installation Method
Ubuntu ships Rust packages in its official repositories, but they lag upstream releases. You have several options: rustup provides the latest toolchains with easy switching, official repository packages offer stable versions integrated with system package management, and source compilation enables custom optimizations and controlled deployments where you prefetch artifacts.
| Method | Channel | Version | Updates | Best For |
|---|---|---|---|---|
| rustup (Recommended) | rustup.rs | Latest stable, beta, or nightly | Manual via rustup update | Active development needing latest features, toolchain flexibility, and version switching |
| APT Repository | Ubuntu Repos | Stable (lags upstream) | Automatic via apt upgrade | Production servers, system-wide installation, conservative stability requirements |
| Compile from Source | GitHub | Any upstream release | Manual recompilation | Custom build flags, offline deployments, reproducible builds, non-standard paths |
rustup is the recommended path for most Ubuntu users. It provides immediate access to the latest stable releases, toolchain flexibility, and straightforward updates. If you prefer system-wide integration or work on production servers, use the APT method. Choose source compilation only if you need custom compilation flags or work in offline environments.
Install Rust Using rustup (Recommended)
Update Ubuntu and Install Supporting Packages
Before installing Rust, update your system and install curl along with essential build tools that Rust crates with native code require:
sudo apt update
Next, install the required packages:
sudo apt install -y curl build-essential pkg-config
rustup installs pre-built toolchains, but these packages ensure you have the C toolchain and pkg-config support required by many Rust crates.
Download and Run the rustup Installer
Next, download and execute the rustup installer script. rustup is the official Rust installer and version manager maintained by the Rust project:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
This command downloads the installer with TLS verification enabled for security and pipes it directly to the shell. The script will prompt you with installation options.
Select Installation Options
After running the installer, the rustup script displays an interactive prompt asking whether to proceed with the default installation or customize options:
Type 1 and press Enter to proceed with the default installation (recommended). The installer will download and install the Rust compiler, Cargo package manager, and associated tools. Installation typically takes 1-5 minutes depending on your internet connection and hardware.
You should see the rustup installer menu similar to this:
Current installation options: default host triple: x86_64-unknown-linux-gnu default toolchain: stable profile: default modify PATH variable? yes 1) Proceed with standard installation (default) 2) Customize installation 3) Cancel installation
Load Rust Environment Variables
Once installation completes, rustup automatically adds Rust to your shell profile (~/.bashrc or ~/.zshrc). For the current terminal session, source the environment variables immediately:
source "$HOME/.cargo/env"
This command updates your PATH to include Rust binaries. Future shell sessions automatically source this file, so you only need to run this command once in the current terminal session. New terminal windows will have Rust available automatically.
Verify Rust Installation
Confirm that Rust installed correctly by checking the compiler version:
rustc --version
rustc 1.x.x (hash date)
Next, verify Cargo, the package manager:
cargo --version
cargo 1.x.x
If you see “command not found,” run source "$HOME/.cargo/env" again to reload the environment for your current session.
Install Rust from Ubuntu’s Official Repository
Ubuntu’s official repositories include pre-built Rust packages maintained by Canonical. This method offers system-wide installation integrated with APT, making it ideal for server deployments and conservative production environments. The trade-off is that Ubuntu’s Rust version typically lags a few releases behind upstream.
This installation method uses Ubuntu’s default APT repositories and works on all current Ubuntu releases, including 22.04 LTS and 24.04 LTS.
Ubuntu’s APT repositories usually trail upstream Rust by a few releases because they follow Ubuntu’s package cadence. Choose the APT method when stability and system-wide package management matter more than immediate access to the newest language features.
sudo apt update && sudo apt install -y rustc cargo
Verify the installation with the same commands as the rustup method:
rustc --version && cargo --version
rustc 1.75.0 (82e1608df 2023-12-21) (built from a source tarball) cargo 1.75.0
This method provides stable, system-wide integration but sacrifices access to the latest Rust features. Ubuntu’s Rust packages receive security updates through the standard apt mechanism, making them suitable for production servers where stability matters more than cutting-edge features.
Install Rust by Compiling from Source
For maximum control, compile Rust from source. This approach takes longer but allows custom optimization flags and non-standard installation paths (e.g., /opt/rust) useful for reproducible builds and tailored deployments.
Source builds still download the Rust bootstrap compiler and dependencies. If you need to build on a restricted or offline system, pre-download the stage0 artifacts and tarball on a connected machine, transfer them locally, and point the build at those files.
Compiling Rust from source takes 30-60 minutes depending on your hardware. A system with 4 CPU cores and 8GB RAM typically completes in 35-40 minutes. Plan accordingly if you’re following this method.
Install Build Dependencies
Install the packages required to compile Rust and its bundled LLVM toolchain:
sudo apt update
sudo apt install -y build-essential curl git python3 pkg-config libssl-dev llvm-dev libclang-dev cmake
Download Rust Source Code
Next, download the latest stable release tarball from Rust’s static hosting service:
RUST_VERSION="$(curl -s https://static.rust-lang.org/dist/channel-rust-stable.toml | awk 'BEGIN{FS="\\\""} /^\\[pkg.rustc\\]/{f=1;next} f && /^version =/{print $2; exit}' | cut -d' ' -f1)"
cd /tmp
curl -O https://static.rust-lang.org/dist/rustc-${RUST_VERSION}-src.tar.gz
The first line reads the stable channel manifest, pulls the rustc version from the rustc section, and strips build metadata so the download URL is valid. If you are offline, set RUST_VERSION manually (for example, RUST_VERSION="1.xx.x") after checking Rust’s static hosting page for available versions.
Verify the download integrity before extracting:
curl -O https://static.rust-lang.org/dist/rustc-${RUST_VERSION}-src.tar.gz.sha256
sha256sum -c rustc-${RUST_VERSION}-src.tar.gz.sha256
rustc-1.x.x-src.tar.gz: OK
If the checksum reports “FAILED,” the download was corrupted or tampered with. Delete the file and re-download before continuing.
Extract and Configure
tar -xzf rustc-${RUST_VERSION}-src.tar.gz && cd rustc-${RUST_VERSION}-src
Copy the example configuration file and decide where you want Rust installed. The config.toml file controls compilation settings, optimization levels, and the installation prefix:
cp config.example.toml config.toml
Leave the default prefix to install into /usr/local (requires sudo during install). To avoid sudo, set a user-local path such as $HOME/.local:
sed -i 's|# prefix = "/usr/local"|prefix = "'"$HOME"'/.local"|g' config.toml
If you want a dedicated system path, set it to /opt/rust and remember the install step will need sudo:
sed -i 's|# prefix = "/usr/local"|prefix = "/opt/rust"|g' config.toml
Compile and Install
With configuration complete, build Rust with optimizations enabled (this takes 30-60 minutes depending on your hardware):
./x.py build --release
Once compilation completes, install the compiled binaries using the same prefix you chose:
./x.py install
If the prefix points to a system directory such as /usr/local or /opt/rust, run the install with sudo:
sudo ./x.py install
Add the install location to your PATH. If you left the default /usr/local prefix, /usr/local/bin is typically present already. For a user-local prefix, run:
export PATH="$HOME/.local/bin:$PATH"
If you installed to /opt/rust, add that bin directory instead:
export PATH="/opt/rust/bin:$PATH"
To make this permanent, add the matching export line to your ~/.bashrc or ~/.zshrc file. Source compilation is most useful for production deployments requiring specific build flags, embedded systems with custom compilation needs, or offline environments where downloading pre-built binaries is not feasible.
Create a Test Rust Project
Generate and Run a Cargo Project
Use Cargo to scaffold and test your toolchain in one step:
cargo new hello-world && cd hello-world
cargo run
The first command creates a directory with src/main.rs (your main source file containing a basic “Hello, world!” program) and a Cargo.toml file (the project manifest that defines your package name, version, and dependencies). Cargo also initializes a git repository and creates a .gitignore file. The second command builds the project and runs the compiled binary. Rust compiles to native code, so you’ll see compilation messages before the program executes:
Compiling hello-world v0.1.0 (/home/$USER/hello-world)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s
Running `target/debug/hello-world`
Hello, world!
The compiled binary lives in target/debug/hello-world and can be run directly without cargo. Cargo handles incremental compilation, so subsequent builds only recompile changed files.
For a release-optimized build, use:
cargo run --release
Release mode applies optimizations that make your program run faster but take longer to compile. Development mode (the default for cargo run) prioritizes fast compilation for quick iteration during development. Use release builds for production deployments and performance testing, and development builds for daily coding work.
Optional: Quick Single-File rustc Test
If you want to confirm the compiler without Cargo, write and run a single file in /tmp:
cat <<'EOF' > /tmp/helloworld.rs
fn main() {
println!("Hello from rustc on Ubuntu with LinuxCapable.com");
}
EOF
rustc /tmp/helloworld.rs && /tmp/helloworld
Hello from rustc on Ubuntu with LinuxCapable.com
Manage Rust Toolchains and Updates
After successfully installing Rust, manage updates, toolchains, and versions to keep your development environment current. These commands apply when you installed Rust using rustup; APT users should use standard package management commands instead. Avoid keeping both rustup and APT installations at the same time to prevent PATH conflicts and version drift; pick one method and remove the other.
Update Rust to the Latest Version
rustup makes updating to the latest stable release simple. Run this command to check for and apply updates:
rustup update
This command checks for the latest stable, beta, and nightly releases and updates your installed toolchains if newer versions are available. If you’re already running the latest version, rustup reports “unchanged” and cleans up temporary files:
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu' info: checking for self-update stable-x86_64-unknown-linux-gnu unchanged - rustc 1.x.x info: cleaning up downloads & tmp directories
Update APT-Installed Rust
If you installed Rust from Ubuntu’s repositories, keep it current with a standard package upgrade:
sudo apt update && sudo apt install --only-upgrade rustc cargo
Remove rustup-installed toolchains before relying on the APT packages so your PATH does not switch between two versions unexpectedly.
Update Rust from Source
If you compiled Rust from source, recompile with the latest release using this update script. The script checks for prerequisites, fetches the latest stable version, and rebuilds Rust with your existing configuration:
#!/bin/bash
set -e
# Check prerequisites
if ! command -v curl &>/dev/null; then
echo "Error: curl is not installed. Install with: sudo apt install curl"
exit 1
fi
if ! command -v python3 &>/dev/null; then
echo "Error: python3 is not installed. Install with: sudo apt install python3"
exit 1
fi
# Fetch latest stable version
RUST_VERSION="$(curl -s https://static.rust-lang.org/dist/channel-rust-stable.toml | awk 'BEGIN{FS="\\""} /^\\[pkg.rustc\\]/{f=1;next} f && /^version =/{print $2; exit}' | cut -d' ' -f1)"
if [ -z "$RUST_VERSION" ]; then
echo "Error: Failed to fetch Rust version from manifest."
exit 1
fi
echo "Downloading Rust $RUST_VERSION source..."
cd /tmp
curl -O https://static.rust-lang.org/dist/rustc-${RUST_VERSION}-src.tar.gz
curl -O https://static.rust-lang.org/dist/rustc-${RUST_VERSION}-src.tar.gz.sha256
sha256sum -c rustc-${RUST_VERSION}-src.tar.gz.sha256 || exit 1
tar -xzf rustc-${RUST_VERSION}-src.tar.gz
cd rustc-${RUST_VERSION}-src
echo "Building Rust $RUST_VERSION..."
./x.py build --release
echo "Installing Rust $RUST_VERSION..."
if [ -d "/opt/rust" ] || grep -q '/opt/rust' ~/.bashrc ~/.zshrc 2>/dev/null; then
sudo ./x.py install
else
./x.py install
fi
echo "Rust update complete. Verify with: rustc --version"
Do not automate this script with cron. Rust compilation can fail due to insufficient resources, network issues, or upstream changes. Run the script manually and verify the build succeeded before relying on the updated version.
Save this script to a file such as ~/update-rust.sh, make it executable with chmod +x ~/update-rust.sh, and run it when you want to update your source-compiled Rust installation.
Install Additional Toolchains
Beyond stable Rust, you can install beta or nightly builds to test upcoming features or use development versions:
rustup toolchain install nightly
Check available toolchains on your system:
rustup toolchain list
Add rustfmt, Clippy, and rust-analyzer
Install essential development tools that rustup does not include by default. rustfmt automatically formats your code to match Rust style guidelines, Clippy provides hundreds of linting rules to catch common mistakes and suggest improvements, and rust-analyzer powers IDE features like autocomplete and inline error checking:
rustup component add rustfmt clippy rust-analyzer
Find the rust-analyzer binary path for your editor or language client:
rustup which --toolchain stable rust-analyzer
If your editor needs rust-analyzer on PATH, symlink the rustup-provided binary:
ln -sf "$(rustup which --toolchain stable rust-analyzer)" "$HOME/.cargo/bin/rust-analyzer"
APT users can add rustfmt and Clippy with the corresponding packages (sudo apt install rustfmt rust-clippy) if they prefer the repository toolchain.
Switch Between Toolchain Versions
If you need to use nightly for a specific project, set it as the default or use it for individual commands:
rustup default nightly
To temporarily use nightly for a single command without changing the default:
cargo +nightly build
Switch back to stable with:
rustup default stable
View Available Compilation Targets
Rust supports cross-compilation to many architectures. List installed targets and available targets for download:
rustup target list
To install a target for cross-compilation (e.g., WebAssembly):
rustup target add wasm32-unknown-unknown
Uninstall Rust
To completely remove Rust and rustup installed via the installer script:
rustup self uninstall
A confirmation prompt will appear. Type y and press Enter to proceed with uninstalling all Rust toolchains and removing rustup from your system.
This will uninstall all Rust toolchains and data, and remove $HOME/.cargo/bin from your PATH environment variable. Continue? (y/N)
If you installed Rust from Ubuntu’s APT repository instead, uninstall with:
sudo apt remove rustc cargo
After removing rustc and cargo, check for orphaned dependencies with sudo apt autoremove -s. Rust packages are self-contained and typically do not leave orphaned dependencies, but running this check ensures a clean removal.
APT removal output is similar to:
Reading package lists... Done Building dependency tree... Done The following packages will be REMOVED: cargo rustc After this operation, 415 MB disk space will be freed. Do you want to continue? [Y/n]
If you compiled Rust from source, remove the install prefix you chose and the PATH entry you added. Replace the path with your prefix (examples shown for /opt/rust and a user-local install):
The following commands permanently delete Rust installation directories. Double-check the path before executing rm -rf commands to avoid accidental data loss.
sudo rm -rf /opt/rust
[ -f "$HOME/.bashrc" ] && sed -i '/opt\\/rust\\/bin/d' "$HOME/.bashrc"
[ -f "$HOME/.zshrc" ] && sed -i '/opt\\/rust\\/bin/d' "$HOME/.zshrc"
# If you used a local prefix instead of /opt/rust, remove only the Rust files:
rm -f "$HOME/.local/bin/rust"* "$HOME/.local/bin/cargo"
rm -rf "$HOME/.local/lib/rustlib"
Troubleshooting Rust Installation
Command Not Found After Installation
If rustc or cargo return “command not found” after installation, the environment variables haven’t been loaded. Source the environment manually:
source "$HOME/.cargo/env"
This only affects your current terminal session. Open a new terminal window and the commands will be available automatically (rustup automatically adds the source command to ~/.bashrc and ~/.zshrc).
rustup Installer Hangs or Times Out
If the installer stalls during download, check your internet connection. If the connection is stable but installation still hangs, try downloading the script first and running it locally:
curl -O https://sh.rustup.rs && chmod +x rustup.rs && ./rustup.rs
Version Mismatch Between rustup and APT Installations
If you’ve installed Rust both via rustup and APT, PATH environment variables might prioritize the wrong version. Check which Rust binary is being used:
which rustc && rustc --version
/usr/bin/rustc rustc 1.75.0 (82e1608df 2023-12-21) (built from a source tarball)
If the output shows /usr/bin/rustc (APT version) but you want rustup’s version from ~/.cargo/bin/rustc, you have three options. First, ensure ~/.cargo/env is sourced in your shell profile by adding this to ~/.bashrc or ~/.zshrc:
export PATH="$HOME/.cargo/bin:$PATH"
Alternatively, remove the APT version entirely to eliminate confusion:
sudo apt remove rustc cargo
Or if you prefer the APT version, remove rustup and rely solely on system packages:
rustup self uninstall
Maintaining both installations creates PATH conflicts, version confusion, and unpredictable behavior. Choose one method and stick with it.
Cargo Build Fails with Linker Errors
If cargo build fails with linker errors such as “error: linker ‘cc’ not found” or “error: could not compile,” the build-essential package is likely missing. Reinstall build dependencies:
sudo apt install -y build-essential pkg-config
After installing dependencies, clean the build cache and try again:
cargo clean && cargo build
Conclusion
Rust delivers memory-safe systems programming with zero-cost abstractions and compile-time guarantees that eliminate entire bug categories. You now have Rust installed through rustup, APT, or source compilation depending on your deployment needs. Keep your toolchain current with rustup update (or apt upgrade for APT installations) and enhance your workflow by adding rustfmt for automatic code formatting and Clippy for catching common mistakes. Start with The Rust Book to master ownership and borrowing fundamentals—these concepts differentiate Rust from other systems languages and enable its safety guarantees. The official documentation provides comprehensive references for standard library APIs, advanced features, and ecosystem crates. For polyglot development environments, our guides on GCC compiler setup, Python virtual environments, and Node.js installation complement your Rust toolchain for projects requiring multiple languages.