Oct 9, 2023 6 min read

How To Install Rust on Ubuntu 20.04

Install Rust on Ubuntu 20.04 with our step-by-step tutorial. It is a modern programming language designed to be fast, safe, and concurrent.

Install Rust on Ubuntu 20.04
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install Rust on Ubuntu 20.04, let’s briefly understand – What is Rust?

Rust is a modern programming language designed to be fast, safe, and concurrent. It focuses on preventing common programming errors, such as null pointer dereferences and buffer overflows, which can lead to security vulnerabilities. Rust's syntax is similar to C++, making it easy to learn for developers familiar with that language.

With its powerful memory management system and extensive tooling, Rust is becoming increasingly popular in areas such as systems programming, web development, and game development. Start using Rust today and enjoy its safety, speed, and simplicity.

In this tutorial, you will install Rust in an independent environment on Ubuntu 20.04. We will also address a few FAQs on how to install Rust on Ubuntu 20.04.

Advantages of Rust

  1. Safety: Rust's strong type system and ownership model ensure memory safety and prevent common bugs like null pointer errors.
  2. Speed: Rust's zero-cost abstractions and low-level control result in fast and efficient code execution.
  3. Concurrency: Built-in features enable safe and easy concurrent programming, making it suitable for multi-threaded applications.
  4. Easy to Learn: Rust's syntax is similar to C++, making it approachable for developers already familiar with that language.
  5. Ecosystem and Tooling: Rust has a growing ecosystem of libraries and tools, including a powerful package manager (Cargo) and extensive documentation, enabling faster development and productivity.

Prerequisites

You'll need an Ubuntu 20.04 server with a sudo-enabled non-root user and a firewall to complete this guide.

Step 1 — Using the rustup Tool to Install Rust on Ubuntu

Although there are a few other ways to install Rust on Linux, using the rustup command line tool is the suggested approach.

Run the following command to install the most recent stable version of Rust and download the rustup tool:

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

You are prompted to select the installation type:

Output

info: downloading installer

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/sammy/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  /home/sammy/.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/sammy/.cargo/bin

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

  /home/sammy/.profile
  /home/sammy/.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
>

The default selection 1 is used in this tutorial. However, you can select option 2 if you are familiar with the rustup installer and wish to customize your installation. Input your choice, then hit Enter.

Option 1's output is as follows:

Output

info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2023-01-10, rust version 1.66.1 (90743e729 2023-01-10)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
 67.4 MiB /  67.4 MiB (100 %)  40.9 MiB/s in  1s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
  6.6 MiB /   6.6 MiB (100 %)   5.5 MiB/s in  1s ETA:  0s
info: installing component 'clippy'
info: installing component 'rust-docs'
 19.1 MiB /  19.1 MiB (100 %)   2.4 MiB/s in  7s ETA:  0s
info: installing component 'rust-std'
 30.0 MiB /  30.0 MiB (100 %)   5.6 MiB/s in  5s ETA:  0s
info: installing component 'rustc'
 67.4 MiB /  67.4 MiB (100 %)   5.9 MiB/s in 11s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu installed - rustc 1.66.1 (90743e729 2023-01-10)


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"

Run the following command after that to add the PATH environment variable to the Rust toolchain directory:

source $HOME/.cargo/env

Step 2 — Verifying the Installation

By asking the version, you can confirm the Rust installation:

rustc --version

The Rust programming language version that is currently installed on your system is returned by the rustc --version command.

Output

rustc 1.66.1 (90743e729 2023-01-10)

Step 3 — Installing a Compiler

To combine compiled outputs into a single file, Rust requires a linker program. A linker is part of the build-essential package's GNU Compiler Collection (gcc). When attempting to compile, you can see the following error if gcc is not installed:

error: linker `cc` not found
  |
  = note: No such file or directory (os error 2)

error: aborting due to previous error

To install the build-essential package, use apt.

Update the Apt package index first:

sudo apt update

If asked, enter your password to proceed. Upgrade any outdated packages next:

sudo apt upgrade

If prompted, press Y to complete the upgrades.

Install the build-essential package when the upgrades have been finished:

sudo apt install build-essential

When asked to continue the installation, press Y. When your terminal reverts to the command prompt without any error warnings, the installation is finished.

Step 4 — Creating, Compiling, and Running a Test Program

You'll write a test program in this phase to use Rust and make sure it's functioning properly.

Create a few directories to hold the test script first:

mkdir ~/rustprojects
cd ~/rustprojects
mkdir testdir
cd testdir

Create a file in testdir using nano or your preferred text editor to store your Rust code:

nano test.rs

The .rs extension must be used for all Rust programs.

Save the file after pasting the following code into test.rs:

fn main() {
    println!("Congratulations! Your Rust program works.");
}

Use the rustc command to compile the code:

rustc test.rs

Execute the resulting file:

./test

In the terminal, the program prints:

Output

Congratulations! Your Rust program works.

Other Commonly-Used Rust Commands

On Ubuntu, it's a good idea to update your Rust installation frequently.

To upgrade Rust, type the following command:

rustup update

Rust and its related repositories can easily be deleted from your computer.

To remove Rust, enter the following command:

rustup self uninstall

If you want to continue with the uninstallation, you must enter Y:

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)

Enter Y to proceed:

Output
Continue? (y/N) y

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

Your system is cleaned of Rust.

FAQs to Install Rust on Ubuntu 20.04

What is the recommended way to update Rust on Ubuntu?

Updating Rust is simple. Open a terminal and use the command rustup update. This will update your Rust installation to the latest stable version.

How can I switch between Rust toolchain versions?

Rust allows you to switch between toolchain versions easily. Run rustup toolchain list to see installed versions, then use rustup default <version> or rustup override <version> to set the default or per-directory version.

What is Cargo, and why is it essential?

Cargo is Rust's package manager and build system. It simplifies project setup and management, handles dependencies, and builds your code. It's an important tool for any Rust development.

How can I create a new Rust project with Cargo?

Open a terminal, navigate to the desired directory, and run cargo new project_name. This will create a new Rust project with the given name, including a basic project structure.

Can I install Rust without using rustup?

While rustup is the recommended way to install Rust, you can install it from the Ubuntu package manager with sudo apt install rustc. However, the version provided might not always be the latest stable release.

Are there any integrated development environments (IDEs) for Rust on Ubuntu?

Yes, several popular IDEs support Rust on Ubuntu, including Visual Studio Code with the Rust extension, IntelliJ IDEA with the Rust plugin, and Emacs with the Rust mode.

How can I uninstall Rust from Ubuntu 20.04?

Open a terminal and run rustup self uninstall to remove Rust from your system completely. This command will also remove associated binaries and tools.

Conclusion

You have successfully installed Rust on your Ubuntu 20.04 system. If you have any queries, feel free to leave a comment below, and we'll be happy to help.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.