Skip to main content

Installing Rust on Ubuntu

Rust is a programming language that offers the performance of C++ with the safety and ease of use of higher-level languages like Python or JavaScript. In this tutorial, we will walk through the step-by-step process of installing Rust on Ubuntu.

Prerequisites

Before we begin, make sure you have the following prerequisites:

  • A system running Ubuntu (version 18.04 or later)
  • Internet access
  • A user account with sudo privileges

Step 1: Update Package Index

First, let's update the package index on our system by opening a terminal (Ctrl+Alt+T) and running the following command:

sudo apt update

Step 2: Install Rust

Now, we're ready to install Rust. Rust provides an installation script, rustup, which manages Rust versions and associated tools. To install rustup, run the following command in the terminal:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

This command will download and run the rustup-init.sh script, which will guide you through the installation process. Press Enter to proceed with the default installation.

Once the installation is complete, you'll see the following message:

Rust is installed now. Great!

Step 3: Configure Rust Environment

To use Rust, we need to add it to our system's PATH environment variable. Open a new terminal window or run the following command to reload the environment variables:

source $HOME/.cargo/env

Step 4: Verify Rust Installation

Let's verify that Rust is installed correctly by checking its version. Run the following command:

rustc --version

You should see the installed Rust version, such as rustc 1.55.0 (xxx), printed on the terminal.

Step 5: Update Rust

Periodically, it's good practice to update Rust to the latest stable version. To do this, run the following command:

rustup update

This command will check for updates and install the latest version if available.

Example 1: Hello, World!

Create a new file named main.rs using your preferred text editor and add the following code:

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

Save the file and exit the text editor. Open a terminal and navigate to the directory containing main.rs. To compile and run the code, execute the following command:

rustc main.rs && ./main

You should see the output Hello, World! printed on the terminal.

Step 6: Uninstall Rust (Optional)

If, at any point, you decide to uninstall Rust from your system, you can use the following command:

rustup self uninstall

Please note that this command will remove all Rust components and cannot be undone. Proceed with caution.

Conclusion

Congratulations! You have successfully installed Rust on your Ubuntu machine. You can now start developing powerful and safe applications using Rust's language features and ecosystem. Remember to keep Rust and Cargo updated regularly to benefit from the latest improvements and bug fixes.

If you encounter any issues during installation, make sure to consult the official Rust documentation or seek help from the Rust community. Happy coding with Rust!