文章

Rust 配置国内代理

Rust 配置国内代理

1 安装 Rust

安装 Rust 将下载并安装 Rust 的官方编译器(rustup),和它的包管理器 Cargo。 在国内的环境下安装 Rust 可能会因为网络问题而很慢或失败。这需要配置为国内的代理或镜像地址。 下面简要介绍相关的主要方法与步骤。

1.1 rustup 设置环境变量使用国内反向代理

Macbook 下设置环境变量:

1
2
3
vi` `~/.bash_profile
# 新增如下内容(使用中科大代理)``export` `RUSTUP_DIST_SERVER=https:``//mirrors``.ustc.edu.cn``/rust-static``export` `RUSTUP_UPDATE_ROOT=https:``//mirrors``.ustc.edu.cn``/rust-static/rustup` `# 使用 rsproxy 代理``# export RUSTUP_DIST_SERVER="https://rsproxy.cn"``# export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"
# 让修改生效``source` `~/.bash_profile

Windows 10 下设置环境变量:

win 键搜索 编辑系统环境变量。在弹出的窗口中设置如上环境变量即可。

1.2 Macbook 安装 Rust

执行如下命令即可:

1
# 使用官方安装脚本``curl --proto ``'=https'` `--tlsv1.2 -sSf https:``//sh``.rustup.rs | sh` `# 使用 rsproxy 代理安装``curl --proto ``'=https'` `--tlsv1.2 -sSf https:``//rsproxy``.cn``/rustup-init``.sh | sh

1.3 windows 系统中安装 Rust

1.4 rustup 的升级

runstup 的升级方案做的十分方便。只需要执行如下命令即可:

1
rustup check``rustup update

2 配置 Cargo 使用国内镜像

新建文件 ~/.cargo/config(windows 10 下为 C:\Users\<用户名>\.cargo\config),输入如下内容:

1
[``source.crates-io``]``registry = ``"https://github.com/rust-lang/crates.io-index"` `# 替换成要使用的镜像``replace-with = ``'rsproxy'` `# 中国科学技术大学``[``source.ustc``]``registry = ``"git://mirrors.ustc.edu.cn/crates.io-index"``# 如果所处的环境中不允许使用 git 协议,可以把上述地址改为 https 协议``#registry = "https://mirrors.ustc.edu.cn/crates.io-index"` `# 清华大学``[``source.tuna``]``registry = ``"https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"` `# 上海交通大学``[``source.sjtu``]``registry = ``"https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"` `# rustcc 社区``[``source.rustcc``]``registry = ``"git://crates.rustcc.cn/crates.io-index"` `# rsproxy``[``source.rsproxy``]``registry = ``"https://rsproxy.cn/crates.io-index"``[``source.rsproxy-sparse``]``registry = ``"sparse+https://rsproxy.cn/index/"``[``registries.rsproxy``]``index = ``"https://rsproxy.cn/crates.io-index"` `[``net``]``git-fetch-with-cli=``true

如果更换 cargo 源后使用 cargo build 命令仍然报错如下错误:

blocking waiting for file lock on package cache lock

可删除文件 ~/.cargo/.package-cache 然后重试。

3 相关参考与链接

  • https://lzw.me/a/rust-abc.html
  • https://visualstudio.microsoft.com/zh-hans/visual-cpp-build-tools/
  • https://mirrors.ustc.edu.cn/help/rust-static.html
  • http://mirrors.ustc.edu.cn/help/crates.io-index.html
  • https://www.rust-lang.org/tools/install
  • https://rsproxy.cn/
本文由作者按照 CC BY 4.0 进行授权