Development

Install Rust on Linux

5 Jun 2020 12:00pm, by

An open source programming language, Rust has grown quite popular over the last few years. Developed as a pet project by one-time Mozilla developer Graydon Hoare in 2006 and backed by the LLVM, Rust is extremely fast, prevents segfaults, and guarantees thread and memory safety. Rust also supports zero-cost abstractions, move semantics, threads without data races, trait-based generics, type inference, efficient C bindings, minimal runtime, and pattern matching.

Used by companies like Dropbox, Red Hat, and NPM, Rust is a multiparadigm language that is focused on performance and safety. The Rust language is similar to C++ and can run on numerous platforms.

If you’re looking to write extremely fast code with a very low memory footprint, you would have typically defaulted to C or C++. The problem with those languages is, using them in production code meant you’d have to manage memory manually and understand exactly how you might cause undefined behavior.

The Rust compiler, however, ensures you’re using memory safely, so you can concentrate on the problem you’re trying to solve, not the problem your programming language might introduce. Case in point, the Rust compiler checks every variable you use and every memory address you reference. In fact, according to this chart, half the Linux CVEs in 2018 would have been avoided using Rust.

If you’re curious as to what applications might make for good Rust applications, consider anything that would benefit from a very low overhead:

  • Embedded systems
  • Appliances
  • Industrial machines
  • Internet of Things/Edge Computing
  • Web apps
  • Bare metal development

The only caveat to Rust is that it does have a rather steep learning curve. This is mostly due to the fact that developers must be very aware of basic computing principles regarding memory allocation and concurrency. If, however, you’re willing to take the time to invest in learning Rust, the payoff will be worth the effort.

With that said, I want to walk you through the process of installing Rust on Linux. I’ll be demonstrating the process on both Debian- and Red Hat-based systems. The only things you’ll need are a running instance of Linux and a user with sudo privileges.

Let’s install.

Taking Care of the Dependencies

The first thing we must do is install the necessary dependencies for Rust. Like Rust, these dependencies are found in the standard repository, so the installation can be taken care of with a single command.

On a Debian-based system (such as Ubuntu), the dependency can be installed with the command:

sudo apt-get install build-essential -y

On a Red Hat-based system (such as CentOS), the dependencies can be taken care of with the command:

sudo dnf install cmake gcc -y

Installing Rust

The installation of Rust is actually quite simple. To do this, we’ll make use of the curl command. On the off-chance your distribution doesn’t include curl, you can install it from the standard repository.

On a Debian-based system, you can install curl with the command:

sudo apt-get install curl -y

On a Red Hat-based system, you can install curl with the command:

sudo dnf install curl -y

With curl installed, you can now install Rust, using the same command, regardless of the distribution you are using. That command is:

curl https://sh.rustup.rs -sSf | sh

When prompted (Figure 1), type 1 and hit Enter on your keyboard.

Figure 1: Installing Rust on Pop!_OS Linux.

The installer does take a bit of time (no more than 2-5 minutes — depending on your network connection). Once it completes, you’ll be informed of the success (Figure 2).

Figure 2: Rust has been successfully installed.

You will notice, in the output, it states that as soon as you log out and log back in the Cargo bin directory will be added to your $PATH. That might fail. So instead, we’ll help it along manually. To do this, issue the command:

source $HOME/.cargo/env

After you’ve done that, source your user .profile so it will use the modified $PATH and ensure your shell functions properly with the Rust environment. To do this, issue the command:

source ~/.profile

Testing your Installation

With Rust installed, let’s run a test. We’ll use the tried and true “Hello, World!” example, with a twist. Create a new directory with the command:

mkdir ~/rustexample

Change into that directory with the command:

cd ~/rustexample

Next, we’ll create a new rust file with the command:

nano testingrust.rs

In that new file, paste the following:

Save and close the file.

Now, we’ll create the Rust executable for our Hello, World! program with the command:

rustc testingrust.rs

You should see no output.

To run the newly compiled program, issue the command (from within the ~/rustexample directory):

./testingrust

You should see Hello, The New Stack! printed in the output (Figure 3).

Figure 3: Our Hello, World! example runs just fine.

If you wanted, you could copy that executable into a directory that’s in your $PATH, so you could run the program from any directory. To do that, issue the command:

sudo mv testingrust /usr/local/bin

With the program moved, you can now run it, from any directory, with the command:

testingrust

And that’s all there is to installing and using Rust on Linux. At this point, you should probably take the time to read the Rust core documentation, in order to get up to speed on how to get most out of the language.

Red Hat is a sponsor of The New Stack.

Feature image by Herbert Aust from Pixabay.

A newsletter digest of the week’s most important stories & analyses.