How to Install Rust and Cargo on RHEL 7

Rust is a systems programming language designed for safety, speed, and concurrency. It has quickly become one of the most loved languages among developers due to its memory safety guarantees, zero-cost abstractions, and powerful type system. Unlike garbage-collected languages, Rust achieves memory safety at compile time through its ownership model — making it ideal for system utilities, web services, embedded programming, and performance-critical applications. Cargo, Rust’s built-in package manager and build tool, simplifies dependency management, building, testing, and publishing of Rust projects. This tutorial walks you through installing Rust and Cargo on Red Hat Enterprise Linux 7 using the official rustup installer, creating a simple project, and getting familiar with the Cargo workflow.

Prerequisites

  • A running RHEL 7 system with a non-root user that has sudo privileges, or access to the root account.
  • Internet access to download the rustup installer script.
  • curl installed — if not present, install it with sudo yum install -y curl.
  • Development tools available for compiling native extensions: sudo yum groupinstall -y "Development Tools".
  • At least 1 GB of free disk space in the user’s home directory for the Rust toolchain.

Step 1: Install Required Build Dependencies

Before downloading Rust, ensure your system has the compiler toolchain and essential build utilities. The Development Tools group installs GCC, make, and other tools that Rust’s linker depends on at compile time.

sudo yum groupinstall -y "Development Tools"
sudo yum install -y curl openssl-devel pkg-config

The openssl-devel package is required by many popular Rust crates that use TLS/HTTPS. Installing it now avoids build failures later when adding dependencies to your projects.

Step 2: Download and Run the rustup Installer

Rust is installed and managed through rustup, the official Rust toolchain installer. It downloads the Rust compiler (rustc), the package manager (cargo), and the standard library. The installer also handles future updates and lets you manage multiple toolchain versions side by side.

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

The flags used here enforce security: --proto '=https' restricts to HTTPS-only connections, and --tlsv1.2 requires TLS 1.2 or higher. The installer will present a menu. For a standard installation, press 1 and then Enter to proceed with the default options. This installs the stable Rust toolchain to $HOME/.cargo/.

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
> 1

After installation completes you will see a message confirming that Rust has been installed to your home directory.

Step 3: Configure Your Shell Environment

The rustup installer adds a line to ~/.profile and ~/.bash_profile that sources the Cargo environment file. For the current terminal session, source it manually:

source $HOME/.cargo/env

To make this permanent for all future sessions, add it to your ~/.bashrc:

echo 'source $HOME/.cargo/env' >> ~/.bashrc
source ~/.bashrc

This adds $HOME/.cargo/bin to your PATH, making rustc, cargo, and rustup available as commands.

Step 4: Verify the Installation

Confirm that Rust and Cargo are correctly installed and accessible:

rustc --version
cargo --version
rustup --version

Expected output similar to:

rustc 1.78.0 (9b00956e5 2024-04-29)
cargo 1.78.0 (54d8815d0 2024-04-09)
rustup 1.27.1 (54dd3d00f 2024-04-24)

You now have a fully functional Rust development environment on RHEL 7.

Step 5: Create Your First Cargo Project

Cargo manages Rust projects with a consistent directory structure and configuration format. Create a new binary project called hello-world:

cargo new hello-world
cd hello-world

Cargo generates the following structure:

hello-world/
├── Cargo.toml
└── src/
    └── main.rs

The Cargo.toml file is the project manifest — it defines the project name, version, edition, and dependencies. The default src/main.rs already contains a working Hello World program:

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

Step 6: Build and Run the Project

To compile and run the project in development mode:

cargo build
cargo run

You should see:

   Compiling hello-world v0.1.0 (/home/user/hello-world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.84s
     Running `target/debug/hello-world`
Hello, world!

For a production-optimized binary with compiler optimizations enabled, build with the --release flag:

cargo build --release

The release binary is placed in target/release/hello-world. Release builds are significantly faster at runtime but take longer to compile due to optimization passes.

Step 7: Run Tests with Cargo

Rust has a built-in testing framework. You can write unit tests directly in your source files using the #[test] attribute. Add a simple test to src/main.rs:

fn add(a: i32, b: i32) -> i32 {
    a + b
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_add() {
        assert_eq!(add(2, 3), 5);
    }

    #[test]
    fn test_add_negative() {
        assert_eq!(add(-1, 1), 0);
    }
}

fn main() {
    println!("2 + 3 = {}", add(2, 3));
}

Run all tests with:

cargo test

Output will show each test and its result:

running 2 tests
test tests::test_add ... ok
test tests::test_add_negative ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Step 8: Add Dependencies via Cargo.toml

To add a third-party library (crate), edit Cargo.toml and add it under [dependencies]. For example, to add the popular serde serialization library:

[package]
name = "hello-world"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"

Run cargo build and Cargo will automatically download, compile, and link the specified crates from crates.io.

Step 9: Update Rust with rustup

Rust follows a six-week release cycle. To upgrade to the latest stable release:

rustup update

To install a specific toolchain version or the nightly channel:

rustup install nightly
rustup default nightly

To return to stable:

rustup default stable

List all installed toolchains with:

rustup toolchain list

Step 10: Useful Cargo Commands Reference

  • cargo check — fast syntax and type check without producing a binary
  • cargo clippy — lint your code for common mistakes and style issues
  • cargo fmt — auto-format code according to Rust style guidelines
  • cargo doc --open — generate and open HTML documentation for your project
  • cargo clean — remove the target/ build directory
  • cargo publish — publish your crate to crates.io

Rust and Cargo are now fully installed and operational on your RHEL 7 system. You have learned how to install the toolchain via rustup, configure your shell environment, create and build Cargo projects, write and run tests, manage dependencies, and keep Rust up to date. Rust’s combination of zero-cost abstractions and memory safety without a garbage collector makes it an excellent choice for building high-performance, reliable system software — and Cargo’s integrated tooling makes the development experience smooth and consistent across projects and teams.