Introduction
If you’re looking to install Rust on Ubuntu, the rustup tool makes the process quick and simple. Rust, a powerful programming language, is widely used for systems programming, and setting it up on Ubuntu 20.04 ensures a smooth development experience. This guide walks you through the necessary prerequisites, installation steps, and how to verify that Rust is properly installed. By the end of this article, you’ll be ready to create, compile, and run your first Rust program on Ubuntu. Let’s get started with setting up Rust and diving into its features!
What is Rust programming language?
Rust is a powerful programming language used for building software like browsers, game engines, and operating systems. It is designed for performance and safety, especially in handling memory management without relying on a garbage collector. This tutorial helps users install Rust on Ubuntu, test it, and understand how to use it for their programming projects.
Prerequisites
Alright, let’s get started! To make sure you can follow along and complete this tutorial without any hiccups, you’ll need to set up an Ubuntu 20.04 server with a few important settings. First, you want to make sure you have a non-root user with sudo privileges. Why? It’s all about security. You don’t want to be logging in as root all the time. By creating a sudo-enabled user, you’ll be able to do admin tasks safely without having to expose your root access.
And here’s the thing: while we’re setting up this server, we can’t forget about security. It’s super important to set up a firewall. Think of it like a security guard that keeps an eye on things, making sure nobody can get into your server without permission. You definitely don’t want to leave the door wide open for hackers, right?
Now, if you’re not sure how to go about this, don’t worry! There are guides available that’ll walk you through each step. These guides will help you create a sudo-enabled non-root user and set up the firewall, so everything runs smoothly.
Just remember, getting these steps right is key to making sure your Ubuntu server stays safe and runs efficiently. Once you have these basics set up, you’ll be ready for the rest of the tutorial, and you won’t have to worry about security issues as you jump into Rust. Trust me, you’ll be glad you did this first!
Note: Setting up the firewall and sudo user is crucial for server security.
For further help, visit the DigitalOcean Community Tutorials.
Step 1 — Installing Rust on Ubuntu Using the rustup Tool
So, you’ve decided to dive into the world of Rust on your Ubuntu system, and you’re probably wondering where to begin. Well, let me introduce you to rustup . It’s like your personal assistant for installing Rust—it simplifies the process by not only handling the installation of Rust itself but also all the important components, like the Rust compiler and the package manager, Cargo. It’s pretty much like having a guide show you exactly what to do to get everything running smoothly.
To get started, open up your terminal and run this command to download rustup :
$ curl –proto ‘=https’ –tlsv1.3 https://sh.rustup.rs -sSf | sh
Once you run that, rustup will start doing its magic. You’ll be asked to choose what kind of installation you want. The default option is to go ahead with the installation as it is, but if you’re familiar with rustup and want to tweak things, you can choose to customize it a bit.
Here’s what you’ll see after running the command, just so you know what’s going on behind the scenes:
sammy@ubuntu:~$ curl –proto ‘=https’ –tlsv1.3 https://sh.rustup.rs -sSf | sh
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
Proceed with installation (default)
Customize installation
Cancel installation
Once you go with the default installation option, you’ll see something like this as rustup continues its work:
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)
And voilà, Rust is installed on your system! But we’re not completely done yet. Before you start writing code, you’ll need to reload your shell so that the Rust tools, like cargo and rustc, are ready to go. Don’t worry, it’s an easy fix.
Just run this simple command to reload your environment:
$ source “$HOME/.cargo/env”
This ensures that the Rust toolchain directory gets added to your system’s PATH, which means you’ll be able to run Rust commands from anywhere in your terminal.
To be absolutely sure everything’s set up correctly, run this command again to make sure the Rust toolchain is fully loaded:
$ source $HOME/.cargo/env
Once that’s done, you’re all set! You can now dive into your first Rust project and start developing. With rustup, setting up Rust has never been easier, and you’re all ready to go! Enjoy the process and happy coding!
Step 2 — Verifying the Installation
Alright, now that Rust is up and running on your Ubuntu system, let’s make sure everything is working properly. The last thing you want is to start writing code, only to realize something isn’t quite right, right? So, let’s quickly check that the installation was successful and that you’ve got the right version of Rust running.
Here’s a simple way to confirm that everything’s in order: open up your terminal and type in this command:
$ rustc –version
What this command does is pretty simple—it asks your system to tell you what version of Rust (specifically the rustc compiler) is installed. If everything went smoothly, you should see something like this:
sammy@ubuntu:~$ rustc –version
rustc 1.66.1 (90743e729 2023-01-10)
sammy@ubuntu:~$
And there you have it—you’ve got confirmation! In this example, it shows that Rust version 1.66.1 is running. It even gives you the specific commit ID (90743e729) and the release date (2023-01-10), which is pretty handy if you ever need to check which exact build you’re using.
If everything looks good, congratulations! Rust is properly installed, and you’re all set to start developing. But, if you don’t see the expected output, no worries. Just take a step back, double-check your installation, and make sure your system’s PATH environment variable is set up right, so Rust can be found by your terminal.
Once everything checks out, you’re good to go! Now, you can dive into the world of Rust programming, no problem!
Make sure your terminal environment is properly set up to find Rust if the command doesn’t work.
Rust Programming: Getting Started
Step 3 — Installing a Compiler
Alright, now that we’re deep into setting up Rust on your Ubuntu system, there’s just one more important thing left: the compiler. Rust doesn’t just magically turn your code into an executable. There’s something behind the scenes called a “linker” that does the heavy lifting by piecing together your compiled outputs into one executable file. Think of it as the glue that holds all the parts of your program together.
Here’s the catch: Rust needs a specific linker to make this work. This linker is part of the GNU Compiler Collection (gcc), which is included in a package called build-essential . Without this tool, when you try to compile your code, you might run into an error that looks something like this:
error: linker `cc` not found
= note: No such file or directory (os error 2)
error: aborting due to previous error
Sounds a bit scary, right? But don’t worry, there’s a simple fix. To avoid this, we just need to install the build-essential package, which includes gcc and other important tools for compiling programs.
Let’s start with something easy: updating your system’s package list. This will make sure you’re working with the latest versions of everything. You can do that by running this command:
$ sudo apt update
If it asks for your password, go ahead and type it in. Your terminal will then check for any updates, and it will show you a list of packages that need upgrading. You might see something like this:
sammy@ubuntu:~$ sudo apt update
[sudo] password for sammy:
Hit:1 http://mirrors.caasify.com/ubuntu focal InRelease
Get:2 http://mirrors.caasify.com/ubuntu focal-updates InRelease [114 kB]
Hit:3 https://repos-caasify.com/apt/caa-agent main InRelease …
Fetched 11.2 MB in 5s (2131 kB/s)
Reading package lists… Done
Building dependency tree
Reading state information… Done
103 packages can be upgraded. Run ‘apt list –upgradable’ to see them.
Once that’s done, the next step is to update your system to the latest versions of your packages. Run:
$ sudo apt upgrade
If it asks, just press Y to continue, and your system will update all the outdated packages. Once that’s finished, we can move on to the next step: installing the build-essential package.
Run this command to install everything you need:
$ sudo apt install build-essential
Once again, you’ll be asked to confirm the installation. Just press Y to continue, and the installation will complete once your terminal returns to the command prompt without any errors. This means that all the necessary compiler and linker tools are now installed on your system.
And that’s it! Now you have everything you need to compile your Rust programs and move forward with your development. From here, you can start turning your awesome Rust code into real applications. So, go ahead and start building something amazing!
For more information on the GNU Compiler Collection (GCC), visit their official site.
GNU Compiler Collection (GCC)
Step 4 — Creating, Compiling, and Running a Test Program
Now that you’ve got Rust installed on your Ubuntu system, it’s time to take it for a spin! But before jumping into big projects, let’s start with a simple test. Think of it like your first “Hello, World!” moment with Rust—just a quick way to make sure everything’s working as it should.
First things first, let’s create a clean, organized spot to store your Rust project files. You don’t want to be searching all over your system later, right? So, let’s set up a directory for your project. Run these commands to create your project folder:
$ mkdir ~/rustprojects
$ cd ~/rustprojects
$ mkdir testdir
$ cd testdir
With that done, it’s time to write some code! You can use whatever text editor you prefer, but for now, let’s stick with nano—it’s a simple and easy-to-use command-line editor. To create a new file for your code, type this:
$ nano test.rs
Inside that file, paste the following simple Rust code. Don’t worry, it’s really easy—just a message to print out when everything’s working:
fn main() {
println!(“Congratulations! Your Rust program works.”);
}
Here’s a quick tip: Rust files need to have the .rs extension, so make sure to save it as test.rs . This tells the system, “Hey, this is Rust code!” Once done, save and close the file.
Next, it’s time to compile your code. Think of compiling like turning your raw ingredients into a fully baked cake—it’s where Rust takes your code and turns it into something the computer can run. To compile your program, just run this:
$ rustc test.rs
Once that’s done, you’ll have an executable file named test in the same directory. To run it, type:
$ ./test
And here’s the fun part! If everything went smoothly, your terminal should show this message:
sammy@ubuntu:~/rustprojects/testdir$ ./test
Congratulations! Your Rust program works.
sammy@ubuntu:~/rustprojects/testdir$
Boom! Just like that, you’ve confirmed that your Rust installation is working perfectly. Now you can relax and get excited about diving into more advanced Rust projects. But, if something’s not working, don’t worry. Double-check your Rust installation and make sure your environment variables are set up right. Once everything’s fixed, you’ll be ready to create amazing things with Rust on Ubuntu. Enjoy the ride!
For more information, check out the Rust Programming Guide.
Other Commonly-Used Rust Commands
Once you’ve got Rust installed and running on your Ubuntu system, you’ll want to keep it fresh and up-to-date. Think of it like taking care of your car: you wouldn’t want to drive around with old parts, right? It’s the same with Rust. To get the latest features, bug fixes, and performance improvements, it’s a good idea to update Rust regularly. Luckily, updating Rust is pretty simple with the rustup tool.
All you need to do is run this one simple command in your terminal:
$ rustup update
What this does is check for updates not only to Rust itself but also to all the other components, like the Rust compiler ( rustc ), the package manager ( cargo ), and any other tools you’re using. It’s a quick and easy way to make sure your Rust environment is always running the latest stable versions, keeping things smooth and efficient.
But what if you no longer need Rust on your system? Maybe you’re cleaning up your setup, or you’ve decided to go in a different direction with your projects. No worries—Rust is easy to uninstall. You can remove Rust, along with all its files and tools, in just a few steps.
To do this, run the following:
$ rustup self uninstall
After running that, your system will ask you to confirm that you want to remove everything. It’ll look like this:
sammy@ubuntu:~/rustprojects/testdir$ rustup self uninstall
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)
If you’re sure, just type Y and hit Enter. The system will then start removing everything related to Rust. You’ll see a bunch of messages like this:
Continue? (y/N) y
info: removing rustup home
info: removing cargo home
info: removing rustup binaries
info: rustup is uninstalled
sammy@ubuntu:~/rustprojects/testdir$
And just like that, Rust and all its components are gone from your system. If you ever want to reinstall it later, all you have to do is follow the installation steps again. Plus, this clean-up will free up some space if you’re trying to streamline your system.
So whether you’re keeping Rust updated or doing a little system refresh, these simple commands make managing Rust a piece of cake.
Rust Programming Language Guide
Conclusion
In conclusion, installing Rust on Ubuntu using the rustup tool is a straightforward process that sets up a powerful programming environment for systems development. By following this guide, you’ve learned how to handle prerequisites, perform the installation, verify your setup, and even run your first Rust program. Whether you’re new to Rust or just need a refresher on setting it up, this tutorial gives you all the essential steps to get started with Rust on Ubuntu. With updates and commands covered, you’re now ready to dive deeper into Rust development. Stay updated with future Rust releases to ensure your setup remains optimized and efficient.