Introduction
The Rust programming language,
also known as rust-lang, is a powerful general-purpose programming language. Rust is syntactically similar to C++ and is used for a wide range of software development projects, including browser components, game engines, and operating systems.
In this tutorial, you will install the latest version of Rust on Ubuntu 20.04 and then create, compile and run a test program. The examples in this tutorial show the installation of Rust version 1.66.
Prerequisites To
complete this tutorial, you will need an Ubuntu 20.04 server with a sudo-enabled non-root user and a firewall. You can configure this by following our tutorial Initial Server Setup with Ubuntu 20.04.
Step 1 — Installing
Rust on Ubuntu Using the rustup tool
Although there are several different ways to install Rust on Linux, the recommended method is to use the
rustup command line tool.
Run the command to download the rustup tool and install the latest stable version of Rust
: curl -proto ‘=https’ -tlsv1.3 https://sh.rustup.rs -sSf | sh
You will be asked to choose the installation type
: Outputsammy@ubuntu:~$
- curl -proto ‘=https’ -tlsv1.3 https://sh.rustup.rs -sSf |
sh info: Download the 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 in 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 the Cargo bin directory, located at: /home/sammy/.cargo/bin This path will 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: Triple default host: x86_64-unknown-linux-gnu Default Toolchain: Stable (Default) Profile: Default Modify PATH Variable: Yes 1) Continue with installation (default) 2) Customize the installation 3) Cancel the installation >
This tutorial uses the default option 1. However, if you are familiar with the rustup installer and want to customize your installation, you can choose option 2. Type your selection and press Enter.
The result of option 1 is:
Outputinfo: 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: download component ‘cargo’ info: download component ‘clippy’ info: download component ‘rust-docs’ info: download component ‘rust-std’ info: download component ‘rustc’ 67.4 MiB / 67.4 MiB (100%) 40.9 MiB/s in 1s ETA: 0s info: downloading component ‘rustfmt’ info: component installation ‘cargo’ 6.6 MiB / 6.6 MiB (100%) 5.5 MiB/s in 1s ETA: 0s info: component installation ‘clippy’ info: component installation ‘rust-docs’ 19.1 MiB / 19.1 MiB (100%) 2.4 MiB/s in 7s ETA: 0s info: component installation ‘rust-std’ 30.0 MiB / 30.0 MiB (100%) 5.6 MiB/s in 5s ETA: 0s info: component installation ‘rustc’ 67.4 MiB / 67.4 MiB (100%) 5.9 MiB/s on 11s ETA: 0s info: component installation ‘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. Not bad! To get started, you may need to restart the current shell. This would reload the PATH environment variable to include the Cargo bin directory ($HOME/.cargo/bin). To configure the current shell, run: source “$HOME/.cargo/env” sammy@ubuntu:~$
Then run the following
command to add the Rust toolchain directory to the PATH environment variable:
- source $HOME/.cargo/env
Step 2 — Verifying
the installation
Verify the Rust installation by requesting the
version: rustc -version The
- rustc -version
command returns the version of the Rust programming language installed on the system. For example
: Outputsammy@ubuntu:~$ rustc -version rustc 1.66.1 (90743e729 2023-01-10) sammy@ubuntu:~$
Step 3 — Installing
a compiler
Rust requires a linker program to join the compiled outputs into one file. The GNU Compiler Collection (gcc) in the build-essential package includes a linker. If you do not install gcc, you may get the following error when trying to compile: Error: Linker
‘cc’ not found | = Note: No such file or directory (OS error 2) error: aborting due to previous error You will use apt to install the
essential build
package.
First, update the Apt package index:
- sudo apt update
Enter your password to continue if prompted. The apt update command generates a list of packages that can be updated. For example
: Outputsammy@ubuntu:~$ sudo apt update [sudo] password for sammy: Hit:1 focal http://mirrors.digitalocean.com/ubuntu InRelease Get:2 http://mirrors.digitalocean.com/ubuntu focal-updates InRelease [114 kB] Hit:3 main https://repos-droplet.digitalocean.com/apt/droplet-agent InRelease Get:4 http://mirrors.digitalocean.com/ubuntu focal-backports InRelease [108 kB] Get:5 http://security.ubuntu.com/ubuntu Focal Security InRelease [114 kB] Get:6 http://mirrors.digitalocean.com/ubuntu focal-updates/main amd64 Packages [2336 kB] Get:7 focal-updates/main Translation http://mirrors.digitalocean.com/ubuntu [403 kB] Get:8 http://mirrors.digitalocean.com/ubuntu focal-updates/main amd64 c-n-f Metadata [16.2 kB] Get:9 http://mirrors.digitalocean.com/ubuntu focal-updates/restricted amd64 Packages [1560 kB] Get:10 http://mirrors.digitalocean.com/ubuntu focal-updates/restricted Translation [220 kB] Get:11 http://mirrors.digitalocean.com/ubuntu focal updates/amd64 restricted c-n-f Metadata [620 B] Get:12 http://mirrors.digitalocean.com/ubuntu focal-updates/universe amd64 packages [1017 kB] Get:13 http://mirrors.digitalocean.com/ubuntu focal-updates/universe Translation [236 kB] Get:14 http://mirrors.digitalocean.com/ubuntu focal-updates/universe amd64 c-n-f Metadata [23.2 kB] Get:15 http://mirrors.digitalocean.com/ubuntu focal-updates/multiverse amd64 Packages [25.2 kB] Get:16 http://mirrors.digitalocean.com/ubuntu focal-updates/multiverse Translation [7408 B] Get:17 http://mirrors.digitalocean.com/ubuntu focal-updates/multiverse amd64 c-n-f Metadata [604 B] Get 18 focal-backports/main http://mirrors.digitalocean.com/ubuntu amd64 Packages [45.7 kB] Get 19 focal-backports/main Translation http://mirrors.digitalocean.com/ubuntu [16.3 kB] Get to:20 http://mirrors.digitalocean.com/ubuntu focal-backports/main amd64 c-n-f Metadata [1420 B] Get:21 http://mirrors.digitalocean.com/ubuntu Fpal-backports/universe amd64 packages [24.9 kB] Get:22 http://mirrors.digitalocean.com/ubuntu focal-backports/universe Translation [16.3 kB] Get:23 http://mirrors.digitalocean.com/ubuntu Focal-backports/universe AMD64 C-N-F Metadata [880 B] Get:24 http://security.ubuntu.com/ubuntu Focal Security Packages/Main amd64 [1960 kB] Get:25 http://security.ubuntu.com/ubuntu focal-security/main Translation [320 kB] Get:26 http://security.ubuntu.com/ubuntu Sensory-Security/Main amd64 c-n-f Metadata [11.7 kB] Get:27 http://security.ubuntu.com/ubuntu Restricted Focal Security Packages/amd64 [1463 kB] Get:28 http://security.ubuntu.com/ubuntu Focal Security/restricted Translation [207 kB] Obtain:29 http://security.ubuntu.com/ubuntu Focal Security/restricted amd64 c-n-f Metadata [624 B] Get:30 http://security.ubuntu.com/ubuntu Focal-security/universe AMD64 Packages [786 kB] Get Obtain:31 http://security.ubuntu.com/ubuntu Furniture-focal security/universe Translation [152 kB] Get:32 http://security.ubuntu.com/ubuntu Furniture-focal / universe amd64 c-n-f Metadata [16.9 kB] Get Obtain:33 http://security.ubuntu.com/ubuntu Focal Security Packages/Multiverse amd64 [22.2 kB] Get:34 http://security.ubuntu.com/ubuntu Focal Security/Multiverse Translation [5464 B] Get:35 http://security.ubuntu.com/ubuntu Focal Security/Multiverse amd64 c-n-f Metadata [516 B] Obtained 11.2 MB in 5s (2131 kB/s) Reading package lists… Done Creation of dependency tree Reading status information… Done 103 packages can be updated. Run ‘apt list -updatable’ to view them. sammy@ubuntu:~$
Next, update the outdated packages
: sudo apt upgrade Type Y if you are prompted to continue with the updates.
When the updates are complete,
install the build-essential package:
- sudo apt install build-essential
Type Y
when you are prompted to continue with the installation. The installation is complete when the terminal returns to the command prompt without error messages.
Step 4: Create, build
, and run a test program In this step, you will create a test program to test Rust
and verify that it works correctly.
Start by creating some directories to store the test script
: mkdir ~/rustprojects
- cd ~/rustprojects mkdir
- testdir
testdir cd testdir Use nano, or your favorite text editor, to create a file in
to
store your Rust code:
- nano test.rs
You must use the .rs extension for all your Rust programs.
Copy the following code to test.rs and save the file:
fn main() { println!( “Congratulations! Your Rust program works.”); }
Compile the code using the command
rustc:
- rustc test.rs
Run the resulting executable
: ./test
The program prints to the terminal
: Outputsammy@ubuntu:~/rustprojects/testdir$
- ./test
Congratulations! Your Rust program works. sammy@ubuntu:~/rustprojects/testdir$
Other commonly used Rust commands It’s
a good idea to update your Rust installation on Ubuntu regularly
. Enter the following command to update Rust: Rustup Update You
can also remove Rust from your system, along with its associated repositories.
Enter the following command
to uninstall Rust:
- Rustup Self-Uninstall
You will be prompted to type Y to continue with the uninstall process:
Outputammy@ubuntu:~/rustprojects/testdir$ rustup auto uninstall Thanks for hacking in Rust! This will uninstall all toolchains and data from Rust, and remove $HOME/.cargo/bin from your PATH environment variable. Continue? (s/n)
Type Y to continue:
OutputContinue? (y/N) and info: Rustup Removal Startup Information: Load Removal Start Information: Rustup binary file removal Info: Rustup is uninstalled sammy@ubuntu:~/rustprojects/testdir$ Rust
is removed from your system
.
Conclusion
Now that you have installed and tested Rust on Ubuntu, continue your learning with more Ubuntu tutorials.