How to Install Rustup on Linux ?

Rustup is the standard and central tool which is used to download and install different versions of the Rust compiler from the official channels. This allows for easy switching between the various versions of the compilers namely, stable, beta and nightly. 

But why Rust ? Well, Rust is a systems level programming language with performance comparable to that of C and C++ whilst prioritising the memory safety of the program through its borrowing and ownership rules, and has been gaining popularity as of recently among developers. This is evident by the fact that the number of Rust developers is growing, as well as the adoption of the language in large codebases.

Apart from that, unlike C and C++, Rust has an official package manager (Cargo) to easily find and install packages, simplify dependency, project management, building, testing and publishing Rust Projects. Also, another benefit of the borrowing and ownership rules appears in concurrent programming. As Rust prevents race conditions at compile time. Also, async/await syntax provided in Rust makes asynchronous programming less error-prone.

But how do you handle the potentially multiple versions of Rust you’ll inevitably encounter across your projects ? That’s where Rustup comes into the picture.

With Rustup, you can compile Rust programs to run on different platforms and architecture. This is especially beneficial in the case of embedded systems programming, or older code bases written with older versions of Rust in mind, as you can simply set up the version of Rust you want for each project.

Installation

Method 1 : Using curl (recommended method)

You can directly install Rustup using the command line through the following command, irrespective of your Linux distribution –

curl https://sh.rustup.rs -sSf | sh
Installing Rustup Using Curl
Installing Rustup Using Curl Part-1

At this point, you have to type 1 and press enter to proceed with the installation, or if you want to customize the installation, you can type 2 and customize the settings as per your requirements. By default, the stable version of Rust will be initialized (can be changed according later on).

Installing Rustup Using Curl 2
Installing Rustup Using Curl Part-2

Finally, you have to set up the PATH environment variables to include cargo (Rust’s package manager) in your PATH. For that, you have to execute the following command –

souce "$HOME/.cargo/env"

Method 2 : Using package manager

You can also install Rustup using the default package manager provided in your distribution. In my case, I’m using Manjaro (Arch-based distribution) with the default package manager being pacman, so in this case, I’ll be running the following command –

pacman -Sy rustup

But you can modify the above command according to the default package manager of your distro.

Method 3 : Downloading the rustup-init from the website

In case you want to manually download the rustup-init file, you can do so from here. Then you proceed normally like you install downloaded apps.

Verifying the Installation

Let’s check if Rustup is properly installed by typing rustup in the terminal and pressing enter. On doing so, you’ll get a similar output as shown below, if not, then you might want to repeat the installation steps you followed.

Confirming Installation 1
Confirming Installation

We’re further going to check if Rust is properly working on the system by building a simple “Hello World” program and running it. To do so, write the following code in a file (make sure the file extension is .rs). In my case, I’ve written the following code in a file named “prog1.rs” which is stored in the folder named “sample”

fn main(){
     println!("Hello World !");
}

Now, you have to navigate to the folder and run the following commands to run the program –

rustc prog1.rs
./prog1
Testing Rustup
Testing Rust installation

Conclusion

In this module, we discussed Rustup, which is the official toolchain installer for Rust. We also learned how to install Rustup on Linux, as well as wrote a simple “Hello World” program in Rust. If you want to learn more about the Rust programming language, you can refer to the Rust Book. If you want to learn more about Rustup, here’s the link to its documentation.

How do I install Rust on Ubuntu?

You can install Rust on Ubuntu using the rustup installer. Open your terminal and run the following command: `curl –proto ‘=https’ –tlsv1.2 -sSf https://sh.rustup.rs | sh`. Follow the onscreen instructions to complete the installation.

What is rustup?

Rustup is the official installer and version management tool for Rust. It allows you to manage Rust versions and associated toolchains easily.

Can I install Rust on Ubuntu using apt?

Yes, you can install Rust on Ubuntu using the apt package manager, but it’s recommended to use rustup for the latest version. To install using apt, you would run `sudo apt update` and then `sudo apt install rustc cargo`.

What are the steps to uninstall Rust?

To uninstall Rust, you can use the command `rustup self uninstall` if you installed it using rustup. If you installed it via apt, you can remove it with `sudo apt remove rustc cargo`.

What is the command to check the installed version of Rust?

You can check the installed version of Rust by running the command `rustc –version` in your terminal.

What is the difference between option 1 and option 2 for installing Rust?

Option 1 refers to using the rustup installer which is the recommended way to install Rust, while option 2 refers to using the apt package manager to install Rust directly from the Ubuntu repositories.

How do I manage multiple Rust toolchains?

You can manage multiple Rust toolchains using rustup. You can install a specific toolchain version with the command `rustup install ` and switch between them using `rustup default `.

Can I use Rust on Windows Subsystem for Linux?

Yes, Windows Subsystem for Linux users can install Rust by running the rustup installer in their terminal just like a regular Linux user. Run the command `curl –proto ‘=https’ –tlsv1.2 -sSf https://sh.rustup.rs | sh` to start the installation.

What should I do if I encounter an error during the installation?

If you encounter an error during the installation of Rust, ensure that your system is updated and that you have the necessary dependencies. You can run `apt update` and install any missing packages. If the problem persists, refer to the Rust documentation or community forums for troubleshooting tips.

How can I start using Rust after installation?

After installation, you can start using Rust by creating a new project with `cargo new project_name`, navigating into the project directory, and running `cargo run` to execute your Rust code.