How to Install Rust on Ubuntu 24.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 standard installation (default - just press enter)
2) Customize installation
3) Cancel installation

It will ask for installation type. Enter 1 to proceed further with installation.

>1

info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2024-05-02, rust version 1.78.0 (9b00956e5 2024-04-29)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 15.1 MiB /  15.1 MiB (100 %)   5.1 MiB/s in  2s ETA:  0s
info: installing component 'rust-std'
 24.3 MiB /  24.3 MiB (100 %)  20.1 MiB/s in  1s ETA:  0s
info: installing component 'rustc'
 63.7 MiB /  63.7 MiB (100 %)  23.3 MiB/s in  2s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu installed - rustc 1.78.0 (9b00956e5 2024-04-29)

Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, you need to source
the corresponding env file under $HOME/.cargo.

This is usually done by running one of the following (note the leading DOT):
. "$HOME/.cargo/env"            # For sh/bash/zsh/ash/dash/pdksh
source "$HOME/.cargo/env.fish"  # For fish
root@vps:~#

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.78.0 (9b00956e5 2024-04-29)
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.78.0 (9b00956e5 2024-04-29)

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

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