Rust安装

诸葛卜霸
2023-12-01

版本:1.34.2

源码编译安装

环境准备

官方要求具备的环境:

  • g++ 5.1 or later or clang++ 3.5 or later
  • python 2.7 (but not 3.x)
  • GNU make 3.81 or later
  • cmake 3.4.3 or later
  • curl
  • git
  • ssl which comes in libssl-dev or openssl-devel
  • pkg-config if you are compiling on Linux and targeting Linux

先甭管都是用来干啥的,装上就是:

$ sudo apt install g++
$ sudo apt install python2.7
$ sudo apt install make
$ sudo apt install cmake
$ sudo apt install curl
$ sudo apt install git
$ sudo apt install libssl-dev
$ sudo apt install pkg-config

源码获取

本来可以直接git clone https://github.com/rust-lang/rust.git获取源码,但网速太慢,而且我们也不是要最新版,就自行从https://static.rust-lang.org/dist/rustc-1.34.2-src.tar.gz下载了。

放到~/下解压:

$ tar -zxvf rustc-1.34.2-src.tar.gz
$ mv rustc-1.34.2-src rust
$ cd rust

编译安装

借用官方提供的config.toml

$ cp config.toml.example config.toml

鉴于/usr/local的权限问题,在config.toml中把安装目录配置到我们的工作目录:

[install]
# Instead of installing to /usr/local, install to this path instead.
prefix = "~/rust"

编译及安装:

$ ./x.py build && ./x.py install

编译过程遇到的坑

- 1 -

获取官方crates时可能太慢卡住了,像这样:

    Updating crates.io index
       Fetch [>                                                      ]   2.81%

改用镜像站,新建~/.cargo/config,贴入:

[source.crates-io]
replace-with = 'ustc'
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index"

https可以换成git,还有一个阿里云的https://code.aliyun.com/rustcc/crates.io-index.git,但有点问题似乎暂时不能用。

- 2 -

下载crates期间遇到报错可能是网络不稳,重新运行会继续下。

- 3 -

编译Rust源码需要用到更早版本的Rust:

https://static.rust-lang.org/dist/2019-02-28/rust-std-1.33.0-x86_64-unknown-linux-gnu.tar.gz
https://static.rust-lang.org/dist/2019-02-28/rustc-1.33.0-x86_64-unknown-linux-gnu.tar.gz
https://static.rust-lang.org/dist/2019-02-28/cargo-0.34.0-x86_64-unknown-linux-gnu.tar.gz

网速太慢经常下载失败,可自行下载后放到~/rust/build/cache/2019-02-28

- 4 -

error: failed to run custom build command for `compiler_builtins v0.1.23`
process didn't exit successfully: `/home/yizhi/rust/build/x86_64-unknown-linux-gnu/stage0-std/release/build/compiler_builtins-b25c1064dfc3ea33/build-script-build` (exit code: 101)
--- stdout
cargo:rerun-if-changed=build.rs
cargo:compiler-rt=/home/yizhi/.cargo/registry/src/mirrors.ustc.edu.cn-12df342d903acd47/compiler_builtins-0.1.23/compiler-rt
cargo:rustc-cfg=feature="unstable"

--- stderr
thread 'main' panicked at 'RUST_COMPILER_RT_ROOT is not set', /home/yizhi/.cargo/registry/src/mirrors.ustc.edu.cn-12df342d903acd47/compiler_builtins-0.1.23/build.rs:423:21
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

添加环境变量

$ export RUST_COMPILER_RT_ROOT=~/rust/vendor/compiler_builtins/compiler-rt/

- 5 -

error[E0635]: unknown feature `compiler_builtins_lib`===============>  ] 35/36: std
   --> src/libstd/lib.rs:244:12
    |
244 | #![feature(compiler_builtins_lib)]
    |            ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0635`.
error: Could not compile `std`.

src/libstd/lib.rs中的#![feature(compiler_builtins_lib)]注释掉。

- 6 -

error: cannot satisfy dependencies so `unicode_width` only shows up once31/149: rustc
  |
  = help: having upstream crates all available in one format will likely make this go away

error: aborting due to previous error

error: Could not compile `rustc`.

这个问题实在无解,更新了一下系统就过了,而且连前面的4和5都没遇到了。

- 7 -

./x.py install期间会遇到系统目录无权限的问题:

cp: cannot create regular file '/etc/bash_completion.d/cargo': Permission denied
chmod: cannot access '/etc/bash_completion.d/cargo': No such file or directory
install: error: file creation failed. see logs at '/home/yizhi/rust/lib/rustlib/install.log'

虽然可以sudo ./x.py install,但这样会在当前编译的工作目录下产生系统权限的文件(夹),因此我选择手动补救:

$ sudo cp /home/yizhi/rust/build/tmp/dist/cargo-0.35.0-dev-x86_64-unknown-linux-gnu/cargo/etc/bash_completion.d/cargo /etc/bash_completion.d/cargo
$ sudo chmod 644 /etc/bash_completion.d/cargo

当然这不是治本的办法,它们后面还有没有指令也有待探究。

Rustup安装

Rustup是Rust的官方版本管理工具,是安装Rust的首选,一行命令搞定:

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

如果网速慢,设置镜像站:

$ export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
$ export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

如果想安装指定的版本,使用toolchain子命令:

$ rustup toolchain install stable-x86_64-unknown-linux-gnu stable-2019-05-14 1.34.2

会逐一下载各工具,其中体积比较大的经常会下载失败,又需要手动下载:

https://static.rust-lang.org/dist/2019-05-14/rust-docs-1.34.2-x86_64-unknown-linux-gnu.tar.xz
https://static.rust-lang.org/dist/2019-05-14/rust-std-1.34.2-x86_64-unknown-linux-gnu.tar.xz
https://static.rust-lang.org/dist/2019-05-14/rustc-1.34.2-x86_64-unknown-linux-gnu.tar.xz
https://static.rust-lang.org/dist/2020-03-12/rust-std-1.42.0-x86_64-unknown-linux-gnu.tar.xz
https://static.rust-lang.org/dist/2020-03-12/rustc-1.42.0-x86_64-unknown-linux-gnu.tar.xz

然后放到~/.rustup/downloads/下,重命名成对应的SHA256值(用sha256sum命令查看)。

 类似资料: