
Rust is a systems programming language focused on memory safety, performance, and concurrency. It is syntactically similar to C++ and is used across browser engines, game engines, operating systems, and CLI tools. The recommended way to install Rust on Ubuntu is with rustup, the official toolchain manager, which installs the latest stable compiler plus Cargo (Rust’s package manager and build tool).
In this guide you’ll install Rust on Ubuntu using rustup, verify the installation, install the system linker dependency, and run a small test program. You’ll also learn how to manage toolchains, add optional components, handle common issues, and keep Rust updated. The steps work on current Ubuntu LTS releases and later versions.
sudo access.curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh, then source "$HOME/.cargo/env".rustc --version and cargo --version.sudo apt install build-essential.rustup update to update; use rustup component add clippy rustfmt for the linter and formatter.rust-toolchain.toml in the project root for reproducible builds across development and CI.The apt package for Rust is typically several major versions behind the current stable release and does not include Cargo or toolchain switching. For development, rustup is the correct choice because it provides up-to-date stable Rust, the full Cargo workflow, and toolchain management.
Run the official installer (this downloads and runs the rustup script):
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
You’ll be asked to choose an installation type:
info: downloading installer
Welcome to Rust!
This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.
Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:
/home/sammy/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
/home/sammy/.cargo
Current installation options:
default host triple: x86_64-unknown-linux-gnu
default toolchain: stable (default)
profile: default
modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>
Option 1 installs the stable toolchain and adds ~/.cargo/bin to PATH; use this for a standard setup. Option 2 lets you choose a different host triple, channel (e.g. nightly or beta), or a minimal install profile. Option 3 cancels the installation.
After the installer finishes, load the environment in the current shell:
source "$HOME/.cargo/env"
The file $HOME/.cargo/env adds ~/.cargo/bin to PATH. Sourcing it is required only in the current session because the installer already updates the shell profile (~/.profile, ~/.bashrc, or the equivalent for your shell); new login sessions will have PATH set automatically.
Check that the compiler and package manager are available:
rustc --version
cargo --version
Example output:
rustc 1.75.0 (82e1608df 2023-12-21)
cargo 1.75.0 (1d8b05cdd 2023-11-20)
Your output will show the current stable version at install time, which will differ from the example above. Rust ships a new stable release every six weeks.
Without a system linker, Rust cannot produce executables. You may see:
error: linker `cc` not found
= note: No such file or directory (os error 2)
The GNU linker is provided by the build-essential package. Install it:
sudo apt update
sudo apt install build-essential -y
On a fresh DigitalOcean Droplet, build-essential is not installed by default. Install it before running cargo build or rustc on a new server.
Create a directory and a single-file program to confirm the toolchain works:
mkdir -p ~/rustprojects/testdir
cd ~/rustprojects/testdir
nano test.rs
Add the following to test.rs:
fn main() {
println!("Congratulations! Your Rust program works.");
}
Compile and run:
rustc test.rs
./test
Expected output:
Congratulations! Your Rust program works.
For any real project, use Cargo instead of invoking rustc directly. Cargo creates a standard project layout and a manifest for dependencies.
cargo new hello_world
cd hello_world
cargo run
Expected output:
Compiling hello_world v0.1.0 (/home/sammy/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 0.43s
Running `target/debug/hello_world`
Hello, world!
cargo new creates a src/main.rs file and a Cargo.toml manifest in the project directory.
Run:
rustup update
Run this regularly to stay on the latest stable.
Stable is the default and is recommended for most work. Some tools require the nightly toolchain: for example, cargo-fuzz (fuzz testing) and miri (undefined behavior detection) run on nightly. To use nightly for a single command without changing the default:
rustup toolchain install nightly
rustup run nightly cargo build
rustup run runs the given command with the specified toolchain and does not change the system default.
For reproducible builds, pin the Rust version.
Method 1: install and set a specific version:
rustup toolchain install 1.75.0
rustup default 1.75.0
Method 2: use a project-level file (preferred for shared repos). Create rust-toolchain.toml in the project root:
[toolchain]
channel = "1.75.0"
Anyone who clones the repo and has rustup installed will automatically use this version when they run cargo or rustc in the project. Replace 1.75.0 with the Rust version your project targets. Check releases.rs for the current stable version.
Clippy is the linter; rustfmt formats code. Add them and list installed components:
rustup component add clippy rustfmt
rustup component list --installed
cargo clippy
cargo fmt
Clippy reports issues the compiler does not flag, such as unnecessary .clone() calls or iterator patterns that compile but are less efficient.
| Feature | apt (system package) | rustup |
|---|---|---|
| Rust version | Determined by Ubuntu release; typically several major versions behind current stable | Latest stable at install time |
| Cargo included | No, must install separately | Yes, bundled |
| Toolchain switching | No | Yes, via rustup default and rustup run |
| Component management | No | Yes, Clippy, rustfmt, rust-analyzer |
| Update method | apt upgrade |
rustup update |
| Recommended for development | No | Yes |
rustc or cargo not found after installSee Step 1 for how $HOME/.cargo/env works. Run source "$HOME/.cargo/env" in the current shell, or open a new terminal.
linker 'cc' not foundThe system linker is missing. Install it:
sudo apt update && sudo apt install build-essential -y
Then retry the build.
Use the official one-liner from Step 1. If running a downloaded script locally, run chmod +x rustup-init.sh and then ./rustup-init.sh.
To uninstall rustup and all toolchains:
rustup self uninstall
Confirm with y. This removes ~/.rustup and ~/.cargo and restores the shell profile. Reinstall anytime with the same curl command from Step 1.
export RUSTUP_DIST_SERVER="https://mirrors.tuna.tsinghua.edu.cn/rustup"
export RUSTUP_UPDATE_ROOT="https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup"
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Set these variables before running the installer. Replace the mirror URL with one geographically close to your server.
rust-toolchain.toml in the project root, or run rustup toolchain install <version> then rustup default <version> for one-off environments. Both ensure reproducible builds.cargo new and cargo build over compiling single files with rustc for dependencies, tests, and standard layout.build-essential once on new systems or containers before building.rustup update regularly and avoid staying on very old stable for long.What is the recommended way to install Rust on Ubuntu?
Use the official rustup installer: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh. It installs the latest stable Rust and Cargo and supports toolchains and components.
Should I use apt or rustup to install Rust?
Use rustup. The apt package is often older and does not provide the same update path or Cargo workflow. rustup is the official rustup installer from the Rust project.
What is Cargo in Rust?
Cargo is Rust’s package manager and build tool. It handles creating projects, resolving dependencies, compiling, testing, and running. Commands include cargo new, cargo build, cargo run, and cargo test.
How do I update Rust on Ubuntu?
Run rustup update. This updates the active toolchain to the latest stable (or the channel you use).
How do I switch Rust toolchains?
Use rustup default <toolchain> (e.g. rustup default stable or rustup default nightly). Use rustup run <toolchain> cargo build to run a single command with a different toolchain without changing the default.
What is the difference between stable and nightly Rust?
Stable is the released, supported channel (recommended for production). Nightly includes experimental features and may break; use it only when a tool or library requires it.
How do I uninstall Rust completely?
Run rustup self uninstall and confirm with y. This removes rustup, all toolchains, and the Cargo and Rustup home directories.
Why is rustc not found after installation?
The current shell’s PATH does not include ~/.cargo/bin. Run source "$HOME/.cargo/env" or start a new login shell. The installer updates the profile so future sessions have PATH set.
Which Ubuntu releases does rustup support?
rustup works on all actively supported Ubuntu LTS releases. Install build-essential as described in Step 3 before compiling. No release-specific steps are required.
When would I need the nightly toolchain?
Use nightly when a tool explicitly requires it. Common examples are cargo-fuzz for fuzz testing and miri for catching undefined behavior. For production application code, always use stable.
You have installed Rust on Ubuntu with rustup, verified the compiler and Cargo, installed the system linker, and run a test program with both rustc and Cargo. For related setup, see How To Install Node.js on Ubuntu, How To Install Git on Ubuntu, and How To Install and Use Docker on Ubuntu.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Building future-ready infrastructure with Linux, Cloud, and DevOps. Full Stack Developer & System Administrator. Technical Writer @ DigitalOcean | GitHub Contributor | Passionate about Docker, PostgreSQL, and Open Source | Exploring NLP & AI-TensorFlow | Nailed over 50+ deployments across production environments.
I’m once again getting ready to take a stab at Rust. And I remembered that the recommended way was via curl rather than apt. But I decided to check what was in the Ubuntu repositories anyway. I note that, at least on my system, there are 1,332 librust-* packages… So, if I go with curl, will I later be in dependency hell / build hell if I want a particular library? (I’m still inclined to use curl, as I’m still a baby rustacean, and by the time I’m needing special libraries, I suppose I’ll be able to figure out what to do.)
- ubuntourist
Hi. Thanks for putting this together. Worked a treat for the latest version: stable-x86_64-unknown-linux-gnu unchanged - rustc 1.53.0 (53cb7b09b 2021-06-17)
- popagaka
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.