0%

rust离线环境使用

1. 需要下载 rustuprustc

rustup 是 rust 的工具链管理工具,方便切换rustc的版本,可以从这里下载离线版本;

rustc 本体需要下载 standalone 版本,从这里下载;

2. 安装 rust 工具链

包含编译器和常用工具, 其中主要是要设置 --prefix 选项,使用默认的会把 rust 分散安装到各个系统目录,污染 rustup 管理路径;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
##########################
# Configurable constants #
##########################
# Set the variables for the selected target triple, as well as Rust version
RUST_VERSION_STRING="1.73.0"
ARCH_DISTRO_STRING="x86_64-unknown-linux-gnu"
# Set the copied rust standalone installer file path
COPIED_FILES_FOLDER="${HOME}/Downloads/offline-rust"
# Set the install folder (The defult installation path is /usr/local)
STANDALONE_INSTALL_DIR="${HOME}/rust-install/rust-${RUST_VERSION_STRING}-${ARCH_DISTRO_STRING}"
# Set toolchain installed toolchain name
RUST_TOOLCHAIN_NAME="rust-toolchain-${RUST_VERSION_STRING}"

########
# Main #
########
archive_file=${COPIED_FILES_FOLDER}/rust-${RUST_VERSION_STRING}-${ARCH_DISTRO_STRING}.tar.gz
extract_archive_dir=${COPIED_FILES_FOLDER}/rust-${RUST_VERSION_STRING}-${ARCH_DISTRO_STRING}

# Extract the Rust standalone installer archive
tar -xzf ${archive_file} -C ${COPIED_FILES_FOLDER}

# Run the installation script
# (view custom installation argument by calling the install script with the option: '--help')
${extract_archive_dir}/install.sh --prefix=${STANDALONE_INSTALL_DIR}

# Delete the extracted folder (cleanup)
rm -r ${extract_archive_dir}

3. 安装 rustup

注意 rustup 本身需要联网,这里不需要联网,因为我们已经安装好了 rust.

1
2
3
4
5
6
7
8
9
10
11
12
13
# XXX: Use same "Configurable Constants" as in the scripts in Step 3

# Ensure rustup-init is executable
chmod +x ${COPIED_FILES_FOLDER}/rustup-init
# Run the installation script
# run with the default toolchain set to `none` so that no download is attempted.
# The '-y' is used to run in batch mode (non interactive).
# This will install `rustup` and set the proxies in the `${CARGO_HOME}/bin` folder.
# It will also configure the system PATH for new sessions
${COPIED_FILES_FOLDER}/rustup-init --default-toolchain none -y

# To make configure the system PATH for the current terminal session:
source ${CARGO_HOME:-${HOME}/.cargo}/env

rustup-init怎么工作?

rustup-init and rustup are essentially the same executable file; they function differently based on the executable’s filename.
When you run rustup-init, it creates copies of itself in the $CARGO_HOME/bin (or %CARGO_HOME%\bin on Windows) directory. Each copy is named after a Rust toolchain’s tool (e.g., cargo, rustc, rustdoc). These copies serve as proxies and are used by Rustup to manage the toolchain versions.

4. 使用 rustup 管理已经安装的 rust 工具链

1
2
3
4
5
6
7
8
9
10
11
12
# XXX: Use same "Configurable Constants" as in the scripts in Step 3

# Link the installed toolchain to Rustup
# Rustup will create a symbolik link named '${RUST_TOOLCHAIN_NAME}' to
# the installation directory '${STANDALONE_INSTALL_DIR}'
rustup toolchain link ${RUST_TOOLCHAIN_NAME} ${STANDALONE_INSTALL_DIR}

# Set the toolchain as default
rustup default ${RUST_TOOLCHAIN_NAME}

# Verify the installation
rustc --version

5. 还有一种简单的方法

在一台联网机器上使用 rustup-init 安装好后,在不联网机器上安装 rustup, 然后把工具链拷过来,用 rustup 链接一下就行了。

离线安装 rustup:

1
./rustup-init --default-toolchain none -y

拷贝 /root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu 这个文件夹,放到离线机器对应目录,

链接:

1
2
3
rustup default stable-x86_64-unknown-linux-gnu

source ~/.cargo/env

6. 离线管理依赖

思路是在联网机器上新建个项目,添加依赖,下载依赖,把依赖放到不联网的机器,重新编译:

联网机器操作:

1
2
3
4
cargo new dummy
cd dummy

cargo vendor --no-delete --versioned-dirs --respect-source-config

会把依赖都下载到 vendor/ 目录下,复制到不联网机器,然后在不联网机器项目下添加文件 .cargo/config.toml,其中写上:

1
2
3
4
5
[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "vendor"

这样就可以了。

问题

  1. 如果vscode rust analyzer 一直报 sysroot 的问题,看看系统中是不是之前装过一套rustc,如果是,找找 /usr/local/lib/rustlib/uninstall.sh, 删掉之前的版本就行了.

参考:

  1. https://buildsoftwaresystems.com/post/development/rust/tools/setting_up_and_using_rust_offline_for_seamless_develeopment__a_step_by_step_tutorial/