Rust is a modern system programming language, first released in 2010. Rust was developed to solve common problems in other programming languages, such as memory safety, concurrency, and performance. Rust is now even being used in the Linux Kernel.

Why use Rust

Memory safety

Rust guarantees memory safety through compile-time checks and strict ownership rules. This avoids common programming errors such as null pointer dereferences, buffer overflows and data overflows.

Concurrency

Rust has built-in support for concurrent programming. It provides lightweight threads called “tasks” and a powerful type system that ensures thread safety.

Performance

Rust provides abstractions in a way that high-level programming constructs do not cause any runtime overhead. This makes it easier for developers to write safe and fast code.

Ownership model

Memory is managed efficiently by Rust due to its built-in ownership model. It enforces strict rules for ownership, borrowing, and lifetime, preventing common mistakes like use-after-free and double-free.

Cargo

Rust has a powerful package manager and build system called Cargo. It makes it easy to manage dependencies, build projects and run tests.

Easy syntax

Rust has a clean and expressive syntax influenced by C but focuses on readability and simplicity. It offers pattern matching, closures, generics, and other modern language features.

Rust shines where performance, reliability, and security are important and is widely used for developing operating systems (like Linux), embedded systems, device drivers, network services, game engines, and other performance-critical software.

In this guide, we’ll take you through the installation process of Rust programming language on an AlmaLinux 9. You will install Rust with two different methods and learn the basic usage of the Cargo package manager for creating and managing the Rust project.

Prerequisites

Before you start, ensure that you have the following:

  • An AlmaLinux 9 machine – Server or Desktop version.
  • A non-root user with root/administrator privileges.

Installing Rust via AppStream Repository

On AlmaLinux 9, the default appstream repository provides a Rust package that you can easily install via DNF. Also, there is the Cargo package, which is the Rust package manager and dependency management for your Rust project.

This section will show you how to install Rust and Cargo package manager on AlmaLinux 9 via the appstream repository.

First, enter the following dnf command to check the detailed Rust information that is available on the AlmaLinux repository.

sudo dnf info rust

In the following output, you could see the Rust v1.66 available on the AlmaLinux appstream repository.

<img alt="rust info package" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/1-info-rust-package.png64806f5564a0a.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="346" loading="lazy" src="data:image/svg xml,” width=”750″>

Now, run the command below to install Rust and Cargo package manager to your system. Input y when prompted and press ENTER.

sudo dnf install rust cargo

<img alt="install rustup" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/2-install-rust-cargo.png64806f55cc79d.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="446" loading="lazy" src="data:image/svg xml,” width=”750″>

Once Rust and Cargo are installed, run the following command to check the location of binary files for Rust and Cargo.

which rust

which cargo

Then, verify the Rust and Cargo version using the command below.

rustc --version

cargo --version

You will see the following output, which confirms Rust and Cargo v1.66 is installed via the appstream repository.

<img alt="check rust and cargo version" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/3-check-rustc-cargo.png64806f56361bb.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="232" loading="lazy" src="data:image/svg xml,” width=”570″>

Installing Rust via Rustup

Another method for installing Rust is via rustup, which is a toolchain multiplexer for your Rust development environment. It’s similar to rbenv for Ruby, nvm for Node.js or Pyenv for Python.

You can install rustup on both system-wide environments or per-user environments. In the following section, you will install Rust via rustup system-wide, so any user can execute your Rust installation.

Installing Dependencies

Before installing rustup, you must install package dependencies for it. You must add the EPEL repository to your system, the install some development tools such as gcc, make, and cmake.

First, run the dnf command below to add the EPEL repository to your system.

sudo dnf install epel-release

Input y and press ENTER to confirm.

<img alt="install epel" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/4-install-epel.png64806f56660b4.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="199" loading="lazy" src="data:image/svg xml,” width=”750″>

Then, install some package dependencies using the dnf command below.

sudo dnf install cmake gcc make wget tree -y

<img alt="install dependencies" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/5-install-dependencies.png64806f56db8b3.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="297" loading="lazy" src="data:image/svg xml,” width=”750″>

Once dependencies are installed, move on to start Rustup installation.

Installing Rust and Rustup

rustup can be installed easily. It provides an installer script that you can run on any Linux distribution.

Now, you will install rustup, which includes the stable version of Rust and Cargo package manager. You will also set up the RUSTUP_HOME environment variable and add the binary path of rustup to the system PATH.

Run the following command to download the Rustup installer, then install the Rustup to the /opt/rust directory.

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

The output during the Rustup installation.

<img alt="installing rustup" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/6-installing-rust-rustup.png64806f580582f.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="161" loading="lazy" src="data:image/svg xml,” width=”750″>

Once Rustup is installed, the output message “Rust is installed now. Great!” will be displayed like this:

<img alt="installation rustup finished" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/7-installation-finished.png64806f58253f7.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="279" loading="lazy" src="data:image/svg xml,” width=”750″>

Next, run the command below to set up the /opt/rust and add the /opt/rust/bin directory to the system PATH.

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

Reload the /etc/profile to apply the new system environment variables.

source /etc/profile

After that, verify both RUSTUP_HOME and PATH environment variables using the following command.

echo $RUSTUP_HOME

echo $PATH

From the output, you can see that RUSTUP_HOME is pointed to the /opt/rust directory and the /opt/rust/bin directory is added to the system PATH.

<img alt="configure rustup" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/8-configure-rustup.png64806f58448f5.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="243" loading="lazy" src="data:image/svg xml,” width=”750″>

Now run the following command to check the location of the Rust binary file and verify the Rust version.

which rustc

rustc -V

Then, run the following command to check the location of the Cargo binary file and verify the Cargo version.

which cargo

cargo -V

The displayed output should confirm that Rust and Cargo binary file is located in the /opt/rust/bin directory. And the installed version of Rust and Cargo is v1.69.

<img alt="check rust and cargo version" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/9-check-rust-cargo.png64806f58668dc.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="294" loading="lazy" src="data:image/svg xml,” width=”392″>

Managing Toolchain

In rustup, there is some concept like toolchain and channel. A channel is where the Rust version comes in, you can select from different builds such as stable, beta, and nightly. A toolchain is a single installation of the Rust compiler, and you can install multiple toolchains on your rustup environment.

In this section, you will learn how to manage the rustup toolchain via the rustup command.

First, run the following command to generate the bas completion for the rustup command and load the new bash completion. This will simplify Rust management via rustup.

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

source /etc/profile.d/bash_completion.sh

Now type the rustup command and press TAB to get the list of available options for rustup.

rustup TAB

If rustup bash completion is successful, you should get the output of available options for the rustup command.

<img alt="rustup completion" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/10-rustup-completion.png64806f588c171.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="119" loading="lazy" src="data:image/svg xml,” width=”750″>

Next, run the rustup command below to check the information of your Rust and rustup installation.

rustup show

From the output below, you should get the location of the rustup home directory /opt/rust and the default stable channel of rustup is installed on your system.

<img alt="list rustup components" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/11-list-rustup-components.png64806f58cd420.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="191" loading="lazy" src="data:image/svg xml,” width=”411″>

Next, run the following command to install another rustup toolchain version. In this example, you will install the nightly version of rustup.

rustup toolchain install nightly

<img alt="installing rust toolchain" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/12-installing-toolchain-rustup.png64806f58f341e.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="405" loading="lazy" src="data:image/svg xml,” width=”750″>

After rustup nightly is installed, run the following command to verify the list of available rustup toolchains. And you should see two versions of the rustup toolchain, the stable and nightly versions.

rustup toolchain list

Now run the rustup command below to switch to a different version of the toolchain. In this example, you will be switching to the rustup nightly.

rustup default nightly

<img alt="setup default rust toolchain" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/13-setup-default-rust-toolchain.png64806f5926299.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="213" loading="lazy" src="data:image/svg xml,” width=”750″>

Now verify the current version of Rust and Cargo using the following command. You should see that Rust and Cargo v1.71 is currently used on the system.

rustc -V

cargo -V

<img alt="check rust versionaand cargo version" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/14-check-rust-cargo-version.png64806f5946bf9.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="169" loading="lazy" src="data:image/svg xml,” width=”461″>

Create First Rust Project

With the Rust programming language and Cargo package manager installed, now you will be creating the first Rust project via Cargo.

First, log in to your user using the following command.

su - username

Now create a new Rust project using the cargo command below. In this demo, you will create a new project hello_rust.

cargo new hello_rust

After the command is executed, you should see the new directory hello_rust will be created.

Move to the hello_rust directory and run the tree command below to get the list of available files and directories.

cd hellow_rust/

tree .

You should the file COnfig.toml which stored information about your application and all required dependencies, and the directory src that contains the Rust application source code.

<img alt="create project with rust" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/15-create-project-rust-with-cargo.png64806f5969794.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="302" loading="lazy" src="data:image/svg xml,” width=”580″>

Below is the similar content of the Config.toml file. Modify it as you need and if you have dependencies for your Rust application, include them in the [dependencies] section.

[package]

name = "hello_rust"

version = "0.1.1"

edition = "2021"[dependencies]

Next, open the file src/main.rs using your preferred editor and change the default “Hello World” message to this.

fn main() {

    println!("Hello World, welcome to Rust.");

}

Save the file and exit the editor when finished.

Now run the cargo command below to compile and build the Rust project. Then verify again the list files and directories after the project is compiled.

cargo build

tree .

You should see a new directory named target is generated, which is where your compiled application is stored at.

<img alt="cargo build project" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/16-cargo-build.png64806f59bd14d.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="750" loading="lazy" src="data:image/svg xml,” width=”561″>

Now run the cargo command below to run the application. You should see the output such as “Hello World, welcome to Rust“.

cargo run

<img alt="run project rust" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/17-run-project-via-cargo.png64806f5a0cb44.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="165" loading="lazy" src="data:image/svg xml,” width=”592″>

Additionally, you can also run the binary file of your Rust application directly like this.

./target/debug/hello_rust

And you should get the same output.

<img alt="run project directly" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/06/echo/17-run-project-directly.png64806f5a35140.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="102" loading="lazy" src="data:image/svg xml,” width=”561″>

Conclusion

Well done! You’ve followed all the steps and installed Rust on your AlmaLinux 9 system. You have learned two methods of installing Rust via the AlmaLinux appstream repository and rustup.

In addition to that, you have also learned how to switch between multiple versions of the rustup toolchain and how to create the first Rust application/project via Cargo – Rust package manager and dependencies manager.