Install Rust on Ubuntu 20.04

Install Rust on Ubuntu 20.04

Rust is an open-source, high-level, general-purpose programming language. Rust does not have garbage collection, guarantees memory-safety and thread-safety.

This tutorial explains how to install Rust on Ubuntu 20.04.

Prepare environment

Before running a Rust program, you must compile it. Build tools are required for compiliation. So make sure you have installed build-essential package in your system.

sudo apt update
sudo apt install -y build-essential

Install Rust

Run the following command to download and install Rust:

wget -qO - https://sh.rustup.rs | sudo RUSTUP_HOME=/opt/rust CARGO_HOME=/opt/rust sh -s -- --no-modify-path -y

By using RUSTUP_HOME environment variable specify root directory of Rustup. It is a Rust toolchain installer. You also need to add /opt/rust/bin directory to the PATH environment variable.

echo 'export RUSTUP_HOME=/opt/rust' | sudo tee -a /etc/profile.d/rust.sh
echo 'export PATH=$PATH:/opt/rust/bin' | sudo tee -a /etc/profile.d/rust.sh

To make changes to take effect logout and login to your machine or execute the following command to apply the changes immediately:

source /etc/profile

Now all commands related with Rust can be used for all users as a system-wide commands.

We can check Rust version:

rustc --version

Testing Rust

Create a main.rs file:

nano main.rs

Add the following code:

fn main() {
    println!("Hello world");
}

Compile a code:

rustc main.rs -o test

Run a program:

./test

Uninstall Rust

If you want to completely remove Rust, delete the installation directory:

sudo rm -rf /opt/rust

Remove rust.sh file that is used to set environment variables:

sudo rm -rf /etc/profile.d/rust.sh

Remove cache files:

rm -rf ~/.cargo

For environment preparation we needed to install build tools. You can remove them with command:

sudo apt purge --autoremove -y build-essential cpp make binutils

Leave a Comment

Cancel reply

Your email address will not be published.