How to Install Rust on Ubuntu 22.04

Rust is a multi-paradigm, general-purpose programming language that emphasizes performance, type safety, and concurrency. It enforces memory safety—ensuring that all references point to valid memory—without requiring the use of a garbage collector or reference counting present in other memory-safe languages.

Install Rust

Install the Rust using rustup command line tool,

Run the below command to download the rustup command,

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

Output:

root@vps:~# curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /root/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  /root/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /root/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /root/.profile
  /root/.bashrc

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:

   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>
info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu

Run the below command to add the Rust toolchain directory to the PATH environment variable,

source "$HOME/.cargo/env"

Verify the Installation

Verify the Rust installation by requesting the version,

rustc --version

Output:

root@vps:~# rustc --version
rustc 1.71.1 (eb26296b5 2023-08-03)
root@vps:~# 

Installing a Compiler

First, update the Apt package index using below command,

apt update

apt upgrade 

Once the upgrades are complete, install the build-essential package using the below command,

apt install build-essential

Creating, Compiling, and Running a Test Program, Start by creating some directories to store the test script,

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

Create a file in testdir to store your Rust code,

nano test.rs

Note: You need to use the .rs extension for all your Rust programs.

Add the following code into test.rs and save the file,

fn main() {
    println!("Congratulations! You have installed your Rust program and it works.");
}

Compile the code using the rustc command,

rustc test.rs

Run the resulting executable,

./test

Output:

root@vps:~/rustprojects/testdir# ./test
Congratulations! You have installed your Rust program and it works.
root@vps:~/rustprojects/testdir# 

Use the below command to update the rustup,

rustup update

Output:

root@vps:~/rustprojects/testdir# rustup update

info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: checking for self-update

  stable-x86_64-unknown-linux-gnu unchanged - rustc 1.71.1 (eb26296b5 2023-08-03)

info: cleaning up downloads & tmp directories
root@vps:~/rustprojects/testdir# 

Done, you’ve installed and tested out Rust on Ubuntu 22.04.