Hostwinds Tutorials

Search results for:


Table of Contents


What is Rust-lang
Installing Rust
Step1: Update Package Lists
Step 2: Install curl
Step 3: Download and Run Rust Installation Script
Step 4: Source the Environment
Step 5: Verify Installation (Optional)
Creating a Rust Project
Step 1: Create Project Directory
Step 2: Navigate into the project directory
Step 3: Build Your Project
Step 4: Run Your Project
How to Uninstall Rust
(Optional) Remove any remaining Rust directories and files

How to Install Rust on Linux

Tags: Cloud Servers,  VPS,  Linux 

What is Rust-lang
Installing Rust
Step1: Update Package Lists
Step 2: Install curl
Step 3: Download and Run Rust Installation Script
Step 4: Source the Environment
Step 5: Verify Installation (Optional)
Creating a Rust Project
Step 1: Create Project Directory
Step 2: Navigate into the project directory
Step 3: Build Your Project
Step 4: Run Your Project
How to Uninstall Rust
(Optional) Remove any remaining Rust directories and files

What is Rust-lang

Rust is designed to be a low-level systems programming language, similar to C and C++. It provides low-level control over system resources, memory management, and performance, making it suitable for developing operating systems, device drivers, game engines, and other performance-driven software.

This tutorial will show you how to install Rust through your Linux distribution.

We'll also go over:

  • How to verify installation
  • How to uninstall Rust
  • How to create a Rust project

Installing Rust

Step1: Update Package Lists

Start by updating the package lists for your Linux distribution. The following command will ensure your package is up to date.

# For Ubuntu/Debian
Copysudo apt update 

# For Fedora
sudo dnf update 

Step 2: Install curl

Rust uses curl to download components during installation. Install curl if it's not already installed.

# For Ubuntu/Debian
Copysudo apt install curl 

# For Fedora
sudo dnf install curl

Step 3: Download and Run Rust Installation Script

Rust provides an installation script that detects the appropriate package for your Linux distribution.

The following script will download and install the latest version of Rust.

Command:

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

Output:

Output should look something like the following, though it may slightly differ depending on the version of Rust you're downloading.

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /home/username/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory located at:

  /home/username/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /home/username/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /home/username/.profile
  /home/username/.bash_profile
  /home/username/.bashrc

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:

   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

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

>

At the bottom of the output you'll be asked to choose from one of three (3) options. Unless you have specific customizations in mind, we suggest choosing option 1 (default).

Once install is complete, you'll see confirmation a message similar to the following:

Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).
To configure your current shell, run:
source $HOME/.cargo/env

Step 4: Source the Environment

After the installation is complete, you need to source the environment, or configure your shell, to make the Rust binaries available in your current shell.

Command:

Copysource $HOME/.cargo/env

No output will be shown, indicating the command executed without error. It will silently update your shell's environment, allowing you to run Rust commands like rustc and cargo.

Step 5: Verify Installation (Optional)

If you'd like to verify that Rust is installed correctly and see which version of Rust you're running, use the following command:

Copyrustc --version
cargo --version

These commands should print the installed versions of the Rust compiler (rustc) and the Cargo package manager (cargo).

Output (rustc):

For the Rust compiler (rustc) you should see a similar output to the following:

rustc --version rustc 1.xx.0 (xxxxxxxxx 20xx-xx-xx)

Output (cargo):

Output for the Cargo package manager would look something like this:

cargo --version cargo 1.xx.0 (xxxxxxxxx 20xx-xx-xx)

That's it! You've successfully installed and verified Rust on your Linux system. You can now start writing Rust code, building projects, and using Cargo to manage dependencies.

Creating a Rust Project

To create a Rust project we'll be using the cargo command. Here are the steps:

Step 1: Create Project Directory

In your terminal window, run the following to create a new directory:

Command:

Copycargo new project_name

Replace project_name with the project name of your choice, just be sure to follow Rust's naming conventions - lowercase with underscores for spaces.

Output:

Once the project is created, you'll see the following output:

Created binary (application) `project_name` package

Step 2: Navigate into the project directory

Command:

Copycd project_name

Output:

You'll see the generated files and directory structure

Copyproject_name/
├── Cargo.toml
├── src/
│   └── main.rs
  • Cargo.toml is the manifest file that contains metadata about your project, such as its name, version, dependencies, and build instructions.
  • src/main.rs is the main source file for your Rust program, which contains the main function that serves as the entry point of your application.

Step 3: Build Your Project

Command:

Copycargo build

This will compile your Rust code and create an executable binary in the target/debug/ directory.

Output:

The output will vary depending on whether or not it's an existing project, you have any project dependencies, or compilation errors. If it's a new project, output should look something like this:

Compiling project_name v0.1.0 (/path/to/your/project)
Finished dev [unoptimized + debuginfo] target(s) in 1.11s

Step 4: Run Your Project

Command:

Copycargo run

This command will build your project (if it hasn't been built yet) and then run the resulting executable.

Output:

Depending on the state of your project, such as existing builds, code changes, or runtime errors, output will vary.

For a new project with the default "Hello, world!" program, you'll something like this:

Compiling project_name v0.1.0 (/path/to/your/project)
    Finished dev [unoptimized + debuginfo] target(s) in 0.61s
     Running `target/debug/project_name`
Hello, world!

How to Uninstall Rust

To uninstall Rust from your system, you can follow these steps:

Command:

Copyrustup self uninstall

This command will remove the entire Rust toolchain, including the Rust compiler (rustc), Cargo package manager (cargo), and all associated components.

Output:

Thanks for hacking in Rust!

This will uninstall all Rust toolchains and data, and remove $HOME/.cargo/bin from your PATH environment variable.

Continue? (y/N)

Type "y" then press ENTER to complete uninstall.

(Optional) Remove any remaining Rust directories and files

The uninstall script should remove most of the Rust-related files and directories. However, you have the option to manually remove any remaining directories or files.

Command:

Copyrm -rf ~/.cargo
rm -rf ~/.rustup

Output:

There is no output when the script is executed. However, you can verify the directories are removed with the following command:

ls -la ~ | grep ".cargo"
ls -la ~ | grep ".rustup"

No output from this will indicate the directories were successfully removed.

Written by Hostwinds Team  /  June 11, 2021