Logo

How to Install Rust on Ubuntu 20.04/22.04

Try this guide with our instant dedicated server for as low as 40 Euros

install rust on ubuntu

Rust is an increasingly popular programming language known for its safety and performance. Developers particularly favor it for system-level projects and developing performance-critical applications. 

Since Ubuntu is the preferred operating system for software development, installing Rust on Ubuntu is often the prerequisite to developing Rust applications. 

So, in this tutorial, we will go into the details of installing Rust on Ubuntu using two different approaches. We will then look into the idea of uninstalling Rust from your Ubuntu systems. Finally, we will see how you can test Rust installation on the system by running a test program.

But first, let’s start with the prerequisites for this process.

Table Of Contents

  1. The Prerequisites
    1. Install Rust on Ubuntu
    2. Option #1: Install Rust on Ubuntu 20.04/22.04 Using apt
    3. Option #2: Install Rust on Ubuntu Using rustup
  2. Create a Test Program in Rust
    1. Step #1: Set Up Your Rust Environment
    2. Step #2: Create a New Rust Project
    3. Step #3: Write Your Test Code
  3. Compile Your Rust Program
    1. Step #1: Use Cargo to Build Your Project
    2. Step #2: Check for Compilation Errors
  4. Run Your Test Program in Rust
    1. Step #1: Run Your Program Using Cargo
    2. Step #2: Verify the Output
  5. Uninstall Rust on Ubuntu
    1. Uninstall Rust Using apt
    2. Uninstall Rust Using rustup
  6. Cargo and Rust Commands
  7. Conclusion
  8. FAQs

The Prerequisites

Before installing Rust on Ubuntu, ensure you have the following:

  • A system running a recent stable Ubuntu version.
  • A user account with root or sudo rights. 

Install Rust on Ubuntu

We recommend the following two methods of installing Rust on the Ubuntu system:

Option #1: Install Rust on Ubuntu 20.04/22.04 Using apt

The first method is to use the standard Ubuntu package installation process. Here are the steps in this process.

Step #1: Update the Package Registry

Updating your package registry ensures you have the latest package versions. For this, open your terminal and execute the following command:

# sudo apt update

sudo apt update

Step #2: Install Rust

After updating the package list, install Rust by running the following command in the terminal:

# sudo apt install rustc

sudo apt install rustc

Step #3: Verify the Installation

Once the installation process is complete, you can verify the installation by checking its version by running the following command:

# rustc --version

rustc --version

This command will display the installed version of Rust, confirming that the installation was successful.

Option #2: Install Rust on Ubuntu Using rustup

The second method involves using the official Rust installer. For this, follow these steps. 

Step #1: Download rustup

rustup is the official installer for managing Rust versions and associated tools. We recommend running the following command to download rustup using cURL

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

This script will download and start the installation process for rustup.

curl --proto

rust install confirmation

Step #2: Add Rust to PATH

After installation, we strongly recommend adding Rust to the system’s PATH (path environment variable). This allows you to run Rust’s tools globally. If rustup doesn’t automatically add it, you can manually add Rust to the system’s PATH by executing the following command:

# source $HOME/.cargo/env

source $HOME

Step #3: Verify the Installation

Similar to the previous apt method, verify the installation by using the rustup tools:

# rustc --version

If Rust has been successfully installed, this command will return the version information.

rust --version 2

Create a Test Program in Rust

Now that you have installed Rust on your system, we recommend that you create and run a test program to essentially test the installation.

Step #1: Set Up Your Rust Environment

Ensure you have Rust installed on your computer. You can install Rust using rustup, Rust’s official command-line tool for managing Rust versions and associated tools.

Step #2: Create a New Rust Project

Run the following command to create a new Rust project:

# cargo new rust_test_project

This command will create a new directory named rust_test_project with a basic Rust project structure.

Step #3: Write Your Test Code

Navigate to the src directory of your new project. Open the main.rs file, which is the entry point of your Rust program. Write a simple function to test the Rust installation. For instance, we will write a function that adds two numbers:

fn add(a: i32, b: i32) -> i32 {

    a + b

}

fn main() {

    println!("The sum of 3 and 4 is {}", add(3, 4));

}

test code

Compile Your Rust Program

Once you have written the code, the next step is to compile it into an executable format.

Step #1: Use Cargo to Build Your Project

Cargo is Rust’s build system and package manager, which helps in compiling and managing your Rust projects. 

In your project directory, run the following command:

# cargo build

This command will compile your project and create an executable file in the target/debug directory.

cargo build

Step #2: Check for Compilation Errors

If there are any errors in your code, the compiler will output them during the build process. Fix any errors before proceeding to the next stage.

Run Your Test Program in Rust

At this point, you are ready to run the compiled Rust program.

Step #1: Run Your Program Using Cargo

With your project compiled, you can now run it using Cargo:

# cargo run

This command will compile (if necessary) and run the Rust program.

Step 2: Verify the Output

Ensure the output of your program is as expected. In our example, the program should output The sum of 3 and 4 is 7.

cargo run

Uninstall Rust on Ubuntu

You may need to uninstall Rust from Ubuntu for various reasons, such as upgrading to a newer version, troubleshooting, or simply freeing up space. 

We will now describe two methods of uninstalling Rust from your Ubuntu system.

Uninstall Rust Using apt

If you installed Rust using apt, we recommend the following steps for uninstalling it.

Step #1: Remove the Rust Package

If you installed Rust using the apt package manager, you could remove it by executing the following command in your terminal:

# sudo apt remove rustc

Step #2: Clean Up the Dependencies

After removing Rust, you might want to remove unused packages and dependencies to free up space. Run the following command to clean up your system:

# sudo apt autoremove

Uninstall Rust Using rustup

rustup provides a native uninstall process that we will demonstrate in the following steps:

Step #1: Run the rustup Uninstall Command

If you installed Rust with rustup, you should use it to uninstall Rust. Execute the following command in your terminal:

# rustup self uninstall

Step #2: Confirm the Uninstallation

The rustup uninstall command will prompt you to confirm the uninstallation process. Type y and press Enter to proceed with the removal of Rust.

rustup self uninstall

Cargo and Rust Commands

Cargo Build

  • Purpose: Compiles the Rust project and produces an executable in the target directory.
  • Command:

    cargo build

Cargo Run

  • Purpose: Compiles the Rust project (if necessary) and runs the executable.
  • Command:

    cargo run

Cargo Test

  • Purpose: Run tests defined in the project.
  • Command:

    cargo test

Cargo Check

  • Purpose: Quickly checks your code to ensure it compiles without producing an executable, speeding up the development process.
  • Command:

    cargo check

Cargo Clean

  • Purpose: Removes the target directory, clearing the compiled artifacts and freeing up space.
  • Command:

    cargo clean

Cargo Update

  • Purpose: Updates the project’s dependencies as listed in the cargo.lock file.
  • Command:

    cargo update

Cargo Doc

  • Purpose: Generates documentation for the current project’s dependencies.
  • Command:
     cargo doc
  • Add –open to automatically open the generated documentation in a web browser:

    cargo doc --open

Rustup Update

  • Purpose: Updates the Rust toolchain to the latest version.
  • Command:

    rustup update

Rustfmt

  • Purpose: Formats Rust code according to style guidelines.
  • Command:

    cargo fmt

Clippy

  • Purpose: A collection of linting components to catch common mistakes and improve your Rust code.
  • Command:
     
    cargo clippy

Conclusion

Installing Rust on Ubuntu can be done efficiently using either apt or rustup. Choose the method that best suits your needs and follow the steps outlined above. Remember, having the right tools is crucial whether you’re developing software or setting up servers. 

The success of a Rust project or application depends in most part on the hosting solution you opt for your project. If you’re looking for a robust server for your Rust projects, we offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. How to install Rust programming language on Ubuntu 20.04/22.04?

To install Rust programming language on Ubuntu 20.04/22.04, you can use the rustup toolchain installer. This allows you to easily manage your Rust installations and toolchains.

Q. Can Rust be installed on Ubuntu using apt?

Rust can be installed directly via apt on Ubuntu but it is recommended to use the rustup installer for managing Rust installations.

Q. How to uninstall Rust on Ubuntu?

To uninstall Rust on Ubuntu, you can remove it using the rustup tool. Simply run the command ‘rustup self uninstall’ in your terminal to remove Rust from your system.

Q. What is rustup and how is it used in Rust installation?

rustup is an installer for Rust programming language that allows you to easily manage multiple Rust toolchains. It is recommended for installing and managing Rust on your system.

Q. What is Cargo in Rust and how to manage it?

Cargo is a package manager and build system for Rust programming language. It comes bundled with the Rust installation and can be managed using environmental variables like CARGO_HOME.

Try this guide with our instant dedicated server for as low as 40 Euros