发布于 ,更新于 

Linux 手动安装 Rust 最新版本编译器

Linux 手动安装 Rust 最新版本编译器

一般来说,Linux 发行版都自带了 Rust 编译器,通过 apt-get 或者 dnf 等包管理工具即可以安装 Rust 编译器。

Rust 编译器套件中包含了自升级命令行组件 rustup,可以进行升级操作:

1
2
rustup self update            # 升级 rustup 工具本身
rustup update # 升级 Rust 编译器整个套件

也可以去 Rust 官方去下载指定的版本进行手动安装。 手动安装可以选择安装组件,可以指定安装路径,也可以选择具体版本。

如下,去 “ 官方地址 ” 去下载具体版本; 这里选择以 1.51.0 版本为例子。(本文书写时最新版本为 1.51.0)。
一般的 Linux 发行版的 platform 请选择 x86_64-unknown-linux-gnu, 特殊的 Alpine Linux发行版的 platform 请选择 x86_64-unknown-linux-musl 。

下面以下载到的文件 rust-1.51.0-x86_64-unknown-linux-gnu.tar.gz 为例:

1
2
3
4
5
# 解压缩:
tar zxvf rust-1.51.0-x86_64-unknown-linux-gnu.tar.gz

# 进入解压缩后的目录:
cd rust-1.51.0-x86_64-unknown-linux-gnu

查看一下具体目录结构:

1
ls -l

你会看到,里面有个官方的安装脚本 install.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
ls -l
total 68
-rwxrwxrwx 1 root root 9322 May 6 20:09 COPYRIGHT
-rwxrwxrwx 1 root root 9723 May 6 20:09 LICENSE-APACHE
-rwxrwxrwx 1 root root 1023 May 6 20:09 LICENSE-MIT
-rwxrwxrwx 1 root root 9566 May 6 20:09 README.md
drwxrwxrwx 1 root root 512 May 6 20:11 cargo
drwxrwxrwx 1 root root 512 May 6 20:11 clippy-preview
-rwxrwxrwx 1 root root 192 May 6 20:11 components
-rwxrwxrwx 1 root root 40 May 6 20:10 git-commit-hash
-rwxrwxrwx 1 root root 27856 May 6 20:10 install.sh
drwxrwxrwx 1 root root 512 May 6 20:11 llvm-tools-preview
drwxrwxrwx 1 root root 512 May 6 20:11 miri-preview
drwxrwxrwx 1 root root 512 May 6 20:11 rls-preview
drwxrwxrwx 1 root root 512 May 6 20:11 rust-analysis-x86_64-unknown-linux-gnu
drwxrwxrwx 1 root root 512 May 6 20:11 rust-analyzer-preview
drwxrwxrwx 1 root root 512 May 6 20:11 rust-docs
-rwxrwxrwx 1 root root 2 May 6 20:11 rust-installer-version
drwxrwxrwx 1 root root 512 May 6 20:11 rust-std-x86_64-unknown-linux-gnu
drwxrwxrwx 1 root root 512 May 6 20:11 rustc
drwxrwxrwx 1 root root 512 May 6 20:11 rustfmt-preview
-rwxrwxrwx 1 root root 29 May 6 20:11 version

运行一下 install.sh 看看有什么安装选项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sudo ./install.sh --help

Usage: ./install.sh [options]

Options:

--uninstall only uninstall from the installation prefix
--destdir=[<none>] set installation root
--prefix=[/usr/local] set installation prefix
--without=[<none>] comma-separated list of components to not install
--components=[<none>] comma-separated list of components to install
--list-components list available components
--sysconfdir=[/etc] install system configuration files
--bindir=[/bin] install binaries
--libdir=[/lib] install libraries
--datadir=[/share] install data
--mandir=[/share/man] install man pages in PATH
--docdir=[\<default\>] install documentation in PATH
--disable-ldconfig don't run ldconfig after installation (Linux only)
--disable-verify don't obsolete
--verbose run with verbose output

这里有几个有用的选项:

  • --prefix=[/usr/local]
    指定安装路径,不指定时为 /usr/local
  • --list-components
    列出可以安装的组件
  • --without=[<none>]
    不安装指定组件

下面看看具体有哪些组件可供选择:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
sudo ./install.sh --list-components

# Available components

* rustc
* cargo
* rls-preview
* rust-analyzer-preview
* clippy-preview
* miri-preview
* rustfmt-preview
* llvm-tools-preview
* rust-analysis-x86_64-unknown-linux-gnu
* rust-std-x86_64-unknown-linux-gnu
* rust-docs

其中,本人亲测,rust-docs 为帮助文档,安装过程特别慢,而且文档可以直接在浏览器上网去查看,所以没有必要装到本地,采用以下命令进行安装:

1
sudo ./install.sh --without=rust-docs

输入编译命令,测试一下,出现版本号则表示安装成功:

1
2
rustc -V
rustc 1.51.0 (2fd73fabe 2021-03-23)