How to Install Rust on Ubuntu 20.04 LTS

Rust is an open-source systems programming language that focuses on speed, memory safety, and parallelism. Developers use Rust to create a wide range of new software applications, such as game engines, operating systems, file systems, browser components, and simulation engines for virtual reality. Rust is syntactically similar to C++ but can guarantee memory safety by using a borrow checker for validating references.

For users and especially developers wanting to try out the Rust Programming language, at the end of this tutorial, you will know how to install Rust on Ubuntu 20.04 LTS Focal Fossa using the command line terminal and official Rust repository, along with how to maintain and remove using their commands and scripts.

Update Ubuntu

Firstly, ensure that your system is updated before proceeding to ensure no conflicts occur during the installation and for good system maintenance for the newer users to CLI.

sudo apt update && sudo apt upgrade -y

Install Required Packages

Next, install the required packages needed during the installation of Rust. Please run the following command to install or to ensure they are present.

sudo apt install curl build-essential gcc make -y

Install Rust Programming Language

Once you have completed the required installation of packages in prerequisites, you now can use (curl) to download the Rust installation script by executing the following command:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

You should see an output like the example below. Type 1 and hit Enter to continue.

how to install rust on ubuntu 20.04

The entire installation should take between 1 to 5 minutes, depending on your server’s internet speed and hardware. Once completed, you will see the following outcome:

how to install rust on ubuntu 20.04

Verify the version build of Rust installed, which will show you it is successfully installed as well. Type the following command and hit enter:

rustc --version

Example output (This will change in time with newer versions):

how to install rust on ubuntu 20.04

Note, you will need to activate the (Rust environment) for your current shell. This is done using the following command to activate the rust environment:

source ~/.profile
source ~/.cargo/env

Create Rust Sample Project Application

So you have installed Rust and believe it should be working correctly. When installing a programming language on your operating system, the best way to verify is to create a quick test application. For the tutorial, you will create the famous (Hello World) output using rust.

First, you need to create a directory that will serve as a (Workspace).

mkdir ~/rust-projects

Secondly, change the directory to the Workspace and create a sample application with the following command:

cd rust-projects && nano helloworld.rs

Next, enter the following code for the hello world test.

fn main() {
    println!("Hello World, this is a test provided by LinuxCapable.com");
}

Save and close (CTRL+O) and then exit (CTRL+X), then compile the program with the following command:

rustc helloworld.rs

This will create an executable application after it has finished compiling. The application will be in your current directory as the example output below.

how to install rust on ubuntu 20.04

To run the application you created using Rust, run the program with the execute command:

./helloworld

Example output from the test application as below:

how to install rust on ubuntu 20.04

How to Update/Upgrade Rust

For the update and upgrade of Rust, you need to use the command line terminal. Use the following command to cover the checking and initialize the update process.

rustup update

Example output:

how to install rust on ubuntu 20.04

How to Remove (Uninstall) Rust

If in the future you no longer require Rust on your Ubuntu operating system, run the following command:

rustup self uninstall

Example output:

how to install rust on ubuntu 20.04

As prompted, type Y to proceed with the removal.

Once done, you will get a similar terminal result as below.

how to install rust on ubuntu 20.04

Comments and Conclusion

In the guide, you have learned how to install Rust programming language on Ubuntu 20.04 LTS Focal Fossa and how to create a basic test application.

Overall, Rust is fantastic, especially in that it guarantees memory safety. You can’t write buffer overflows, dangling pointers, or double-free bugs in Rust which, instead of C/C++, eliminates a whole class of security bugs in your software.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!