I don't really blog anymore. Click here to go to my main website.

muhuk's blog

Nature, to Be Commanded, Must Be Obeyed

November 07, 2017

How to Install Rust on Debian Without Sudo


Since I have written this post rustup has evolved and now it does not require sudo. Please see the latest documentation.

Rust has been around for a while but I have just recently found the opportunity to study it. It has some unique and powerful ideas and I intend to experiment further. This post summarizes the steps I have followed to install Rust toolchain on my Debian Stretch machine without admin rights[1].

Step 0: Temporarily Disabling Sudo

Official Rust documentation suggests installing official installer rustup by running the command below:

curl https://sh.rustup.rs -sSf | sh

This would install the toolchain system-wide, writing to directories like /usr/local. I wanted a user-space install. To ensure sudo is not used I aliased the command like below:

sudo whoami       # => root
alias sudo=''
sudo whoami       # => muhuk
unalias sudo
sudo whoami       # => root

This does not prevent any script to execute /usr/bin/sudo but it is good enough to quell my paranoia.

Step 1: Install Rust Toolchain

I copied rustup.sh file to my ~/bin and made it executable. Then I created a base directory for Rust:

cp ~/Downloads/rustup.sh ~/bin
chmod +x ~/bin/rustup.sh
mkdir -p ~/lib/rust

With this we are ready to run the installer:

$ rustup.sh --help

Usage: rustup.sh [--verbose]


     --channel=(stable|beta|nightly)   Install from channel (default stable)
     --date=<YYYY-MM-DD>               Install from archives
     --revision=<version-number>       Install a specific release
     --spec=<toolchain-spec>           Install from toolchain spec
     --prefix=<path>                   Install to a specific location (default /usr/local)
     --uninstall                       Uninstall instead of install
     --with-target=<triple>            Also install the standard library for the given target
     --add-target=<triple>             Updates an existing installation with a new target
     --list-available-targets          Lists targets available to an existing installation
     --disable-ldconfig                Do not run ldconfig on Linux
     --disable-sudo                    Do not run installer under sudo
     --save                            Save downloads for future reuse
     --yes, -y                         Disable the interactive mode
     --help, -h                        Display usage information

I have used the following command to install[2]:

rustup.sh --disable-sudo --prefix=/home/muhuk/lib/rust

This will download and install Rust compiler and Cargo package manager. It run successfully but since I did not specify --disable-ldconfig there was a warning about that. Following commands should fix this issue:

sudo bash -c 'echo "/home/muhuk/lib/rust/lib" > /etc/ld.so.conf.d/rust.conf'
sudo ldconfig

# Below command would only produce any output if
# you have already compiled something with Rust:
sudo ldconfig -p | grep rust

Step 2: Add Rust Binaries to PATH

Rust base directory has these subdirectories:

$ ls -1 ~/lib/rust

Adding export PATH=~/lib/rust/bin:$PATH to ~/.profile would make Rust compiler and Cargo available through the PATH:

$ tail -n1 ~/.profile
export PATH=~/lib/rust/bin:$PATH

Step 3: Verify Installation

Run following commands to verify everything works:

cargo new cargo-test
cd cargo-test
cargo check
cargo build
cargo test

Output of cargo test should look something like below:

$ cargo test
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running target/debug/deps/cargo_test-9badc6b245845605

running 1 test
test tests::it_works ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests cargo-test

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

More Rust posts will follow.

[1]For the most part. I still needed admin rights for ldconfig.
[2]Needless to say you should replace muhuk with your own user name.

If you have any questions, suggestions or corrections feel free to drop me a line.