Guide to Install Rust and Use Cargo Package Manager on AlmaLinux 9

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.

rust info package

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

install rustup

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.

check rust and cargo version

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.

install epel

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

sudo dnf install cmake gcc make wget tree -y

install dependencies

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.

installing rustup

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

installation rustup finished

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.

configure rustup

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.

check rust and cargo version

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.

rustup completion

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.

list rustup components

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

installing rust toolchain

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

setup default rust toolchain

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

check rust versionaand cargo version

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.

create project with rust

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.

cargo build project

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

cargo run

run project rust

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.

run project directly

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.

Share this page:

0 Comment(s)