Ben Chuanlong Du's Blog

And let it direct your passion with reason.

Install Rust Globally in Linux

There are 3 ways to install a global standalone version of Rust in Linux. The recommended way is to use rustup with customized environment variables.

Using a Package Management Tool

Some Linux distributions provide packages for Rust. Taking Debian/Ubuntu based Linux distributions as an example, you can install a global version of Rust using the command below.

wajig install rust rust-src cargo rustfmt

Notice that the Rust version installed by this way is relatively old. Taking Ubuntu 22.04 as example, the above comamnd installs Rust 1.58.1 while the latest stable Rust version is 1.61.0 as of May 2022.

Using Environment Variable + rustup

By default, rustup installs Rust locally. However, you can configure environment variables to install Rust globally.

export RUSTUP_HOME=/usr/local/rustup
export CARGO_HOME=/usr/local/cargo
export PATH=/usr/local/cargo/bin:$PATH
curl --proto '=https' --tlsv1.2 -sSf | sh

Manually Install Rust Globally

  1. Download Rust.

    curl -sSL -o /tmp/rust.tar.gz
  2. Untar the downloaded file.

    tar -zxvf /tmp/rust.tar.gz
  3. Go to the untarred directory rust-1.61.0-x86_64-unknown-linux-gnu and install Rust and necessary components.

    ./ --without=rust-demangler-preview,rls-preview,rust-analysis-x86_64-unknown-linux-gnu
  4. Go to the directory /usr/local/lib/rustlib and remove non-needed files. You only to keep the 3 directories etc, src and x86_64-unknown-linux-gnu.

    rm -rf !(etc|src|x86_64-unknown-linux-gnu)
  5. Download Rust source code.

    curl -sSL -o /tmp/rust.tar.gz
  6. Untar the downloaded source file into the directory /usr/local/lib/rustlib/src/rust/ .

    mkdir -p /usr/local/lib/rustlib/src/rust
    tar -zxvf /tmp/rust.tar.gz -C /usr/local/lib/rustlib/src/rust --strip-components=1