Home Rust How To Install Rust Programming Language In Linux

How To Install Rust Programming Language In Linux

Beginner's Tutorial: Setting Up Rust Using Rustup on Linux Systems

By sk
9.1K views

This detailed tutorial provides a concise introduction to the Rust programming language, followed by clear instructions on how to install Rust using rustup on Linux. It also guides you through updating Rust, creating and running a sample Rust program, and finally, how to uninstall Rust from your Linux system.

Introduction to Rust Programming Language

Rust, also known as rust-lang, is a modern, fast, cross-platform, and open-source systems programming language designed to replace C/C++ while offering high-level abstractions that appeal to developers familiar with C# and Java.

Rust boasts a plethora of features, including:

  • Zero-cost abstractions: Rust's abstractions incur no runtime overhead, ensuring performance comparable to native code.
  • Move semantics: Rust's ownership model utilizes move semantics to efficiently transfer ownership of data between variables, eliminating the need for manual memory management.
  • Guaranteed memory safety: Rust's strong type system and ownership model enforce memory safety, preventing memory-related errors like dangling pointers and memory leaks.
  • Threads without data races: Rust's type system and ownership model also ensure thread safety, preventing data races and ensuring consistent memory access across threads.
  • Trait-based generics: Rust's generic programming capabilities are based on traits, enabling flexible and reusable code that works with a variety of data types.
  • Pattern matching: Rust's pattern matching syntax allows for concise and expressive code, particularly when dealing with data structures like enums and tuples.
  • Type inference: Rust's type inference system automatically deduces the types of variables and expressions, reducing the need for explicit type declarations.
  • Minimal runtime: Rust's minimal runtime eliminates the overhead of garbage collection, further enhancing its performance.
  • Efficient C bindings: Rust seamlessly integrates with C code, enabling interoperability with existing C libraries.

Rust is actively used in production by prominent organizations such as Canonical, Chef, Coursera, CoreOS, Dropbox, Mozilla, and NPM.

Install Rust Programming Language in Linux

The Rust language can be installed in a few different ways. The officially recommended method for installing Rust is to use Rustup, the official Rust toolchain installer.

Installing rust using rustup is quite simple. All you have to do is open your Terminal and run the following command:

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

Or,

$ curl https://sh.rustup.rs -sSf | sh

Type 1 (number one) to proceed the installation with default values. Or, type 2 to customize the installation. I am going with default values, so I typed 1.

Sample output:

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/ostechnix/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  /home/ostechnix/.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/ostechnix/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /home/ostechnix/.profile
  /home/ostechnix/.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

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

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-11-16, rust version 1.74.0 (79e9716c9 2023-11-13)
info: downloading component 'cargo'
  8.2 MiB /   8.2 MiB (100 %)   3.8 MiB/s in  2s ETA:  0s
info: downloading component 'clippy'
[...]
 58.7 MiB /  58.7 MiB (100 %)  16.1 MiB/s in  4s 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.74.0 (79e9716c9 2023-11-13)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source "$HOME/.cargo/env"
Install Rust Programming Language Using rustup in Linux
Install Rust Programming Language Using rustup in Linux

As seen in the output above, the rustup installer script downloads and installs the official compiler for the Rust programming language, along with its package manager, Cargo. It also adds commands like cargo, rustc, rustup, and others to Cargo's bin directory, located at ~/.cargo/bin. Furthermore, the script modifies the profile file located at ~/.profile to add Cargo's bin directory to your PATH environment variable.

Finally, run the following command to configure your current shell:

$ source $HOME/.cargo/env

To verify the installed version, run:

$ rustc --version
rustc 1.74.0 (79e9716c9 2023-11-13)

Alright! We have just installed the latest Rust version. Let us go ahead and see if it works by creating a sample rust program.

Create a Sample Program in Rust

Make a main project directory to put all your Rust code in. This directory works as parent directory to all our Rust programs.

For example, I am going to create a main project directory named "my_rust_projects" in my $HOME directory.

$ mkdir ~/my_rust_projects

Go to that directory:

$ cd ~/my_rust_projects

Next create a binary 'hello_world' package using Cargo as shown below.

$ cargo new hello_world

The above command will create a new directory named "hello_world" with all necessary files in it.

CD into that directory:

$ cd hello_world

Finally run hello_world program using command:

$ cargo run

This command compiles and runs your program. You should see "Hello, world!" printed in the terminal.

Sample output:

   Compiling hello_world v0.1.0 (/home/ostechnix/my_rust_projects/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.14s
     Running `target/debug/hello_world`
Hello, world!
Run a hello world program in Rust
Run a hello world program in Rust

This is the recommended way to use Cargo.

Alternatively, you can manually create the project's directory, write the code in a text file and finally compile and run it like below.

Let us create the project directory:

$ mkdir hello_world

Cd into that directory:

$ cd hello_world

Now write your first Rust program using any text or GUI editors. The rust files always ends with extension .rs.

To write a sample rust program, for example ostechnix.rs, do:

$ vi ostechnix.rs

Copy and paste the following code in it.

fn main() {
    println!("Hello, Welcome To OSTechNix blog!");
}

Press ESC key and type :wq to save and quit the file.

Next run the following command to compile the rust code.

$ rustc ostechnix.rs

The above command will create a executable rust program named ostechnix in the current directory.

$ ls
ostechnix ostechnix.rs

Finally run the Rust program with command:

$ ./ostechnix 
Hello, Welcome To OSTechNix blog!
Run Hello World Program in Rust programming language
Run Hello World Program in Rust programming Language in Linux

It's working!!

Rust Learning Resources: Consider checking out The Rust Programming Language book online for comprehensive learning.

Troubleshooting Rust

Sometimes you may get "error: linker cc not found" message when compiling programs with Cargo. To fix this install a C compiler like GCC as described in the following link.

Enable Tab Completion

Rustup supports tab completion for popular shells such as Bash, Fish, Zsh and Powershell.

To enable Tab completion for Bash, do:

$ mkdir -p ~/.local/share/bash-completion/completions
$ rustup completions bash >> ~/.local/share/bash-completion/completions/rustup

On Fish:

$ mkdir -p ~/.config/fish/completions
$ rustup completions fish > ~/.config/fish/completions/rustup.fish

On Zsh:

$ mkdir ~/.zfunc

Then add the following lines to your '.zshrc' file just before 'compinit':

fpath+=~/.zfunc

Now you can install the completions script using the following command:

$ rustup completions zsh > ~/.zfunc/_rustup

After enabling tab completion, you should log out and log back in to your shell session for the changes to take effect.

Update Rust

To update rust when a new version is released, run:

$ rustup update
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2018-03-29, rust version 1.25.0 (84203cac6 2018-03-25)
info: downloading component 'rustc'
info: downloading component 'rust-std'
info: downloading component 'cargo'
info: downloading component 'rust-docs'
info: removing component 'rustc'
info: removing component 'rust-std'
info: removing component 'cargo'
info: removing component 'rust-docs'
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: installing component 'rust-docs'
info: checking for self-updates

stable-x86_64-unknown-linux-gnu updated - rustc 1.25.0 (84203cac6 2018-03-25)

This command also checks for updates to rustup and automatically installs the latest version.

If you want to manually check for updates and install the latest version of rustup without updating installed toolchains, type:

$ rustup self update

Uninstall Rust

You can uninstall rust language at any time using command:

$ rustup self uninstall

This command will delete Rust from your system, and all the above changes will be reverted.

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) y

info: removing rustup home
info: removing cargo home
info: removing rustup binaries
info: rustup is uninstalled

Finally, remove your Rust project's parent directory.

$ rm -fr ~/my_rust_projects

Frequently Asked Questions (FAQ)

Q: What is Rust programming language?

A: Rust is a modern, efficient, and safe systems programming language. It's designed for performance and reliability, offering features like zero-cost abstractions and guaranteed memory safety.

Q: How do I install Rust on Linux?

A: You can install Rust on Linux using rustup, which is the Rust toolchain installer. Run the command curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh in your terminal, and follow the on-screen instructions.

Q: What is rustup?

A: rustup is a command-line tool for managing Rust versions and associated tools. It simplifies installing, updating, and managing the Rust programming environment.

Q: How do I update Rust using rustup?

A: To update Rust, simply open your terminal and run rustup update.

Q: Can I write and run a Rust program immediately after installation?

A: Yes, once Rust is installed, you can write and run Rust programs. Use Cargo, Rust's package manager and build system, to create and manage your projects.

Q: How do I create a Rust program?

A: Use the command cargo new project_name to create a new Rust project. This creates a new directory with the necessary files to start coding.

Q: How do I run a Rust program?

A: Navigate to your project directory and run cargo run. Cargo will compile and execute your Rust program.

Q: What if I want to remove Rust from my Linux system?

A: To uninstall Rust and rustup, run rustup self uninstall in your terminal.

Q: Is Rust used in real-world applications?

A: Absolutely! Rust is used by numerous organizations for various applications, including network services, embedded systems, and even as part of operating systems.

Q: Where can I find more resources to learn Rust?

A: The official Rust website (rust-lang.org) offers extensive documentation. The online book "The Rust Programming Language" is also a great resource for beginners.

And, that's all for now. You know now how to install Rust using rustup , how to update it, create and run a sample rust program and finally how to remove rust from your system. Hope this was useful.

Resources:

You May Also Like

4 comments

Alex-PK April 23, 2018 - 10:16 pm

You don’t need to create the project directory manually and run rustc directly. The recommended way is to use cargo:

cd ~/my_rust_projects

cargo new hello_world

cd hello_world

cargo run

Reply
sk January 2, 2020 - 5:08 pm

Thanks for pointing it out. Updated the guide with your inputs.

Reply
Terence Golightly February 18, 2020 - 2:39 am

Hi Ostechnix,

This unfortunately does not work for me. I’m running omv with a pve kernel bash shell 4.4.12(1)
$ uname -vm
#1 SMP PVE 4.15.18-52 (Thu, 05 Dec 2019 10:14:17 +0100) x86_64

$ which curl
/usr/bin/curl

$ which sh
/bin/sh

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

$ curl https://sh.rustup.rs -sSf | sh
-bash: $: command not found
I tried placing the quotes around the url n/c
tried quoting the whole string url to sh same result.
Havn’t tried to install rustc using apt. Just thought there might be some input you can provide me with.
tx

Reply
sk February 18, 2020 - 11:17 am

Hey, I haven’t tested it in OpenMendiaVault. So, don’t have any working solution for now. My apologies. Please try the following command and see if it works:

curl –proto ‘=https’ –tlsv1.2 -sSf https://sh.rustup.rs | sh

If it doesn’t work, please file an issue in GitHub. https://github.com/rust-lang/rust/issues

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More