How to install Rust on Ubuntu Linux

Filed Under: Ubuntu
Install Rust on Ubuntu Linux

Today we’ll learn how to install Rust on Ubuntu Linux. The Rust programming language, more commonly known as rust-lang, is a powerful general-purpose programming language.

Developed by Mozilla and designed by Graydon Hoare, this programming language is syntactically similar to C++.

Built on the pillars of data safety, parallelism, and speed, Rust is used for a wide range of software development projects ranging from browser components to game engines and operating systems.

Its ability to be easily integrated seamlessly with other languages means that its user base is expanding with every passing day.

Starting with the Installation

We made this tutorial on Ubuntu 20.04 (Focal fossa) but the same commands will apply to previous versions and other Linux distributions that are based on Debian/Ubuntu.

Before we begin to install Rust on Ubuntu, we might wish to make sure that all our system repositories are up-to-date. To do this, we need to update our default repositories using the apt package management service. 

To do this, you need to open the terminal on your Ubuntu system and type the following. 

sudo apt update
Apt Update Ubuntu Focal Fossa
Apt Update Ubuntu Focal Fossa

You should see an output similar to this on your screen. Next, we will use the upgrade command in our terminal

sudo apt upgrade

Enter ‘Y’ in the command line to proceed with the operation. Now, we would have all the updated repositories when we install Rust on Ubuntu Linux.

How to Install Rust on Ubuntu

Now that we understand the Rust programming language, and have covered the prerequisites, it is time to install Rust on Ubuntu. To do so, you need to follow the following steps.

Install Rust on Ubuntu using the RustUp Script

While there are several different ways to install Rust on Linux, the most recommended method is using the rustup shell script.

This is the recommended way to install Rust on Ubuntu on the official Rust website. So this would be the method we will be using in this tutorial. To use the rustup installer, we need to enter the following command in our terminal.

curl https://sh.rustup.rs -sSf | sh
Install Rust Rustup Script
Install Rust Rustup Script

When you run this command, you should see a screen similar to the output shown above. You will be given the option to choose the type of installation you wish to choose.

If you are familiar with the rustup installer and wish to customize your installation, choose the second option. However, for the sake of this tutorial, we will simply choose the default first option and press enter. 

Install Rust Complete 1
Install Rust Complete

Now, we will configure our current shell using the following command.

source $HOME/.cargo/env

Verifying the installation

Once we install Rust on Linux, we need to verify the installation before we can start using Rust on our system.

We can verify this by asking the terminal for the version of the Rust programming language on our system. To do so, we enter this in our terminal.

rustc --version
Rust Version
Rust Version

This will fetch the version of the Rust programming language on our system. At the time of making this tutorial, the latest available stable release of Rust is version 1.43.1 as seen in the screenshot below.

Now, we will test if Rust is working properly. To do so, we will first create some directories for our Rust scripts using the following commands.

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

This will create a directory for our Rust projects and another test directory within the project directory. Now we will create a file to store our Rust code using nano or any of your preferred text editors.

nano rusttestfile.rs

Note that you must use the .rs extension for all your Rust scripts. When te file is opened, enter the following test code and save the file.

fn main() {
    println!("Congrats! Your Rust script is running");
}

Now, we will compile the code using the rustc command and then create a rust executable in the same directory. Now, let us run the executable file using the following command. If we did everything correctly, we should expect to see an output like the one above.

Run Rust App 1
Run Rust App

If your test script is working, you were successful to install Rust on Ubuntu. Now, you can proceed to work on developing software and scripts using the Rust programming language.

Other commonly used Rust commands

We have covered the steps to install Rust on Ubuntu. But there are still two commands to discuss that would be useful to you. Over regular intervals, you should update your installation of Rust on Linux.

This is important to ensure that you have access to the latest features and functionalities of rust on your system. To do so, enter the following command in your terminal.

rustup update
Rustup Updater
Rustup Updater

In the event that you no longer wish to use Rust on your system, you can choose to uninstall it. To remove Rust from your system along with its associated repositories, you need to use this command. 

rustup self uninstall
Uninstall Rust
Uninstall Rust

Conclusion

Rust is a multi-paradigm programming language. It was created as a language designed to create programs that are powerful and efficient. Its open-source nature, support from Mozilla, and a dedicated community are just a few reasons why it is being adopted by more and more developers over time.

We hope this tutorial was able to help you install Rust on Ubuntu. If you have any feedback, queries or suggestions, feel free to reach out to us in the comments below.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages