September 17, 2017 in Tutorial4 minutes
What is Rust? Quoting Rust website:
Rust is a system programming language that runs blazingly fast, prevent segfaults and guarantees thread safety.
Several features of Rust language:
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.
Update repository metadata using command below:
sudo apt-get update
Install rust using command below:
sudo apt-get install rustc
This will install rustc and required dependencies.
Press Y to continue.
After installation finished we can check rustc version using -V flag:
rustc -V
rustc 1.15.1
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 | 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: if this is what you want, restart the installation with `-y'
error: cannot install while 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:
sudo apt-get -y remove rustc
Remove unused applications that was installed as rustc package dependencies:
sudo 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 | sh
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/ubuntu/.cargo/bin
This path will then be added to your PATH environment variable by modifying the profile file located at:
/home/ubuntu/.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
Choose 1
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2017-07-20, rust version 1.19.0 (0ade33941 2017-07-17)
info: downloading component 'rustc'
37.6 MiB / 37.6 MiB (100 %) 5.5 MiB/s ETA: 0 s
info: downloading component 'rust-std'
55.6 MiB / 55.6 MiB (100 %) 5.4 MiB/s ETA: 0 s
info: downloading component 'cargo'
info: downloading component 'rust-docs'
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.19.0 (0ade33941 2017-07-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
Latest stable version of Rust installed. At the time of this writing the latest stable version is 1.19.0.
We need to run load Cargo’s bin directory located in $HOME/.cargo/bin
to get started:
source $HOME/.cargo/env
Let’s check rust version installed:
rustc -V
rustc 1.19.0 (0ade33941 2017-07-17)
Check where rustc installed:
which rustc
/home/ubuntu/.cargo/bin/rustc
Create new file named hello.rs
with contents below:
fn main() {
println!("Hello Rust From Xenial! - howtodojo");
}
Compile the source code using command below:
rustc hello.rs
This time we will get error message similar to output below:
error: could not exec the linker `cc`: No such file or directory (os error 2)
...
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:
sudo apt-get install 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.
file hello
hello: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=65a62f130512190581f42c8d7b23265072442624, not stripped
Now let’s run our first rust program:
./hello
Hello Rust From Xenial! - howtodojo
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!