Home Rust Install Rust Programming Language In Linux

Install Rust Programming Language In Linux

By sk
Published: Last Updated on 7.9K views

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 how 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 --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.

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

  /home/ostechnix/.cargo/bin

This can be modified with the CARGO_HOME environment variable.

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.

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

  /home/ostechnix/.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
               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 2020-06-18, rust version 1.44.1 (c7087fe00 2020-06-17)
info: downloading component 'cargo'
  5.0 MiB /   5.0 MiB (100 %)   3.3 MiB/s in  1s ETA:  0s
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 12.2 MiB /  12.2 MiB (100 %)   2.6 MiB/s in  5s ETA:  0s
info: downloading component 'rust-std'
 17.7 MiB /  17.7 MiB (100 %)   2.4 MiB/s in  8s ETA:  0s
info: downloading component 'rustc'
 60.3 MiB /  60.3 MiB (100 %)   2.2 MiB/s in 26s ETA:  0s
info: downloading component 'rustfmt'
  3.4 MiB /   3.4 MiB (100 %)   2.6 MiB/s in  1s ETA:  0s
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 12.2 MiB /  12.2 MiB (100 %)   2.5 MiB/s in  4s ETA:  0s
info: installing component 'rust-std'
 17.7 MiB /  17.7 MiB (100 %)   8.6 MiB/s in  4s ETA:  0s
info: installing component 'rustc'
 60.3 MiB /  60.3 MiB (100 %)   7.2 MiB/s in  9s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable'

  stable installed - rustc 1.44.1 (c7087fe00 2020-06-17)

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
Install rust programming language in Linux Using rustup
Install rust programming language in Linux Using rustup

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.44.1 (c7087fe00 2020-06-17)

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

Sample output:

Compiling hello_world v0.1.0 (/home/sk/my_rust_projects/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.44s
     Running `target/debug/hello_world`
Hello, world!
Run hello world program in Rust
Run 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 In Linux
Run Hello World Program In Rust programming language In Linux

It's working!!

Troubleshooting

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 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.

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