How to Install Rust Programming Language on Ubuntu 20.04
This tutorial exists for these OS versions
- Ubuntu 22.04 (Jammy Jellyfish)
- Ubuntu 20.04 (Focal Fossa)
On this page
Rust is an open-source and nowadays very popular programming language developed by Graydon Hoare in 2006. It is extremely fast, prevents segfaults, and guarantees thread and memory safety. It supports zero-cost abstractions, threads without data races, move semantics, efficient C bindings, minimal runtime, and pattern matching. It is very similar to C++ and can run on several platforms.
In this tutorial, I will show you how to install the Rust programming language on Ubuntu 20.04.
Prerequisites
- A server running Ubuntu 20.04.
- A root password is configured on the server.
Install Rust
To install Rust, you will need to install the Curl and other packages to your system.
apt-get install curl build-essential make gcc -y
After installing Curl package, download and run the Rust installation script as shown below:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
You will be asked to choose the installation options as shown below:
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: /root/.rustup This can be modified with the RUSTUP_HOME environment variable. The Cargo home directory located at: /root/.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: /root/.cargo/bin This path will then be added to your PATH environment variable by modifying the profile files located at: /root/.profile /root/.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 >1
Type 1 and hit Enter to continue. You should get the following 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 2021-06-17, rust version 1.53.0 (53cb7b09b 2021-06-17) info: downloading component 'cargo' info: downloading component 'clippy' info: downloading component 'rust-docs' info: downloading component 'rust-std' info: downloading component 'rustc' 48.4 MiB / 48.4 MiB (100 %) 26.8 MiB/s in 1s ETA: 0s info: downloading component 'rustfmt' info: installing component 'cargo' info: installing component 'clippy' info: installing component 'rust-docs' 16.1 MiB / 16.1 MiB (100 %) 1.9 MiB/s in 6s ETA: 0s info: installing component 'rust-std' 25.3 MiB / 25.3 MiB (100 %) 5.8 MiB/s in 4s ETA: 0s info: installing component 'rustc' 48.4 MiB / 48.4 MiB (100 %) 7.1 MiB/s in 7s 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.53.0 (53cb7b09b 2021-06-17) 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
After the installation, you will need to activate the Rust environment for your current shell. You can activate it using the following command:
source ~/.profile
source ~/.cargo/env
Next, verify the version of Rust using the following command:
rustc --version
You should get the following output:
rustc 1.53.0 (53cb7b09b 2021-06-17)
Create a Sample Application with Rust
Next, you will need to create a sample application to test the Rust.
First, create a directory for your application:
mkdir app
Next, change the directory to the app and create a sample application with the following command:
cd app
nano app.rs
Add the following code:
fn main() { println!("Hello World, this is my first app"); }
Save and close the file then compile the program with the following command:
rustc app.rs
This will create an executable called app in the current directory.
Next, run the program with the following command:
./app
You should see the following output:
Hello World, this is my first app
To update the Rust package, run the following command:
rustup update
You should get the following output:
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu' info: checking for self-updates stable-x86_64-unknown-linux-gnu unchanged - rustc 1.53.0 (53cb7b09b 2021-06-17)
If you want to remove the Rust from your system, run the following command:
rustup self uninstall
You should get the following 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 info: removing rustup home info: removing cargo home info: removing rustup binaries info: rustup is uninstalled
Conclusion
Congratulations! you have successfully installed Rust on Ubuntu 20.04 server. You can now write extremely fast code with a very low memory footprint using Rust.