Rust, or rust-lang
, is a versatile, general-purpose programming language with syntax similarities to C++. Employed in various software development projects, Rust is used in creating browser components, game engines, and even operating systems. This article will guide you through the installation of Rust on Ubuntu 20.04, demonstrating how to create, compile, and run a test program. This guide will use Rust version 1.66 as an example.
Prerequisites
Before beginning, make sure you have an Ubuntu 20.04 server with a non-root user with sudo privileges and a firewall. You can set this up by referring to the Initial Server Setup guide with Ubuntu 20.04.
Installation Process
There are several ways to install Rust on Linux, but the recommended method is to use the rustup
command-line tool.
Downloading and Installing Rust
To download the rustup
tool and install the latest stable version of Rust, run the following command:
curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh
You will be prompted to choose the type of installation.
shapehost@ubuntu:~$ curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh ... 1) Proceed with installation (default) 2) Customize installation 3) Cancel installation >
For the purposes of this article, we will proceed with the default installation (option 1). However, if you’re familiar with the rustup
installer and wish to customize your installation, you can select option 2.
Adding Rust to the PATH Environment Variable
After successful installation, add the Rust toolchain directory to the PATH environment variable by running:
source $HOME/.cargo/env
Verifying the Installation
Confirm the successful installation of Rust by checking the version:
rustc --version
This command returns the version of the Rust programming language installed on your system.
Installing a Compiler
Rust requires a linker program to join compiled outputs into a single file. The GNU Compiler Collection (gcc) in the build-essential
package includes a linker.
Updating the Apt Package Index
First, update the Apt package index:
sudo apt update
Upgrading Out-of-date Packages
Next, upgrade any out-of-date packages:
sudo apt upgrade
Installing the build-essential package
Finally, install the build-essential
package:
sudo apt install build-essential
Creating, Compiling, and Running a Test Program
In this section, you will create a test program to verify that Rust is working properly.
Creating Directories for the Test Script
Create some directories to store the test script:
mkdir ~/rustprojects cd ~/rustprojects mkdir testdir cd testdir
Creating and Saving the Rust Program
Use nano
, or your preferred text editor, to create a file in testdir
to store your Rust code:
nano test.rs
Rust programs should always use the .rs
extension.
Copy the following code into test.rs
and save the file:
fn main() { println!("Congratulations! Your Rust program works."); }
Compiling and Running the Test Program
Compile the code using the rustc
command:
rustc test.rs
Run the resulting executable:
./test
The program prints to the terminal:
shapehost@ubuntu:~/rustprojects/testdir$ ./test Congratulations! Your Rust program works. shapehost@ubuntu:~/rustprojects/testdir$
Other Common Rust Commands
It’s recommended to update your Rust installation regularly.
Updating Rust
To update Rust, run the following command:
rustup update
Uninstalling Rust
If necessary, you can also uninstall Rust and its associated repositories. Run the following command to uninstall Rust:
rustup self uninstall
You will be prompted to confirm the uninstallation process.
Conclusion
With Rust successfully installed and tested on Ubuntu, you are now ready to take on more advanced Ubuntu tutorials. Rust is a powerful language that offers performance, reliability, and the ability to develop robust applications, making it a favorite among developers.
The steps outlined in this article will ensure that you have a smooth installation process, providing you with the necessary tools to start your journey with Rust.
For high-performing Linux SSD VPS solutions, visit ShapeHost. With ShapeHost, you get efficient, scalable, and secure cloud hosting solutions that meet your business needs.