How to Install Rust on Ubuntu 22.04 or 20.04

Venturing into systems programming, Rust offers a compelling blend of performance and safety. Developed by Mozilla Research, Rust is a systems programming language that seamlessly merges low-level programming capabilities with the assurances and ease of high-level languages. This article will guide you on how to install Rust on Ubuntu 22.04 Jammy Jellyfish or the older stable release of Ubuntu 20.04 Focal Fossa.

Key Highlights of Rust:

  • Memory Safety: Rust’s unique ownership system, checked during compile-time, ensures memory safety without needing a garbage collector. This eliminates common bugs like null pointers or double frees.
  • Zero-Cost Abstractions: Rust offers the advantage of high-level abstractions without any performance trade-offs, a feature not always present in other languages.
  • Concurrency: Rust’s inherent support for concurrent and parallel programming ensures thread safety, mitigating common concurrency issues.
  • C Interoperability: With its foreign function interface, Rust can seamlessly integrate with C code, allowing the use of existing C libraries.
  • Cargo Package Manager: Managing project dependencies and build configurations becomes effortless with Rust’s built-in package manager, Cargo.
  • Documentation Focus: Rust emphasizes comprehensive documentation. The integrated Rustdoc tool facilitates the creation of detailed software documentation.

While powerful, Rust’s rich feature set comes with a learning curve. But the rewards, in terms of performance and safety, are well worth the effort. As we proceed, we’ll detail the steps to get Rust up and running on your Ubuntu system, leveraging the official Rust APT repository. Prepare to harness the capabilities of Rust on your Ubuntu platform.

Install Rust on Ubuntu 22.04 or 20.04

Step 1: Refreshing and Upgrading Your Ubuntu System Before Rust Installation

Before we embark on the installation of the Rust programming language, it’s crucial that we first update our Ubuntu system. This ensures all existing packages are up-to-date, thus minimizing potential conflicts during the Rust installation. To accomplish this, execute the following command in your terminal:

sudo apt update

Next, we upgrade any outdated packages on our system using the command below. Upgrading ensures we have the latest features and security fixes for our installed packages:

sudo apt upgrade

A system reboot is recommended if this process results in many updates, particularly involving the Linux Kernel. This ensures the system correctly utilizes the updated components.

Step 2: Install Initial Packages Required for Rust Installation on Ubuntu

Now that our system is up-to-date, we must install the essential dependencies to install and run Rust successfully. To do this, execute the following command, which will install the necessary packages:

sudo apt install curl build-essential gcc make -y

The packages installed by this command are frequently used in various software installations, providing tools for building and compiling software.

Step 3: Download Rust Installation Script on Ubuntu

With the prerequisites handled, we’re ready to proceed with the Rust installation. Using the curl command, we can download the Rust installation script from the official Rust repository. Execute the following command to do this:

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

This command fetches the installation script and pipes it to the sh command for execution, initiating the Rust installation process.

Step 4: Install Rust on Ubuntu 22.04 or 20.04 via Bash Script

After downloading and executing the Rust installation script, you’ll be prompted. This prompt provides an option to proceed with the default installation or customize the installation parameters.

To continue with the default installation, type 1 and press Enter. If you prefer to customize the installation, follow the provided instructions.

Screenshot of the Rust installation options prompt on Ubuntu 22.04 or 20.04 Linux.
User choice selection during Rust installation on Ubuntu 22.04 or 20.04 Linux.

The installation time will vary based on your server’s internet speed and hardware capabilities, typically ranging between 1 to 5 minutes.

Step 5: Activating the Rust Environment on Ubuntu 22.04 or 20.04

Once the Rust installation is complete, we must activate the Rust environment for our current shell session. The Rust environment contains a set of variables and settings necessary for adequately using Rust. To activate it, execute the following command:

source "$HOME/.cargo/env"

This command loads the Rust environment into your current shell session, readying it for Rust development.

Step 6: Confirm Successful Rust Install on Ubuntu

Lastly, let’s ensure our Rust installation was successful by checking the installed version of the Rust compiler (rustc). Execute the following command to do this:

rustc -V

If the Rust environment was correctly activated and Rust was successfully installed, this command should print the version of rustc to your terminal.

If you cannot retrieve the version, it’s likely the Rust environment hasn’t been properly activated for your current shell session.

Create a Test Rust Application on Ubuntu 22.04 or 20.04

Step 1: Create a Rust Workspace on Ubuntu

Now that we have our Rust environment, it’s time to validate the installation by creating a simple Rust application. Often in programming, the first application one creates in a new language is a “Hello, World!” program, and that’s exactly what we’ll do.

First, we must create a directory for Rust projects as our workspace. In the terminal, type in the following command:

mkdir ~/rust-projects

This command creates a directory named rust-projects in your home directory, which will hold all our upcoming Rust creations.

Step 2: Create the Rust Source File on Ubuntu

Next, we navigate to our new rust-projects directory and create a new Rust source file named helloworld.rs. We’ll do this using nano, a convenient terminal-based text editor. Execute the following command to accomplish this:

cd rust-projects && nano helloworld.rs

The cd command navigates to our rust-projects directory, and nano helloworld.rs opens a new file named helloworld.rs in the nano text editor.

Step 3: Writing the “Hello, World!” Program with Rust on Ubuntu

In the nano text editor, type in the following Rust code:

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

This simple Rust program contains a main function (main()), which is the entry point for all Rust programs. Within this function, we call println!(), a Rust macro that prints text to the console. Here, we’re printing the string “Hello World; this is a test provided by LinuxCapable.com.”

To save the file in nano, press Ctrl+O, then Enter to confirm. To exit nano, press Ctrl+X.

Step 4: Compile Test Rust Program on Ubuntu

With our Rust program written, it’s time to compile it. Rust’s compiler is rustc, which we’ll use to compile our helloworld.rs program. In the terminal, type the following command:

rustc helloworld.rs

This command invokes the rustc compiler on our helloworld.rs file, which, upon successful compilation, will produce an executable file in the current directory.

Step 5: Running the Rust Program on Ubuntu

Now, it’s time for the grand reveal. To run our newly compiled Rust program, use the following command:

./helloworld

This command executes the helloworld binary, resulting in the output of our println!() macro, displaying our “Hello World, this is a test provided by LinuxCapable.com” message to the console. If you see this message, congratulations! You’ve successfully written and run your first Rust program.

Screenshot of a "Hello World" test in Rust on Ubuntu 22.04 or 20.04 Linux.
Demonstration of a working Rust application, confirming successful installation on Ubuntu 22.04 or 20.04 Linux.

Managing Rust on Ubuntu 22.04 or 20.04

After successfully installing Rust and crafting your first application, knowing how to manage Rust on your Ubuntu system is essential. This involves knowing how to update and remove (uninstall) Rust when necessary. Let’s delve into how you can perform these actions.

Update Rust on Ubuntu 22.04 or 20.04

Rust comes with an in-built updater, making updating to the latest version of Rust a straightforward process. To do this, open your terminal and execute the following command:

rustup update

This command prompts Rust’s version management tool, rustup, to check for the latest stable release of Rust and update your installed version if a newer one is available.

Terminal output screenshot showing the process of updating Rust on Ubuntu 22.04 or 20.04 Linux.
Step-by-step terminal output for updating Rust on Ubuntu 22.04 or 20.04 Linux.

Remove Rust on Ubuntu 22.04 or 20.04

There may come a time when you no longer require Rust on your Ubuntu system. In this case, you can use rustup to easily uninstall Rust. Here’s how:

To remove the Rust programming language along with all its related data and toolchains, execute the following command in your terminal:

rustup self uninstall

Upon running this command, a message will appear in your terminal, prompting you to confirm your decision to uninstall Rust. It reads as follows:

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

Continue? (y/N)

If you’re sure you want to uninstall Rust, type Y and press Enter to proceed.

Terminal output screenshot detailing the process to remove Rust from Ubuntu 22.04 or 20.04 Linux.
Terminal commands and output for removing Rust from Ubuntu 22.04 or 20.04 Linux.

And that’s it! Rust should now be successfully removed from your Ubuntu system. Whether you’re just starting with Rust or already have a few projects, knowing how to manage your Rust installation effectively is essential to your Rust journey.

Conclusion

In conclusion, we’ve successfully walked through the process of installing the Rust programming language on a Ubuntu Linux distribution. We’ve covered the initial setup steps, including updating your Ubuntu system and installing the necessary dependencies. We then downloaded and executed the Rust installation script, verified the installation, and even created a simple Rust application for demonstration purposes. Finally, we discussed managing your Rust installation effectively, including the necessary steps to update or remove Rust from your Ubuntu system.

Leave a Comment