Rust 安装与配置国内代理与镜像地址的方法参考

11,369次阅读
没有评论

共计 2017 个字符,预计需要花费 6 分钟才能阅读完成。

提醒:本文最后更新于2025-07-07 14:54,文中所关联的信息可能已发生改变,请知悉!

1 安装 Rust

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

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

Macbook 下设置环境变量:

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

执行如下命令即可:

# 使用官方安装脚本
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 的升级方案做的十分方便。只需要执行如下命令即可:

rustup check
rustup update

2 配置 Cargo 使用国内镜像

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

[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 相关参考与链接

正文完
 0
任侠
版权声明:本站原创文章,由 任侠 于2021-07-08发表,共计2017字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码