How to Install Rust Programming Language on Debian 11

Rust has been adopted by hundreds of big companies in production environments. From applications like Dropbox, Firefox, and Cloudflare, to embedded devices and scalable web services, Rust can be used on all those types of applications and deployments.

rustup is a tool part of the Rust project that allows you to install Rust programming language to your system. Using rustup allows developers easily to manage and maintain Rust. Also, it enables you to switch between stable, beta, and nightly compilers and makes the cross-compiling process easier.

In this guide, you will learn how to install Rust programming language on the Debian 11 Bullseye. We will be using the rustup toolchain for installing and managing Rust on the Debian system.

Prerequisites

To complete this guide, ensure you have got the following requirements:

  • Operating System: Debian 11 Bullseye
  • Root privileges

Now let's start the installation.

Installing Packages Dependencies

First, you will be installing packages dependencies such as build-essentials, GCC, make, etc to your Debian system.

1. Run the 'sudo su' command below to get root privileges, then update and refresh your repository.

sudo su
sudo apt update

2. Now install packages dependencies using the following command.

sudo apt install curl build-essential gcc make -y

The installation will take some time, depending on your internet connection.

Installing Rust with rustup

rustup is an official project backed by Rust that allows you to install Rust most easily. rustup provides an installer for Unix-like operating systems and Windows.

In this step, you will be installing Rust programming language using the rustup on the Debian system.

1. Execute the following command to download the rustup installer and install Rust system-wide.

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

This command will download the rustup toolchain to the custom installation directory '/opt/rust'. Also, define the environment variable for 'RUSTUP_HOME' and 'CARGO_HOME' to the directory '/opt/rust'.

The installation will take some time, depending on your system spec and internet connection.

If the Rust installation completes, you will see the output message 'Rust is installed now. Great!'.

Install Rust with rustup

2. After rustup installation completes, execute the following command to add the environment variable '$RUSTUP_HOME=/opt/rust' and the binary path of rustup toolchain '/opt/rust/bin' to the '$PATH' environment variable. This will make environment variables permanent and automatically load at every login.

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

Now execute the following command to reload your current shell profile. This will apply a new shell configuration, including new environment variables.

source /etc/profile

Verify the '$RUSTUP_HOME' and '$PATH' environment variables using the following command.

echo $RUSTUP_HOME
echo $PATH

If your configuration is correct, you will see a similar output as below.

Setup RUSTUP_HOME and PATH binary environment variable

The '$RUSTUP_HOME' directory is '/opt/rust' and the rustup toolchain binary path is '/opt/rust/bin' directory.

3. To verify Rust installation, execute the following command using the following command.

rustc --version

You will see a similar output as below.

rustc 1.56.1 (59eed8a2a 2021-11-01)

Rust system-wide installation allows you to run Rust with different users, including the non-root user.

Log in as your user and check the Rust version as below.

su - username
rustc --version

You will see a similar output as below.

Verify Rustup

4. By default, rustup provides command completions for a different types of shells, including bash.

Execute the following command to generate the rustup command completion for the bash.

rustup completions bash > /usr/share/bash-completion/completions/rustup

Now reload the bash_completion profile to apply a new configuration.

source /etc/profile.d/bash_completion.sh

Now type the 'rustup' command and press 'TAB' to get the list of rustup command completions.

rustup TAB

Below is the similar output you will get.

Enable autocomplete rustup

Now you've concluded the Rust installation of the Debian system using the rustup.

Setting up Rust Project

The main advantage of installing Rust system-wide is all users can use it without any additional package installation and configuration.

For this example, you will be creating hello-world Rust using a non-root user.

Log in as your user using the following command.

su - username

1. Create a new directory '~/project' and change your working directory into it.

mkdir -p ~/project; cd ~/project

2. Create a new Rust script 'hello-world.rs' using your favorite editor.

nano hello-world.rs

Copy and paste the following Rust code for a simple hello-world message.

fn main() {
    println!("Hello World, welcome to Rust.");
}

Save the file.

3. Compile the hello-world.rs script using the 'rustc' command below.

rustc hello-world.rs

The output of the hello-world.rs will be an executable file 'hello-world'.

4. Execute the binary executable file 'hello-world' as below.

./hello-world

And you will see the 'hello-world' message below.

Rust Hello World

Managing Rust with rustup

rustup has its own configuration file 'settings.toml'. This configuration can be found under the '$RUSTUP_HOME' directory.

1. Setup default Rust toolchain using the rustup command below.

rustup default nightly

2. Setup default Rust profile. You can use minimal, default, or complete.

rustup set profile minimal

3. Compiling Rust code with a specific Rust version

rustup run nightly rustc hello-world.rs

4. Show the default toolchain that will be used in the current project directory.

rustup show

5. Show available targets for the current active toolchain. Targets are crossplatform.

rustup target list

6. Remove/uninstall specific toolchains from the current rustup environment.

rustup toolchain uninstall nightly

7. Show help message from rustup sub-command.

rustup toolchain help

Change the 'toolchain' with another sub-command.

8. Show the man page for a specific toolchain.

rustup man cargo

Now you've learned the basic rustup command for managing the Rust environment.

Uninstall Rust and rustup

In case you want to remove the Rust and rustup completely, you can just remove the installation directory '/opt/rust'.

1. Uninstall Rust and rustup completely using the following command.

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

2. Remove some additional temp directory and unused configuration files.

rm -rf ~/.cargo
rm -f /etc/profile.d/rust.sh
rm -f /usr/share/bash-completion/completions/rustup

Conclusion

Congratulation! You've successfully installed Rust programming language on the Debian 11 system using rustup. Also, you've learned the basic usage of the rustup command for managing your Rust environment.

Share this page:

5 Comment(s)