Install Rust Programming Language In Linux

Install Rust Programming Language In Linux

Rust or rust-lang, is a modern, fast, cross-platform, and open source systems programming language designed to replace C/C++ but with high-level abstractions to please those coming from C# and Java. It comes with many features including zero-cost abstractions, move semantics, guaranteed memory safety, threads without data races, trait-based generics, pattern matching, type inference, minimal runtime, and efficient C bindings etc. Rust is being actively used in production by popular organizations such as Canonical, Chef, Coursera, CoreOS, Dropbox, Mozilla, NPM and many. Today, we are going to learn to install Rust programming language in Linux.

Install Rust Programming Language In Linux

The Rust language can be installed in a few different ways. The officially recommended way to install Rust is by using Rustup, the 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 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.

It will add the cargo, rustc, rustup and other commands to Cargo's bin 
directory, located at:

/home/sk/.cargo/bin

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

/home/sk/.profile

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
 modify PATH variable: yes

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

info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
270.0 KiB / 270.0 KiB (100 %) 179.7 KiB/s ETA: 0 s 
info: latest update on 2018-03-29, rust version 1.25.0 (84203cac6 2018-03-25)
info: downloading component 'rustc'
 55.2 MiB / 55.2 MiB (100 %) 2.6 MiB/s ETA: 0 s 
info: downloading component 'rust-std'
 47.3 MiB / 47.3 MiB (100 %) 2.1 MiB/s ETA: 0 s 
info: downloading component 'cargo'
 3.9 MiB / 3.9 MiB (100 %) 2.5 MiB/s ETA: 0 s 
info: downloading component 'rust-docs'
 5.7 MiB / 5.7 MiB (100 %) 3.3 MiB/s ETA: 0 s 
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: installing component 'rust-docs'
info: default toolchain set to 'stable'

stable installed - rustc 1.25.0 (84203cac6 2018-03-25)

Rust is installed now. Great!

To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH 
environment variable. Next time you log in this will be done automatically.

To configure your current shell run source $HOME/.cargo/env

As you see in the above output, The rustup installer script downloads and installs the official compiler for the Rust programming language, and its package manager named Cargo, and it adds the cargo, rustc, rustup and other commands to Cargo’s bin directory, located at ~/.cargo/bin. And then, it adds the Cargo’s bin directory to your PATH environment variable by modifying the profile file located at ~/.profile.

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

$ source $HOME/.cargo/env

To verify the installed version, run:

$ rustc --version
rustc 1.25.0 (84203cac6 2018-03-25)

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

Test Rust programming language

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 new directory to put our sample program. For instance, I am going to call it “hello_world”.

$ mkdir hello_world

CD to that directory:

$ cd hello_world

Now write your first Rust program. 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!");
}

Save and quit the file (:wq).

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!

It’s working!!

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 and all 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

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.

More good stuffs to come. Stay tuned!

Cheers!

Resources:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...

1 Response

  1. Alex-PK says:

    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

Leave a Reply

Your email address will not be published. Required fields are marked *