How to Install Rust on Ubuntu 22.04?


Rust is a robust programming language inspired by C++ but has an execution and optimization edge over C++. Developers have been using it or aiming to use it for Web and Game Development, Network and System Programming, etc. The practical implication of Rust is foreseen in the IoT, Embedded Systems, Industrial Automation, Machine Learning, and more.

It is on the record that Rust has been one of the admired and beloved programming languages in the recent two years (as per the Stack Overflow developer survey 2022 and 2023).

The graph shows that 84.66% (most among all the languages) of the developers admired Rust.

Inspired by its importance, this post will address the suitable methods to install and run/use Rust on Ubuntu 22.04.

How to Install Rust on Ubuntu 22.04?

Rust is available on Ubuntu in the official repositories of Ubuntu and the rustup The rustup is referred to as the Rust manager and is used to install, update, or uninstall, Rust on Ubuntu. Let’s get into the installation methods one by one:

Install Rust From the Ubuntu Official Repositories

Ubuntu repositories carry the Rust compiler named “rustc”. As we are installing from the official repositories, it is recommended to first load the updated packages via the command:

sudo apt update

Now, install the Rust support using the command:

sudo apt install rustc

Verify the installation using the command:

rustc --version

Install Rust Using the rustup

The rustup is the toolchain installer for the Rust programming language. A toolchain comprises a set of utilities, that are used by the Rust language to manage the programming operations. The rustup support is available through the shell script and the snap store. Let’s get into both methods:

    1. Through the Shell Script

A curl script is available which can be downloaded and run to install the Rust language. For that, you must have curl installed, if not use the command below to install curl first:

sudo apt install curl

Step 1: Download/Execute the Script

Now, use the following command to download as well as execute the script to install the rust:

curl -sSf https://sh.rustup.rs | sh

During the execution, the rustup starts configuring your system for the Rust language. Once the Rust is installed, it shows the version on the screen:

Moreover, it shows that we need to either source the “~/.cargo/env” directory or restart the shell to start using Rust.

Step 2: Configure or Configure the Shell

To start using the Rust, you have to restart the shell or configure the current one using the command(s):

  • source ~/.cargo/env: The system acknowledges all the environment variables in the cargo directory.
  • source ~/.profile (Optional): To refresh the current shell and acknowledge all the variables.
source ~/.cargo/env
source ~/.profile

Now, verify the installation by checking the compiler version:

rustc --version

    1. Through the Snap

The snap store contains the rustup tool. You can install the rustup tool and then use it to install/manage the Rust compiler and other components for the Rust language. Let’s see how it can be done:

Step 1: Install the rustup

sudo snap install rustup --classic

Note: If you do not have snap support, you can install it via the command, “sudo apt install snapd”.

Step 2: Install Rust

Use the following command with the version number to install Rust on Ubuntu, i.e., 1.75 in this case:

rustup install <version-number>

Note: Go to the Following Release Page to check the versions of Rust that you can install with the above command.

How to Use/Run Rust on Ubuntu 22.04?

As an example, we will create a sample Rust script on Ubuntu 22.04 and run it from the terminal. Let’s first create a “.rs” file, as we did here:

nano linuxgenie.rs

A simple print statement is used in the Rust syntax as:

Compile the script using the Rust compiler:

rustc linuxgenie.rs

The compilation will create a file without “.rs”, i.e., linuxgenie. Now, grant the executable permissions to the file:

sudo chmod +x linuxgenie

Finally, run the file from the terminal (if the file is not at the PWD, use the exact path of the file):

./linuxgenie

That’s how you can execute the Rust code.

Troubleshooting Tip: How to Fix the Syntax Error Near Unexpected Token?

While executing the code placed in the file, you might encounter the “syntax error near unexpected token” as shown below. This error occurs when you are trying to execute the uncompiled file (a file with a “.rs” extension).

When the file is compiled, the rustc creates another file (with the same name but no extension). After the file is compiled, you need to do the following two steps to get rid of the syntax error:

  • Make the compiled script executable using the “chmod” command.
  • Execute that compiled file (the file without the “.rs” extension).

That’s how you can fix the “syntax error near unexpected token” in Rust.

How to Uninstall/Remove Rust From Ubuntu 22.04?

The removal of Rust depends on the method you followed for installation, i.e., the official repositories (using apt), and the rustup (script and snap). All the possible methods are listed below:

If Installed From the apt

If you have just installed the compiler support of Rust via official repositories, you can remove it easily via the command:

sudo apt autoremove rustc

If Installed Using the rustup

As discussed rustup is managed through the script and the Snap store. The quick way is to remove the rustup tool itself which will automatically remove the rustc support from your Ubuntu 22.04. There are two possibilities to remove the Rust support in this case:

  • Remove/Uninstall the Rust (Installed Via the Script)
rustup self uninstall

The cargo and the rustup both are removed from the system, which ultimately removes the rustc as well.

  • Remove/Uninstall the rustup
sudo snap remove rustup

Bonus Tip: How to Manage Rust on Ubuntu 22.04?

The rustup can also be used to manage the Rust versions on Ubuntu 22.04. You can install, update, and uninstall various versions of Rust. Let’s see the commands to do so:

Install Specific Rust Version

The command below installs any specific version that the user wants. This is helpful when you have programs that support different versions of Rust. First, get the list of supported versions from GitHub’s Release Page and use the below command to install:

rustup install <Version-Number>

Update the Rust and rustup

First, check the updates for the Rust versions via the command:

rustup check

The below command updates all the Rust versions (and the rustup) installed:

rustup update

Update (or check for the updates) the specific Rust version:

rustup update <Version-Number>

Set the Default Rust

The first installed version is considered as the default version. You can check the default version using the command:

rustup show

However, if you want to set the default version other than that, you can use the command as follows:

rustup default <version-number>

Use the Specific Rust Version For One Session

If you want to run a specific command (Rust script) with the specific Rust version, use the below command (Only applicable for the one command):

rustup run <Version> <Command>

Use Specific rustup Profile

The rustup comes with three different profiles, i.e., minimal (three basic components), default (three basic plus three additional), and complete (all the Rust components). You can set the profile via the command (use the minimal, default, or complete in the command):

rustup set profile <minimal|default|complete>
rustup show profile

Note: For more details on rustup usage, you can get help using the command “rustup –help”.

Bottom Line

Rust, being a widely admired programming language, is used by developers across Ubuntu and other Linux distros. On Ubuntu, Rust support is available through the official repositories and the rustup tool. The Rust compiler installed from Ubuntu’s repositories offers a bit lower version than the one installed through the rustup tool.

You have learned the potential installation methods of Rust and its components on Ubuntu 22.04.

 

Print Friendly, PDF & Email
Categories