Install Rust from Source on Linux

Install Rust from Source on Linux

Read Installing Rust for more details and updated information. This guide is customized with some assumptions, primarily that you'll use clang++ to build it and install rustc to /usr/local. I tested this guide on Ubuntu 14.04.

Install Pre-requisites

user@host:~$ sudo aptitude install build-essential curl git clang checkinstall

Get Ready

I like to use /usr/local/src for cloning the git repo. But first you have to make sure you have rwx permissions to it.

I usually create a builder group and add any user who needs access to /usr/local/src needs to be added to the group.

user@host:~$ sudo groupadd builder
user@host:~$ sudo usermod -a -G builder user

Log out and log back in. If you run the groups command you'll see you are now part of the builder group.

Change permissions on /usr/local/src to allow builder group rwx permissions on it.

user@host:~$ sudo chown root:builder /usr/local/src
user@host:~$ sudo chmod g+rwx /usr/local/src

Download Source

Obtain the source code using git.

user@host:~$ cd /usr/local/src
user@host:/usr/local/src$ git clone https://github.com/rust-lang/rust.git
user@host:/usr/local/src$ cd rust

Since commits are constantly going into the master branch it may not be a good idea to continuously track it. One reason is that building rust can take a long time (50 mins on my VM) and you don't want to have to do it again and again if you don't need to. Building official releases can be really helpful in this case.

Since the last official release (besides the nightly releases) is 1.0.0-alpha.2 I'll use that tag in the git repo to create a local branch which I'll use to build from.

user@host:/usr/local/src/rust$ git tag
user@host:/usr/local/src/rust$ git branch build-from-1.0.0-alpha.2 1.0.0-alpha.2
user@host:/usr/local/src/rust$ git checkout build-from-1.0.0-alpha.2
user@host:/usr/local/src/rust$ git branch

Build and Install rustc

Set environment variables to use clang and run ./configure.

user@host:/usr/local/src/rust$ CC=clang CXX=clang++ ./configure --disable-libcpp --enable-clang --prefix=/usr/local

Run make. It'll take its sweet long time to build. Read Why does Rust compile slowly? for more information.

user@host:/usr/local/src/rust$ make -j2 all

You can now sudo make install. Alternatively, you can use checkinstall to create a package that you can then re-use with your package manager on this host or any other host without needing to re-build.

user@host:/usr/local/src/rust$ sudo checkinstall -D --pkgname rustc --pkgversion 1.0.0-alpha.2 --pkgrelease 1 --arch amd64 --pkglicense "Apache, MIT" --pkggroup checkinstall --maintainer "You Name \<your.name@example.com\>" --pkgsource "https://www.rust-lang.org/" --provides rustc --docdir "$PWD"/doc-pak --install=no

checkinstall will create a .deb in /usr/local/src/rust that you can install with dpkg. Copy this deb package to other machines to install there as well.

user@host:/usr/local/src/rust$ sudo dpkg -i /usr/local/src/rust/rustc_1.0.0-alpha.2-1_amd64.deb

Run rustc to make sure everything is working well.

user@host:/usr/local/src/rust$ rustc --version
rustc 1.0.0-dev (522d09dfe 2015-02-19) (built 2015-03-12)

You can delete the temporary branch since it's no longer needed. Remove the checkinstall artifacts (files, directories, etc.) and then delete the branch.

user@host:/usr/local/src/rust$ git status
user@host:/usr/local/src/rust$ sudo rm backup-031220151211-pre-rustc.tgz
user@host:/usr/local/src/rust$ sudo rm description-pak
user@host:/usr/local/src/rust$ sudo rm -rf doc-pak/
user@host:/usr/local/src/rust$ scp rustc_1.0.0-alpha.2-1_amd64.deb user@remotehost:/path/to/repo
user@host:/usr/local/src/rust$ sudo rm rustc_1.0.0-alpha.2-1_amd64.deb
user@host:/usr/local/src/rust$ git status
user@host:/usr/local/src/rust$ git checkout master
user@host:/usr/local/src/rust$ git branch -d build-from-1.0.0-alpha.2

Build and Install cargo

Cargo is the Rust package manager. It needs to be installed separately.

Install pre-requisites.

user@host:~$ sudo aptitude install python curl cmake libssl-dev pkg-config
user@host:/usr/local/src/rust$ cd /usr/local/src
user@host:/usr/local/src$ git clone https://github.com/rust-lang/cargo
user@host:/usr/local/src$ cd cargo
user@host:/usr/local/src/cargo$ git submodule update --init
user@host:/usr/local/src/cargo$ ./.travis.install.deps.sh
user@host:/usr/local/src/cargo$ ./configure --prefix=/usr/local --local-rust-root="$PWD"/rustc
user@host:/usr/local/src/cargo$ make -j2 all
user@host:/usr/local/src/cargo$ sudo checkinstall -D --pkgname cargo --pkgversion 0.1.0 --pkgrelease 1 --arch amd64 --pkglicense "Apache, MIT" --pkggroup checkinstall --maintainer "You Name \<your.name@example.com\>" --pkgsource "https://crates.io/" --provides cargo --install=no

You'll be asked about some files to exclude from the final package. Say yes to excluding them like so

Some of the files created by the installation are inside the build
directory: /usr/local/src/cargo

You probably don't want them to be included in the package,
especially if they are inside your home directory.
Do you want me to list them?  [n]: y
Should I exclude them from the package? (Saying yes is a good idea)  [y]: y

checkinstall will create a .deb in /usr/local/src/cargo that you can install with dpkg. Copy this deb package to other machines to install there as well.

user@host:/usr/local/src/cargo$ sudo dpkg -i /usr/local/src/cargo/cargo_0.1.0-1_amd64.deb

Run cargo to make sure everything is working well.

user@host:/usr/local/src/cargo$ cargo --version
cargo 0.0.1-pre (07cd618 2015-03-12) (built 2015-03-12)

Cleanup the /usr/local/src/cargo path because we treat it as a pristine clone of the upstream git repo. You can remove the checkinstall artifacts (files, directories, etc.) manually and then, optionally, delete the temporary branch.

user@host:/usr/local/src/cargo$ git status
user@host:/usr/local/src/cargo$ sudo rm backup-031220151500-pre-cargo.tgz
user@host:/usr/local/src/cargo$ sudo rm description-pak
user@host:/usr/local/src/cargo$ sudo rm -rf doc-pak/
user@host:/usr/local/src/cargo$ scp cargo_0.1.0-1_amd64.deb user@remotehost:/path/to/repo
user@host:/usr/local/src/cargo$ sudo rm cargo_0.1.0-1_amd64.deb
user@host:/usr/local/src/cargo$ git status