How to Install Rust on Ubuntu Linux

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, strong standard library support, and seamless C interoperability make Rust practical for real-world development, from embedded systems and command-line tools to web servers and game engines.

This guide covers three methods to install Rust on Ubuntu: using rustup (the recommended installer and version manager), installing from Ubuntu’s official repositories for system-wide stability, and compiling from source for custom optimization requirements. You’ll verify your installation, create and run a test program to confirm everything works, and manage Rust updates and toolchains.

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.

Installation MethodRust VersionBest ForTrade-offs
rustup (Recommended)Latest stable, beta, or nightly toolchainsDevelopers needing cutting-edge Rust, toolchain flexibility, quick updates, and version switching without system package overheadInstalls to user home directory (~/.cargo/), not system-wide; you manage updates manually; requires internet access for initial setup
APT RepositoryStable version packaged by Ubuntu maintainers (typically slightly behind upstream)Server deployments, system-wide installation, integration with package managers, conservative version requirementsVersion lags behind latest Rust releases; updates depend on Ubuntu’s release cycle; may not have newest language features
Compile from SourceCustom build of any upstream releaseCustom compilation flags, non-standard installation paths (/opt/rust), reproducible builds, restricted networks with pre-fetched artifactsTakes 30-60 minutes to compile depending on hardware; requires build dependencies; you manage security patches yourself

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. Adding common build tools helps when compiling Rust crates that include native code:

sudo apt update

Now install curl with the essential build tooling:

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

rustup is the official Rust installer and version manager. Download and execute the installer script from the official Rust repository using curl:

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, you’ll see 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

After installation, 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

Also verify Cargo, the package manager:

cargo --version

Both commands should display version numbers (for example, “rustc 1.xx.x” and “cargo 1.xx.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.

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

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

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

The checksum command should return “OK.” If it reports a mismatch, re-download the tarball 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:

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

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 and a Cargo.toml file; the second builds and runs the default program.

Compiling hello-world v0.1.0 (/home/$USER/hello-world)
Finished dev [unoptimized + debuginfo] target(s) in 1.2s
     Running `target/debug/hello-world`
Hello, world!

For a release-optimized build, use:

cargo run --release

Release mode compiles with optimizations; development mode builds faster for iterative changes.

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

Managing Rust on Your System

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.

Typical update output looks like:

info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: checking for self-updates
info: downloading component 'rustc'
info: downloading component 'cargo'
info: downloading component 'rust-std'
info: installing component 'rustc'
info: installing component 'cargo'
info: installing component 'rust-std'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

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.

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 the formatting, linting, and language server components that rustup does not include by default:

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

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):

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

If the APT version is being used but you want rustup, ensure ~/.cargo/env is sourced in your shell profile. For a permanent fix with rustup priority, add this to ~/.bashrc or ~/.zshrc:

export PATH="$HOME/.cargo/bin:$PATH"

Cargo Build Fails with Linker Errors

Linker errors typically occur when build-essential or required development libraries are missing. Reinstall build dependencies:

sudo apt install -y build-essential pkg-config

Conclusion

Rust delivers memory-safe systems programming with three installation paths on Ubuntu: rustup for the latest releases and flexible toolchain management, APT for system-wide stability, and source compilation for custom optimization. The ownership system eliminates entire bug categories at compile time while Cargo handles dependency management. Update your rustup installation with rustup update and explore the official documentation and The Rust Book to master ownership, borrowing, and practical patterns.

Leave a Comment