How to Install Rust on Ubuntu 24.04, 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.

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: Rust’s built-in package manager, Cargo, effortlessly manages project dependencies and build configurations.
  • 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.

Install Rust via RustUp PPA

Refreshing and Upgrading Your Ubuntu System Before Rust Installation

Before we begin 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.

Install Initial Packages Required for Rust Installation

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.

Download Rust Installation Script

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.

Run the RustUp Bash Installation 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.

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

Activating the Rust Environment

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.

Confirm Successful Rust Installation

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.

Note: If you cannot retrieve the version, the Rust environment has likely not been properly activated for your current shell session.

Create a Test Rust Application (Optional)

Create a Rust Workspace

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.

Create the Rust Source File

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.

Writing the “Hello, World!” Program with Rust

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.

Compile Test Rust Program

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.

Running the Rust Program

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.

Managing Rust On Your System

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

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.

Remove Rust

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.

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 installed 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.

Useful Links

Here are some valuable links related to using Rust:

  • Rust Official Website: Visit the official Rust website for information about the programming language, features, and download options.
  • Learn Rust: Access resources for learning Rust, including tutorials, courses, and books.
  • Play Rust: Try out Rust code directly in your browser with the online Rust playground.
  • Rust Community: Join the Rust community to connect with other developers, participate in discussions, and find support.
  • Rust Tools: Explore the various tools available for Rust development, including compilers, IDEs, and package managers.
  • Rustup: Install Rust with Rustup, the recommended installer and version manager for Rust.
Joshua James
Follow me
Latest posts by Joshua James (see all)

Leave a Comment