How to Install Rust on Debian 12

Choose a different version or distribution

Introduction

Before we begin talking about how to install Rust on Debian 12, let's briefly understand – What is Rust?

Rust is a modern, powerful, and safe programming language. It is designed to address the challenges faced by developers when creating reliable software. Rust's key features include memory safety, concurrency, and zero-cost abstractions. With its syntax resembling C++, Rust offers better memory management, preventing bugs like null pointer exceptions.

Moreover, its built-in concurrency mechanisms facilitate efficient parallel programming. Rust's popularity is rising due to its robustness, making it an ideal choice for system and web development.

In this tutorial, you will install Rust on Debian 12. We will also address a few FAQs on how to install Rust on Debian 12.

Advantages of Rust

  1. Memory Safety: Rust ensures that your code is free from memory vulnerabilities like null pointer errors, dangling pointers, and buffer overflows.
  2. Concurrency: Rust makes it easier to write efficient, safe, and concurrent code, enabling developers to build highly performant applications.
  3. Zero-cost Abstractions: Rust's abstractions come with no runtime overhead, allowing developers to write high-level code without sacrificing performance.
  4. Robustness: Rust's strict compile-time checks catch bugs early, preventing crashes and security vulnerabilities in production code.
  5. Growing Community: Rust has a thriving community of developers who actively contribute libraries, frameworks, and resources, making it easier to build projects quickly and efficiently.

Install Rust on Debian 12 via APT PPA

Step 1: Updating the packages

Update to make sure all installed packages are current before beginning the Rust installation. By doing this, compatibility is guaranteed and possible problems with installation are avoided. Utilize the subsequent command to upgrade your system:

sudo apt update && sudo apt upgrade

Step 2: Install initial required installation packages for Rust

Installing a few necessary packages is the next step. These packages are needed for the Rust installation. These packages contain curl, build-essential, gcc, and make among other necessary build tools. Use these packages to be installed by running the following command:

sudo apt install curl build-essential gcc make

Step 3: Install Rust via cURL and bash on Debian

Once the necessary packages are installed, you can use the curl command on your system to install Rust on Debian. curl will be used to download the Rust installation script, which will then run automatically.

To download and execute the Rust installation script, type the following command:

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

During the installation process, you will be asked to select an installation option. To continue with the default installation, type 1 and press Enter.

The installation procedure may require one to five minutes, contingent upon the hardware and internet speed of your server. You'll see a confirmation message after the installation is finished.

Step 4: Activate Rust environment on Debian

The Rust environment for your current shell session needs to be activated next. This lets you use commands and tools specific to Rust. Use the following commands to launch the Rust environment:

source ~/.cargo/env

Step 5: Confirm Rust installation

Lastly, you can verify that Rust has been installed successfully on your system by looking up the version of the Rust compiler (rustc). Press Enter after entering the following command to see the version of the Rust compiler:

rustc -V

Create a Test Rust Application on Debian 12

Testing the installation is crucial after installing Rust to make sure everything is operating as it should. The best method to confirm this is to write a basic test program, similar to the well-known "Hello World" application.

Step 1: Create a workspace directory

To begin with, make a directory that will be your Rust project workspace. This makes your projects more accessible and organized. To create a new directory named rust-projects in your home folder, run the following command:

mkdir ~/rust-projects

Step 2: Create a sample application in Rust

Next, use a text editor such as nano to create a new Rust source file called helloworld.rs in the newly created rust-projects directory. To open the file in nano, execute the following command:

cd rust-projects && nano helloworld.rs

Now, for the "Hello World" test, enter the following Rust code:

fn main() {
    println!("Hello World.");
}

You can exit nano by pressing CTRL+X after saving the changes by pressing CTRL+O.

Step 3: Compile Rust test program

Now that the source file has been created, use rustc, the Rust compiler, to compile the program. To compile the helloworld.rs file, execute the following command:

rustc helloworld.rs

When compilation is finished, this command will create an executable file called helloworld in your current directory. Both the source file and the compiled executable should now be present in the directory, as the following example output illustrates:

helloworld  helloworld.rs

Step 4: Run the test application

Lastly, run the following command to launch the "Hello World" application you made with Rust:

./helloworld

The test application's output should appear as follows when you view it:

Output

Hello World.

If you get the output shown above, your installation of Rust is successful, and you can use your system to create Rust applications.

Additional Commands Rust Commands on Debian 12

The commands and advice in this section will help you manage your Rust installation, including updating and uninstalling Rust.

Update Rust on Debian

It's crucial to maintain an updated Rust installation if you want to take advantage of the newest features, performance boosts, and bug fixes. Rust updates are simple to perform and only require one terminal command. Run the following command to update Rust:

rustup update

This command will automatically install any updates that are found and check for new ones. The command will let you know if your Rust installation is already current.

Remove (Uninstall) Rust From Debian

Rust can be easily removed from your Debian system with the rustup tool if it is no longer needed. You can use the following command to remove Rust:

rustup self uninstall

Rust and its related components will be eliminated from your system by running this command.

💡
The projects, source files, and executables you've made with Rust remain intact; this command merely uninstalls Rust.

FAQs to Install Rust on Debian 12

Does Rust require any specific dependencies on Debian 12? 

Yes, Rust requires some dependencies like build-essential, curl, and file to be installed. These dependencies can be installed using the package manager apt with the command sudo apt install build-essential curl file.

How do I add Rust's binaries to my PATH? 

Rust's binaries are automatically added to the PATH during installation. Restart your terminal or run source $HOME/.cargo/env to update your current session.

Can I have multiple Rust versions installed on Debian 12? 

Yes, Rust makes it easy to manage multiple toolchain versions. You can use rustup command to install and switch between different Rust versions.

How do I update Rust to the latest version on Debian 12? 

Use the rustup update command to update Rust and its components to the latest version. Rustup will automatically download and install the necessary updates.

Does Rust use a separate package manager for libraries? 

No, Rust uses Cargo, a package manager and build tool, for managing dependencies and building projects. Cargo is installed alongside Rust.

Can I use IDEs with Rust on Debian 12? 

Yes, Rust has excellent IDE support. Popular choices include Visual Studio Code with the Rust extension, IntelliJ IDEA with the Rust plugin, and Emacs with Rust mode.

Does Rust support cross-compilation on Debian 12? 

Yes, Rust supports cross-compilation to various target architectures. Rustup provides a convenient way to manage and install cross-compilation toolchains.

Conclusion

We hope this tutorial helped you understand how to install Rust on Debian 12.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.