How to set up a Rust environment on Linux

Start your Rust development journey by setting up a Rust development environment on your Linux PC. Here are detailed instructions.

Start your Rust development journey by setting up a Rust development environment on your Linux PC. Here are detailed instructions.

Picture 1 of How to set up a Rust environment on Linux

According to Stack Overflow's Developer Survey 2022, Rust has become the most popular programming language for the past 7 years. It is secure, efficient and flexible to handle applications of all levels of complexity, from system programming to chatbots and more.

If you haven't had a chance to start developing in Rust yet, you should start by setting up a Rust development environment on Linux.

Rust installation required on Linux

Before installing Rust, you need to install a dependency: the build-essential package. Why? Because Rust needs a link to link all the object files produced by the Rust compiler into an executable binary. The build-essential package contains a linker that will get the job done.

Here is the command to install the build-essential package on Linux:

sudo apt update && sudo apt install build-essential

For Arch Linux, run:

sudo pacman -S base-devel

 

Once you've finished installing this package, move on to installing Rust.

Install Rust on Linux

Picture 2 of How to set up a Rust environment on Linux

Rust is super easy to install and set up. All you need to do is run a single script. It will install and set everything up for you.

The best thing is that the official Rust developer has developed this script strictly, so you can't run suspicious software on the system.

Here is the command to install and run the rustup script:

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

This command will use curl to load the script and run it using sh. Enter 1 when prompted.

After a while, the script will complete the task and ask you to update the PATH variable to include the Cargo bin directory. You can do that with the source command:

source "$HOME/.cargo/env"

Setting up and writing 'Hello, World' in Rust

Rust's build system, Cargo is a resourceful tool that helps you get up and running, organize, and test your code with a few lines of command. To set up a new Rust project, run this command:

cargo new 

Picture 3 of How to set up a Rust environment on Linux

 

This command will set up a directory structure for the base project and add the main.rs file containing the code for the 'Hello, World' program inside //src . You can manually compile this code with compiler rustc or use Cargo to run it:

cargo run

Picture 4 of How to set up a Rust environment on Linux

Also, here's the syntax for using rustc to manually compile the code:

rustc main.rs ./a

Picture 5 of How to set up a Rust environment on Linux

Now that you have Rust installed and set up on your Linux system, you can focus on learning and developing all kinds of programs with Rust right away.

Update 24 July 2023
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile