Rust is a powerful programming language that has gained popularity among developers due to its focus on safety, performance, and concurrency. In this article, we will guide you through the process of installing Rust on a Ubuntu 22.04 machine. We will explore two different methods for installation and provide step-by-step instructions to help you get started.
Prerequisites
Before we begin, let’s ensure that you have everything you need to install Rust on your Ubuntu 22.04 machine. You will need:
- A Ubuntu 22.04 machine (can be either Ubuntu Server or Desktop)
- A non-root user with sudo/root privileges
If you have these prerequisites in place, we can proceed with the installation.
Method 1: Installing Rust via APT
The first method we will explore is installing Rust via the APT package manager. This method is suitable if you only need to compile Rust applications and don’t require a complex development environment.
To begin, open a terminal and update your Ubuntu repositories by running the following command:
sudo apt update
Once the repositories are updated, you can install Rust by running the following command:
sudo apt install rustc
When prompted, enter ‘y’ to confirm the installation and press ENTER to proceed.
After the installation is complete, you can verify the version of Rust installed on your system by running the following command:
rustc -Vv
This will display the Rust version and other relevant information.
Now that Rust is installed, let’s create a simple ‘Hello World’ program to test it. Create a new file named ‘hello-world.rs’ using your preferred text editor:
sudo nano hello-world.rs
Add the following code to the file:
fnmain(){ println!("Hello, World!"); }
Save the file and exit the editor.
Next, compile the Rust file using the ‘rustc’ command:
rustc hello-world.rs
This will generate an executable file named ‘hello-world’ in your current working directory.
You can now run the executable by executing the following command:
./hello-world
If everything is set up correctly, you should see the output “Hello, World!”.
To uninstall Rust from your machine, you can use the following command:
sudo apt remove rustc
Method 2: Installing Rust via rustup
The second method we will explore is installing Rust via rustup. This method is recommended if you want to become a Rust developer and need a more flexible and customizable development environment.
To begin, open a terminal and run the following command to install the necessary dependencies:
sudo apt install curl build-essential gcc make
Once the dependencies are installed, you can proceed with the installation of rustup and Rust. Run the following command to download and execute the rustup installer:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
The installer will provide instructions for setting up the Rust installation on your system. You can choose the default options by pressing ENTER.
After the installation is complete, you will need to reload your shell to apply the changes to the PATH environment variables. You can do this by running the following command:
source "$HOME/.cargo/env"
To verify the Rust version and the path of the Rust binary, run the following commands:
rustc -Vv
which rustc
With rustup installed, you can now manage multiple versions of Rust and development tools. You can install additional toolchains, switch between them, and update your Rust environment using the rustup command.
To install a different version of Rust, you can use the following command:
rustup toolchain install nightly
This will install the nightly version of Rust alongside the default stable version.
To switch to a different toolchain, you can use the following command:
rustupdefault nightly
This will set the nightly version as the default for your current user.
To update your Rust installation and all installed toolchains, use the following command:
rustup update
This will synchronize your Rust environment with the latest versions available.
Managing Rust Environment with rustup
rustup provides a set of commands that allow you to manage your Rust environment effectively.
Setting up rustup Command Autocomplete
To enable command autocomplete for the rustup command, follow these steps:
- Create a new directory for the autocomplete script:
mkdir -p ~/.local/share/bash-completion/completions/
- Generate the rustup autocomplete script:
rustup completions bash > ~/.local/share/bash-completion/completions/rustup
- Load the autocomplete script to your shell:
source ~/.local/share/bash-completion/completions/rustup
Now you can use the TAB key to autocomplete rustup commands.
Installing Multiple Versions of Rust and Development Tools
rustup allows you to install and manage multiple versions of Rust and development tools. Here’s how you can install and switch between different toolchains:
- Check the list of installed toolchains:
rustup toolchain list
- Install a new version of Rust:
rustup toolchain install nightly
- Switch to a different toolchain:
rustup default nightly
- Verify the Rust version and binary path:
rustc -Vv
which rustc
By installing and switching between toolchains, you can easily manage different versions of Rust for different projects.
Setup Profiles in rustup
rustup provides profiles that group components of your Rust environment. There are three different profiles available: minimal, default, and complete.
To check the current profile applied to your environment, run the following command:
rustup show profile
To switch to a different profile, use the following command:
rustup set profile minimal
You can revert to the default profile by running:
rustup set profiledefault
The profile determines which components are installed in your Rust environment. You can manually add or remove components using the rustup command.
Updating Rust via rustup
One of the advantages of using rustup is the ability to easily update your Rust installation and development tools. To update your Rust environment, run the following command:
rustup update
This will update all installed toolchains and components to the latest versions available.
Create and Manage Rust project with Cargo
Cargo is the package manager and build system for Rust. It makes it easy to create, build, and manage Rust projects. Let’s explore how to use Cargo to create a new Rust project:
- Create a new project named “hello-world”:
cargo new hello-world
- Change into the project directory:
cd hello-world
- Open the main.rs file in your preferred text editor:
nano src/main.rs
- Replace the default code with the following:
fnmain(){ println!("Hello, world!"); }
- Save the file and exit the editor.
Now that you have a Rust project set up, you can use Cargo to build, test, and run your code. Here are some useful Cargo commands:
- Build the project:
cargo build
- Run the project:
cargo run
- Test the project:
cargo test
Cargo will handle all the necessary dependencies and build configurations for your project, making it easier to manage and maintain.
Uninstalling Rust and rustup
If you decide to remove Rust and rustup from your system, you can do so by following these steps:
- Remove the Rust installation directory:
rm -rf ~/.cargo
- Remove the rustup installation directory:
rm -rf ~/.rustup
- Remove the rustup command autocomplete:
rm -f ~/.local/share/bash-completion/completions/rustup
With these steps, you have successfully uninstalled Rust and rustup from your system.
Conclusion
In this article, we have explored two different methods for installing Rust on a Ubuntu 22.04 machine. We have covered the installation process using the APT package manager and the rustup toolchain multiplexer. We have also learned how to manage multiple versions of Rust, switch between toolchains, and use Cargo to create and manage Rust projects.
Rust is a powerful programming language that offers safety, performance, and concurrency. By following the installation instructions provided in this article, you can start exploring the features and benefits of Rust programming. Whether you choose to install Rust via APT or rustup, you will have a solid foundation for developing efficient and reliable applications.
If you are looking for a reliable cloud hosting provider to support your Rust projects, consider Shape.host. Shape.host offers scalable and secure Cloud VPS solutions that can meet the needs of your growing applications. With their reliable services and excellent customer support, Shape.host is the perfect choice for your Rust development environment.