Rust has become a popular choice for many large companies in their production environments. Its versatility makes it suitable for use in applications such as Dropbox, Firefox, and Cloudflare, as well as in embedded devices and scalable web services.
rustup
is a tool developed as part of the Rust project to streamline the installation and management of the Rust programming language on your system. Utilizing rustup
simplifies switching between stable, beta, and nightly compilers and facilitates cross-compiling.
This guide will walk you through installing the Rust programming language on Debian 11 Bullseye using the rustup
toolchain.
Prerequisites
Before proceeding with the installation, ensure you have the following:
- Operating System: Debian 11 Bullseye
- Root privileges
Let’s begin the installation process.
Installing Package Dependencies
First, install the necessary package dependencies, such as build-essential, GCC, and make, on your Debian system.
Run the ‘sudo su
‘ command to gain root access, then update and refresh your repository list.
sudo su sudo apt update
Now, install the package dependencies using the following command.
sudo apt install curl build-essential gcc make -y
Please note that the installation time may vary depending on your internet connection.
Installing Rust with rustup
rustup
is the official tool for installing Rust across various operating systems, including Unix-like systems and Windows.
Here’s how to use it to install Rust on your Debian system:
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 downloads the rustup
toolchain to the specified directory, /opt/rust
, and sets the RUSTUP_HOME
and CARGO_HOME
environment variables.
Once the installation is complete, you should see the message: “Rust is installed now. Great!“.
After completing the rustup
installation, add the environment variable ‘$RUSTUP_HOME=/opt/rust‘ and the Rust toolchain binary path ‘/opt/rust/bin‘ to the ‘$PATH‘ variable to ensure permanent loading.
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
Execute the following command to reload your current shell profile, applying the new configuration:
source /etc/profile
Verify the ‘$RUSTUP_HOME‘ and ‘ $PATH‘ variables with the following commands:
echo $RUSTUP_HOME echo $PATH
If configured correctly, you should see output similar to this:
The ‘$RUSTUP_HOME‘ directory is ‘/opt/rust‘, and the Rust toolchain binary path is ‘/opt/rust/bin‘.
To verify your Rust installation, execute this command:
rustc --version
The expected output should resemble the following:
rustc 1.56.1 (59eed8a2a 2021-11-01)
With system-wide installation, Rust can be run by different users, including non-root users. Log in as a non-root user and verify Rust:
su - username rustc --version
Expected output:
Enable rustup
command completions for different shells, including Bash.
rustup completions bash > /usr/share/bash-completion/completions/rustup
Reload the bash_completion
profile to apply the new configuration:
source /etc/profile.d/bash_completion.sh
To see command completions for rustup
, type ‘rustup’ and press ‘TAB‘.
rustup TAB
The output should look like this:
Setting Up a Rust Project
One primary advantage of a system-wide Rust installation is that it allows all users to access it without additional setup.
Let’s create a simple “Hello World” Rust project as a non-root user.
Log in using:
su - username
Create a directory ‘~/project‘ and switch to it.
mkdir -p ~/project; cd ~/project
Create a Rust script named ‘hello-world.rs‘ with your preferred text editor.
nano hello-world.rs
Paste the following code:
fn main() { println!("Hello World, welcome to Rust."); }
Save and exit.
Compile the hello-world.rs script using:
rustc hello-world.rs
This will generate the executable file ‘hello-world‘.
Run the executable using:
./hello-world
The anticipated output is:
Managing Rust with rustup
The rustup
configuration file, ‘settings.toml‘, is located in the ‘$RUSTUP_HOME‘ directory.
Set the default Rust toolchain version using:
rustup default nightly
Set the default Rust profile: minimal, default, or complete.
rustup set profile minimal
Compile Rust code with a specific Rust version.
rustup run nightly rustc hello-world.rs
Show the default toolchain for the current project directory.
rustup show
List available targets for the current active toolchain for cross-compilation.
rustup target list
Remove specific toolchains from the rustup
environment.
rustup toolchain uninstall nightly
Display help for a rustup
sub-command.
rustup toolchain help
Replace ‘toolchain‘ with another sub-command as needed.
Show the man page for a specific toolchain.
rustup man cargo
You now know the basic rustup
commands for managing the Rust environment.
Uninstalling Rust and rustup
To completely remove Rust and rustup
, delete the installation directory ‘/opt/rust‘.
Use the following command to uninstall Rust and rustup
:
sudo rm -rf /opt/rust sudo rm -rf /etc/profile.d/rust.sh
Remove any additional temporary directories and unused configuration files.
rm -rf ~/.cargo rm -f /etc/profile.d/rust.sh rm -f /usr/share/bash-completion/completions/rustup
Conclusion
Congratulations! You have successfully installed the Rust programming language on Debian 11 using rustup
, and you have learned the basics of managing your Rust environment using rustup
commands.
Frequently Asked Questions (FAQ)
1. Why use rustup for installing Rust?
rustup
is a versatile tool that simplifies installing and managing multiple Rust toolchains. It allows easy switching between stable, beta, and nightly versions and facilitates cross-compilation.
2. Can I install Rust without root privileges?
Yes, you can install Rust in a user-local setup without root privileges by following the standard rustup
installation, which defaults to installing in user directories.
3. How often should I update my Rust installation?
The Rust community releases updates frequently, often every six weeks. It’s a good practice to update regularly to benefit from the latest features and improvements.
4. Can I have multiple Rust toolchains installed simultaneously?
Yes, rustup
allows you to install and manage multiple toolchains simultaneously, enabling you to compile projects with different Rust versions easily.
5. How do I switch between different Rust toolchains?
Use rustup default
followed by the toolchain name, such as rustup default stable
, to switch to the stable toolchain. Similarly, you can use rustup default nightly
for the nightly toolchain.