Skip to main content

Installing Rust on Debian

Rust is a modern programming language that provides memory safety, concurrency, and high performance. In this tutorial, we will walk you through the step-by-step process of installing Rust on Debian.

Prerequisites

Before we begin, make sure you have the following prerequisites installed on your Debian system:

  • Debian OS (any recent version)
  • Access to a terminal or command-line interface
  • Internet connection

Step 1: Update the System

First, let's ensure that our system is up to date. Open a terminal and execute the following command:

sudo apt update && sudo apt upgrade -y

This command updates the package lists and upgrades all installed packages to their latest versions.

Step 2: Install Rust Using rustup

Rust provides an official tool called rustup for managing Rust versions and associated tools. Let's install rustup by executing the following command:

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

This command uses curl to download the rustup installer script and then pipes it to the shell (sh) for execution.

Step 3: Configure Environment Variables

After the installation, rustup modifies your environment variables to include the Rust binaries in your system's PATH. However, these changes will only take effect in new terminal sessions. To immediately apply the changes, execute the following command:

source $HOME/.cargo/env

This command reloads the environment variables in the current terminal session.

Step 4: Verify the Installation

To verify that Rust has been successfully installed, run the following command:

rustc --version

You should see the version number of the Rust compiler printed on the terminal, indicating a successful installation. For example:

rustc 1.55.0 (c8dfcfe04 2021-12-06)

Step 5: Update Rust Toolchain

Rust receives frequent updates, and it's always a good idea to keep your installation up to date. To update the Rust toolchain, run the following command:

rustup update

This command updates the Rust toolchain to the latest available version.

Step 6: Usage Examples

Now that you have Rust installed, let's explore a couple of usage examples.

Example 1: Hello, World!

Let us look at a simple Hello World example. First create a new file named main.rs using your a text editor and add the following code:

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

After that simply save the file and exit the text editor. Open a terminal and navigate to the directory containing main.rs. To compile and run the code, execute the following command:

rustc main.rs && ./main

This will output Hello, World! printed on the terminal.

Example 2: Creating a Rust Project

Rust provides a built-in tool called Cargo for managing Rust projects. Let's create a new project using Cargo. In a terminal, execute the following command:

cargo new myproject

This command creates a new directory named myproject with the necessary files and structure for a Rust project. Navigate to the project directory using cd myproject. To build and run the project, execute the following command:

cargo run

Cargo will automatically compile and run your project, and you should see the output on the terminal.

Conclusion

Congratulations! You have successfully installed Rust on Debian and explored a few usage examples. Feel free to dive deeper into Rust by exploring its official documentation and experimenting with various Rust projects. Happy coding!