Tutorials  /  Ubuntu

How to Install Rust on Ubuntu 20.04: Step-by-Step Guide

Ccentron Redaktion · January 2025 ·6 min read ·Ubuntu, Tutorial

Introduction

The Rust programming language, also known as rust-lang, is a powerful general-purpose programming language. Rust is syntactically similar to C++ and is used for a wide range of software development projects, including browser components, game engines, and operating systems.

VM

Matching infrastructure at centron

Ubuntu servers without your own hardware: ccloud³ VMs with full root access from €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Overview of This Tutorial

In this tutorial, you’ll install the latest version of Rust on Ubuntu 20.04, and then create, compile, and run a test program. The examples in this tutorial show the installation of Rust version 1.66.

Note: This tutorial also works for Ubuntu 22.04, however, you might be presented with interactive dialogs for various questions when you run apt upgrade. For example, you might be asked if you want to automatically restart services when required or if you want to replace a configuration file that you’ve modified. The answers to these questions depend on your software and preferences and are outside the scope of this tutorial.

Prerequisites

To complete this tutorial, you’ll need an Ubuntu 20.04 server with a sudo-enabled non-root user and a firewall.

Step 1 — Installing Rust on Ubuntu Using the rustup Tool

This tutorial uses the default option 1. However, if you’re familiar with the rustup installer and want to customize your installation, you can choose option 2. Type your selection and press Enter.

The output for option 1 is:

Code
info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2023-01-10, rust version 1.66.1 (90743e729 2023-01-10)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
67.4 MiB / 67.4 MiB (100 %) 40.9 MiB/s in 1s ETA: 0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
6.6 MiB / 6.6 MiB (100 %) 5.5 MiB/s in 1s ETA: 0s
info: installing component 'clippy'
info: installing component 'rust-docs'
19.1 MiB / 19.1 MiB (100 %) 2.4 MiB/s in 7s ETA: 0s
info: installing component 'rust-std'
30.0 MiB / 30.0 MiB (100 %) 5.6 MiB/s in 5s ETA: 0s
info: installing component 'rustc'
67.4 MiB / 67.4 MiB (100 %) 5.9 MiB/s in 11s ETA: 0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

stable-x86_64-unknown-linux-gnu installed - rustc 1.66.1 (90743e729 2023-01-10)

Rust is installed now. Great!

To get started you may need to restart your current shell.

This would reload your PATH environment variable to include Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:

Code
source "$HOME/.cargo/env"

Step 2 — Verifying the Installation

Next, run the following command to add the Rust toolchain directory to the PATH environment variable:

Code
source $HOME/.cargo/env

Verify the Rust installation by requesting the version:

Code
rustc --version

The rustc --version command returns the version of the Rust programming language installed on your system. For example:

Code
Output
sammy@ubuntu:~$ rustc --version
rustc 1.66.1 (90743e729 2023-01-10)

Step 3 — Installing a Compiler

Rust requires a linker program to join compiled outputs into one file. The GNU Compiler Collection (gcc) in the build-essential package includes a linker. If you don’t install gcc, then you might get the following error when you try to compile:

Code
error: linker `cc` not found
  |
  = note: No such file or directory (os error 2)

error: aborting due to previous error

You’ll use apt to install the build-essential package.

First, update the Apt package index:

Console
sudo apt update

Enter your password to continue if prompted. The apt update command outputs a list of packages that can be upgraded. For example:

Go
Output
sammy@ubuntu:~$ sudo apt update
[sudo] password for sammy: 
Hit:1 http://mirrors.digitalocean.com/ubuntu focal InRelease
Get:2 http://mirrors.digitalocean.com/ubuntu focal-updates InRelease [114 kB]
...
Fetched 11.2 MB in 5s (2131 kB/s)
Reading package lists... Done
Building dependency tree       
Reading state information... Done
103 packages can be upgraded. Run 'apt list --upgradable' to see them.

Next, upgrade any out-of-date packages:

Console
sudo apt upgrade

When the upgrades are complete, install the build-essential package:

Console
sudo apt install build-essential

Step 4 — Creating, Compiling, and Running a Test Program

In this step, you’ll create a test program to try out Rust and verify that it’s working properly.

Start by creating some directories to store the test script:

Console
mkdir ~/rustprojects
cd ~/rustprojects
mkdir testdir
cd testdir

Use nano or your favorite text editor to create a file in testdir to store your Rust code:

Code
nano test.rs

Copy the following code into test.rs and save the file:

Code
fn main() {
    println!(\"Congratulations! Your Rust program works.\");
}

Compile the code using the rustc command:

Code
rustc test.rs

Run the resulting executable:

Code
./test

The program prints to the terminal:

Code
Congratulations! Your Rust program works.

Other Commonly-Used Rust Commands

It’s a good idea to update your installation of Rust on Ubuntu regularly.

Enter the following command to update Rust:

Code
rustup update

You can also remove Rust from your system, along with its associated repositories.

Enter the following command to uninstall Rust:

Code
rustup self uninstall

You’re prompted to enter Y to continue the uninstall process:

Code
Output
sammy@ubuntu:~/rustprojects/testdir$ rustup self uninstall

Thanks for hacking in Rust!

This will uninstall all Rust toolchains and data, and remove
$HOME/.cargo/bin from your PATH environment variable.

Continue? (y/N)

Enter Y to continue:

Code
Continue? (y/N) y

info: removing rustup home
info: removing cargo home
info: removing rustup binaries
info: rustup is uninstalled

Rust is now removed from your system.

Conclusion

Now that you’ve installed and tested out Rust on Ubuntu, continue your learning with more Ubuntu tutorials.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Ubuntu
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren