Install and Use Rust Programming Language on Ubuntu 22.04

In this guide, we want to teach you to Install and Use Rust Programming Language on Ubuntu 22.04.

Rust is a statically-typed programming language designed for performance and safety, especially safe concurrency and memory management. Its syntax is similar to that of C++. It is an open-source project developed originally at Mozilla Research.

It solves problems that C/C++ developers have been struggling with for a long time: memory errors and concurrent programming. This is seen as its main benefit.

Install and Use Rust Programming Language on Ubuntu 22.04

For installing Rust Programming Language, you should log in to your server as a non-root user with root privileges. To do this, you can check our guide the Initial Server Setup with Ubuntu 22.04.

Install Rust on Ubuntu 22.04

First, you need to update and upgrade your APT repository list with the following command:

sudo apt update && sudo apt upgrade -y

Then, you need to install the required packages on your server with the command below:

sudo apt install curl build-essential gcc make -y

Now you can install Rust Language on Ubuntu 22.04, by running the Rust installer script:

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

You will get the following output. Type 1 and press enter to continue your installation.

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
>1

When your Rust installation on Ubuntu 22.04 is completed, you will get the following output:

Output
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

Now you need to activate the Rust environment for your current shell with the following commands:

# source ~/.profile
# source ~/.cargo/env

Verify your Rust installation on Ubuntu 22.04 by checking its version:

rustc -V
Output
rustc 1.61.0 (fe5b13d68 2022-05-18)

How To Use Rust Programming Language

At this point, we want to teach you how to use the Rust programming language by creating a sample project Hello world!.

First, you need to create a Rust project directory with the following command:

mkdir ~/rust-projects

Then, switch to your Rust programming directory:

cd rust-projects

Now use your favorite text editor to create your sample application, here we use vi:

vi helloworld.rs

Add the following content to the file:

fn main() {
println!("Hello World, this is a test provided by orcacore.com");
}

When you are done save and close the file.

Next, you need to compile the program with the following command:

rustc helloworld.rs

This will create an executable application.

Run the application with the command below:

./helloworld

In your output you will see:

Output
Hello World, this is a test provided by orcacore.com

To update your Rust programming language, you can use the following command:

rustup update

If you don’t want to use Rust, you can easily uninstall it from your Ubuntu 22.04 with the following command:

rustup self uninstall
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) y

When you removed Rust, you will get the following output:

Output
info: removing rustup home
info: removing cargo home
info: removing rustup binaries
info: rustup is uninstalled

Conclusion

At this point, you learn to Install and Use Rust Programming Language on Ubuntu 22.04. Also, you learn the basic usage of Rust by creating a sample project.

Hope you enjoy it.

You may be interested in these articles:

Install and Use Rust Programming Language on Rocky Linux 8

How To Set up Rust Programming Language on Debian 11

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

Stay informed and not overwhelmed, subscribe now!