Step-by-step Guide: Installing Rust on Ubuntu 20.04

Rust lang

Rust, also referred to as rust-lang, is an influential, all-purpose programming language that shares similarities with C++. It finds application in diverse software development ventures such as browser components, game engines, and operating systems.

You will be guided through installing the most up-to-date Rust version on Ubuntu 20.04. Additionally, you’ll learn how to create, compile, and execute a test program. The provided examples demonstrate the installation of Rust’s 1.66 version.

Note

Please be aware that this tutorial is applicable to Ubuntu 22.04 as well. However, it is possible that you will encounter interactive dialogs with different questions during the apt upgrade process. These questions may include whether you wish to automatically restart services when necessary or replace a configuration file that you have modified. The responses to these questions are specific to your software and personal preferences, and are not covered in this tutorial.

Requirements

In order to finish this tutorial, it is necessary to have an Ubuntu 20.04 server with a non-root user enabled with sudo and a firewall. You can accomplish this by referring to our guide on Initial Server Setup with Ubuntu 20.04.

To install Rust on Ubuntu, utilize the rustup Tool by following these steps:

While there are various methods available to install Rust on Linux, the preferred approach is utilizing the rustup command line tool.

Execute the command to acquire the rustup tool and install the most recent stable edition of Rust.

  1. curl –proto ‘=https’ –tlsv1.3 https://sh.rustup.rs -sSf | sh

 

You’re asked to select the installation type.

Output

sammy@ubuntu:~$ curl –proto ‘=https’ –tlsv1.3 https://sh.rustup.rs -sSf | sh 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 This can be modified with the CARGO_HOME environment variable. The cargo, rustc, rustup and other commands will be added to Cargo’s bin directory, located at: /home/sammy/.cargo/bin This path will then be added to your PATH environment variable by modifying the profile files located at: /home/sammy/.profile /home/sammy/.bashrc You can uninstall at any time with rustup self uninstall and these changes will be reverted. 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 >

If you are familiar with the rustup installer and wish to tailor your installation, you have the option to select option 2 instead of the default option 1. Simply type your preference and hit Enter.

For option 1, the result is:

Output

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: source “$HOME/.cargo/env” sammy@ubuntu:~$

After that, execute the given command to include the Rust toolchain directory in the PATH environment variable.

  1. source $HOME/.cargo/env

 

Verifying the Installation: Proceed with Step 2.

To confirm the installation of Rust, simply request for the version.

  1. rustc –version

 

When you run the rustc –version command, it displays the installed version of the Rust programming language on your system. For instance:

Output

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

Installing a Compiler is the third step.

If you don’t have gcc installed, you may encounter an error during the compilation process because Rust relies on a linker program to merge compiled outputs into a single file. However, by installing gcc from the build-essential package, which includes a linker, you can resolve this issue.

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

error: aborting due to previous error

To install the build-essential package, apt will be utilized.

To begin with, please ensure that you update the Apt package index.

  1. sudo apt update

 

If prompted, input your password to proceed. The command “apt update” generates a list of packages that are eligible for upgrading. To illustrate:

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] Hit:3 https://repos-droplet.digitalocean.com/apt/droplet-agent main InRelease Get:4 http://mirrors.digitalocean.com/ubuntu focal-backports InRelease [108 kB] Get:5 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB] Get:6 http://mirrors.digitalocean.com/ubuntu focal-updates/main amd64 Packages [2336 kB] Get:7 http://mirrors.digitalocean.com/ubuntu focal-updates/main Translation-en [403 kB] Get:8 http://mirrors.digitalocean.com/ubuntu focal-updates/main amd64 c-n-f Metadata [16.2 kB] Get:9 http://mirrors.digitalocean.com/ubuntu focal-updates/restricted amd64 Packages [1560 kB] Get:10 http://mirrors.digitalocean.com/ubuntu focal-updates/restricted Translation-en [220 kB] Get:11 http://mirrors.digitalocean.com/ubuntu focal-updates/restricted amd64 c-n-f Metadata [620 B] Get:12 http://mirrors.digitalocean.com/ubuntu focal-updates/universe amd64 Packages [1017 kB] Get:13 http://mirrors.digitalocean.com/ubuntu focal-updates/universe Translation-en [236 kB] Get:14 http://mirrors.digitalocean.com/ubuntu focal-updates/universe amd64 c-n-f Metadata [23.2 kB] Get:15 http://mirrors.digitalocean.com/ubuntu focal-updates/multiverse amd64 Packages [25.2 kB] Get:16 http://mirrors.digitalocean.com/ubuntu focal-updates/multiverse Translation-en [7408 B] Get:17 http://mirrors.digitalocean.com/ubuntu focal-updates/multiverse amd64 c-n-f Metadata [604 B] Get:18 http://mirrors.digitalocean.com/ubuntu focal-backports/main amd64 Packages [45.7 kB] Get:19 http://mirrors.digitalocean.com/ubuntu focal-backports/main Translation-en [16.3 kB] Get:20 http://mirrors.digitalocean.com/ubuntu focal-backports/main amd64 c-n-f Metadata [1420 B] Get:21 http://mirrors.digitalocean.com/ubuntu focal-backports/universe amd64 Packages [24.9 kB] Get:22 http://mirrors.digitalocean.com/ubuntu focal-backports/universe Translation-en [16.3 kB] Get:23 http://mirrors.digitalocean.com/ubuntu focal-backports/universe amd64 c-n-f Metadata [880 B] Get:24 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1960 kB] Get:25 http://security.ubuntu.com/ubuntu focal-security/main Translation-en [320 kB] Get:26 http://security.ubuntu.com/ubuntu focal-security/main amd64 c-n-f Metadata [11.7 kB] Get:27 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [1463 kB] Get:28 http://security.ubuntu.com/ubuntu focal-security/restricted Translation-en [207 kB] Get:29 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 c-n-f Metadata [624 B] Get:30 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [786 kB] Get:31 http://security.ubuntu.com/ubuntu focal-security/universe Translation-en [152 kB] Get:32 http://security.ubuntu.com/ubuntu focal-security/universe amd64 c-n-f Metadata [16.9 kB] Get:33 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [22.2 kB] Get:34 http://security.ubuntu.com/ubuntu focal-security/multiverse Translation-en [5464 B] Get:35 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 c-n-f Metadata [516 B] 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. sammy@ubuntu:~$

Afterwards, update all outdated packages.

  1. sudo apt upgrade

 

If asked to continue with the upgrades, enter Y.

Once the upgrades are finished, proceed with installing the build-essential package.

  1. sudo apt install build-essential

 

To proceed with the installation, input Y when prompted. The installation is finished once your command prompt no longer displays any error messages.

Step 4 involves the creation, compilation, and execution of a test program.

Once you reach this stage, you will generate a trial program to experiment with Rust and confirm its correct functioning.

To begin, establish a few directories dedicated to storing the testing script.

  1. mkdir ~/rustprojects
  2. cd ~/rustprojects
  3. mkdir testdir
  4. cd testdir

 

To store your Rust code, utilize nano or any preferred text editor to create a file within testdir.

  1. nano test.rs

 

For all your Rust programs, ensure that you use the .rs extension.

Save the file by copying the given code into test.rs.

Provide one option to paraphrase “test.rs” natively:

– “test.rs can be regarded as an evaluation/study in the realm of computing.”

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

Use the rustc command to assemble the code.

  1. rustc test.rs

 

Execute the generated executable.

  1. ./test

 

The program outputs to the terminal.

Output

sammy@ubuntu:~/rustprojects/testdir$ ./test Congratulations! Your Rust program works. sammy@ubuntu:~/rustprojects/testdir$

Additional frequently-used Rust commands

Regularly updating your Rust installation on Ubuntu is highly advisable.

To update Rust, input the following command.

  1. rustup update

 

Additionally, you have the option to eliminate Rust from your system, along with any repositories linked to it.

To remove Rust, input the given command.

  1. rustup self uninstall

 

You are required to input Y to proceed with the uninstallation process.

Output

ammy@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)

Please press Y to proceed.

Output

Continue? (y/N) y info: removing rustup home info: removing cargo home info: removing rustup binaries info: rustup is uninstalled sammy@ubuntu:~/rustprojects/testdir$

Your system has been rid of Rust.

In summary, to conclude

After successfully installing and experimenting with Rust on Ubuntu, enhance your knowledge by exploring additional tutorials specifically designed for Ubuntu.

 

 

More  tutorials

Tutorial on how to set up a Hibernate Tomcat JNDI DataSource.(Opens in a new browser tab)

How to Set Up Git on Ubuntu 22.04(Opens in a new browser tab)

Spring Component annotation(Opens in a new browser tab)

Spring Component annotation(Opens in a new browser tab)

React Application Component Testing Integrate with Playwright(Opens in a new browser tab)

Python Compiler Error during package installation?(Opens in a new browser tab)

Leave a Reply 0

Your email address will not be published. Required fields are marked *