How to install Rust on Linux

Rust is an incredibly important programming language for creating both system and backend software. Find out how to install Rust on Linux in a few quick steps.

Image: Getty Images/iStockphoto

Rust was developed by Mozilla in 2010, for highly concurrent and safe systems. The syntax is similar to C and C++, with blocks of code delineated by curly braces, as in:

fn main() {

    println!("Hello World!");
}

Rust is employed in data centers by companies like Dropbox, Postmates, Stac, Wantedly, Doctolib, and QIWI, and emphasizes safety, control of memory layout, and concurrency. Rust supports concepts like:

Rust is open source and can be installed on a number of platforms. I want to walk you through the process of installing this highly useful language on both Debian/Ubuntu- and RHEL-based Linux distributions.

SEE: Rust: What it is, why you should learn it, and how you can master it (TechRepublic download)

What you’ll need

If you use a distribution that doesn’t work with sudo, you’ll then have to su to the root user in place of using the sudo command.

How to install Rust

The first thing to be done is the installation of curl. On a Debian- or Ubuntu-based distribution, do this with the command:

sudo apt-get install curl -y

On a Red Hat-based distribution, install curl with the command:

sudo dnf install curl -y

Once curl is installed, download and install Rust with the command:

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

When the installer has downloaded, it will run and first ask if you want to proceed, customize, or cancel the installation (Figure A).

Figure A

rustd.jpg

Let’s proceed with the installation.

Type 1 to proceed. During the process, Rust will fail to add the bin directory for Cargo (the package manager and crate host for rust) to your $PATH, so you’ll have to do it manually with the command:

source $HOME/.cargo/env

After that, you’ll need to source your user .profile to use the modified $PATH and ensure your user shell will function with the Rust environment. This is accomplished with the command:

source ~/.profile

Finally, you need to install a few dependencies, required by the rust command. For Debian/Ubuntu, install the remaining dependencies with the command:

sudo apt-get install build-essential -y

For CentOS/RHEL use the command:

sudo dnf install cmake gcc -y

How to test the Rust installation

Let’s test Rust using the “Hello, World!” application. First, create a new directory to house our test with the command:

mkdir rusttest

Change into the new directory with the command:

cd rusttest

Create a new rust file with the command:

nano rusttest.rs

Past the following Hello, World example into the new file:

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

Save and close the file. 

Create the Rust executable with the command:

rustc rusttest.rs

A new executable file will be created, called rusttest. You can run that newly built application with the command:

./rusttest

You should see the output of the application printed as Hello, TechRepublic (Figure B).

Figure B

rusto.jpg

A successful run of our application.

Congratulations, you’ve installed Rust and used it to create your first application. 

Also see