Rust 安装与配置国内代理与镜像地址的方法参考
- 电脑基础
- 2021-07-08
- 11705热度
- 0评论
目录
[隐藏]
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
- 下载并安装 Visual Studio C++ Build tools: 。可直接安装
visualstudio 2019/2021 社区版
。 - 下载并安装 RUSTUP-INI.exe
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
然后重试。