How To Install Rust On Ubuntu 16.04

Overview

What is Rust? Quoting [Rust website][1]:

Rust is a system programming language that runs blazingly fast, prevent segfaults and guarantees thread safety.

Several features of Rust language:

  • zero-cost abstraction
  • move semantics
  • guarantted memory safety
  • threads without data races
  • trait-based generics
  • Pattern matching
  • Type inference
  • Minimal runtime
  • Efficient C bindings

In this tutorial we’ll learn how to install Rust on Ubuntu 16.04. We’ll install Rust from Ubuntu repository and also install latest stable version of Rust from Rust website.

Install Rust from Ubuntu Repository

Update repository metadata using command below


$ <span class="token function">sudo</span> <span class="token function">apt-get</span> update

```

Install rust using command below


$ <span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> rustc

```

This will install rustc and required dependencies.

[install rust ubuntu 16.04][2]

Press **Y** to continue.

After installation finished we can check rustc version using **-V** flag.


$ rustc -V
rustc 1.15.1

```

## Install Latest Version of Rust on Ubuntu 16.04

We already installed Rust from Ubuntu Repository, the version shipped with Ubuntu is not always the latest stable version.

To install latest stable version of Rust we can install it from Rust website using custom install script.

Run command below to install latest stable version of Rust


$ curl https://sh.rustup.rs -sSf <span class="token operator">|</span> sh

```

Since we already have Rust installed it will give us error message:


info: downloading installer
error: it looks like you have an existing installation of Rust at:
error: /usr/bin
error: rustup cannot be installed alongside Rust. Please uninstall first
error: <span class="token keyword">if</span> this is what you want, restart the installation with `-y'
error: cannot <span class="token function">install</span> <span class="token keyword">while</span> Rust is installed

```

The installer cannot continue since it detect a rust compiler installed on `/usr/bin`. Let's remove rust that we installed from Ubuntu repository using command below.


$ <span class="token function">sudo</span> <span class="token function">apt-get</span> -y remove rustc

```

Remove unused applications that was installed as rustc package dependencies.


$ <span class="token function">sudo</span> apt -y autoremove

```

Now let's install rust. Please note that this will install Rust on our home directory so Rust will only available for our current user and not for all users on the system.


$ curl https://sh.rustup.rs -sSf <span class="token operator">|</span> sh
info: downloading installer

Welcome to Rust<span class="token operator">!</span>

This will download and <span class="token function">install</span> the official compiler <span class="token keyword">for</span> 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/ubuntu/.cargo/bin

This path will <span class="token keyword">then</span> be added to your PATH environment variable by modifying the
profile <span class="token function">file</span> located at:

  /home/ubuntu/.profile

You can uninstall at any <span class="token function">time</span> 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: <span class="token function">yes</span>

1<span class="token punctuation">)</span> Proceed with installation <span class="token punctuation">(</span>default<span class="token punctuation">)</span>
2<span class="token punctuation">)</span> Customize installation
3<span class="token punctuation">)</span> Cancel installation

```

Choose **1**


info: syncing channel updates <span class="token keyword">for</span> <span class="token string">'stable-x86_64-unknown-linux-gnu'</span>
info: latest update on 2017-07-20, rust version 1.19.0 <span class="token punctuation">(</span>0ade33941 2017-07-17<span class="token punctuation">)</span>
info: downloading component <span class="token string">'rustc'</span>
 37.6 MiB /  37.6 MiB <span class="token punctuation">(</span>100 %<span class="token punctuation">)</span>   5.5 MiB/s ETA:   0 s                
info: downloading component <span class="token string">'rust-std'</span>
 55.6 MiB /  55.6 MiB <span class="token punctuation">(</span>100 %<span class="token punctuation">)</span>   5.4 MiB/s ETA:   0 s                
info: downloading component <span class="token string">'cargo'</span>
info: downloading component <span class="token string">'rust-docs'</span>
info: installing component <span class="token string">'rustc'</span>
info: installing component <span class="token string">'rust-std'</span>
info: installing component <span class="token string">'cargo'</span>
info: installing component <span class="token string">'rust-docs'</span>
info: default toolchain <span class="token keyword">set</span> to <span class="token string">'stable'</span>

  stable installed - rustc 1.19.0 <span class="token punctuation">(</span>0ade33941 2017-07-17<span class="token punctuation">)</span>


Rust is installed now. Great<span class="token operator">!</span>

To get started you need Cargo's bin directory <span class="token punctuation">(</span><span class="token variable">$HOME</span>/.cargo/bin<span class="token punctuation">)</span> <span class="token keyword">in</span> your PATH 
environment variable. Next <span class="token function">time</span> you log <span class="token keyword">in</span> this will be <span class="token keyword">done</span> automatically.

To configure your current shell run <span class="token function">source</span> <span class="token variable">$HOME</span>/.cargo/env

```

Latest stable version of Rust installed. At the time of this writing the latest stable versionis 1.19.0

We need to run load Cargo's bin directory located in `$HOME/.cargo/bin` to get started


$ <span class="token function">source</span> <span class="token variable">$HOME</span>/.cargo/env

```

Let's check rust version installed


$ rustc -V
rustc 1.19.0 <span class="token punctuation">(</span>0ade33941 2017-07-17<span class="token punctuation">)</span>

```

Check where rustc installed


$ <span class="token function">which</span> rustc
/home/ubuntu/.cargo/bin/rustc

```

## Hello Rust from Xenial

Create new file named `hello.rs` with contents below:


fn main<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    println<span class="token operator">!</span><span class="token punctuation">(</span><span class="token string">"Hello Rust From Xenial! - howtodojo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

```

Compile the source code using command below


$ rustc hello.rs 

```

This time we will get error message similar to output below


error: could not <span class="token function">exec</span> the linker <span class="token variable"><span class="token variable">`</span>cc<span class="token variable">`</span></span><span class="token keyword">:</span> No such <span class="token function">file</span> or directory <span class="token punctuation">(</span>os error 2<span class="token punctuation">)</span>
<span class="token punctuation">..</span>.

```

This is because Rust doesn't have its own linker so we need to install gcc to help rust compiling code. Let's install gcc


$ <span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> gcc

```

Compile the code


$ rustc hello.rs 

```

The result will be named hello. When we check the file type we will get it's a Linux ELF 64-bit file.


$ <span class="token function">file</span> hello
hello: ELF 64-bit LSB shared object, x86-64, version 1 <span class="token punctuation">(</span>SYSV<span class="token punctuation">)</span>, dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, <span class="token keyword">for</span> GNU/Linux 2.6.32, BuildID<span class="token punctuation">[</span>sha1<span class="token punctuation">]</span><span class="token operator">=</span>65a62f130512190581f42c8d7b23265072442624, not stripped

```

Now let's run our first rust program


$ ./hello
Hello Rust From Xenial<span class="token operator">!</span> - howtodojo

```

## Summary

In this tutorial we learned how-to install Rust from Ubuntu repository. We also learned installing latest stable version of Rust using a script from Rust website.

We also created our first Hello Rust application and compile it. Have fun with Rust!

 [1]: https://www.rust-lang.org/
 [2]: https://www.howtodojo.com/wp-content/uploads/2017/09/how-to-install-rust-ubuntu-16.04-01.png