必须用rustup安装,因系统包管理器安装的rust通常滞后1–2个大版本、缺cargo等关键组件,且无法切换工具链或更新;apt install rustc会报cargo command not found,而共存旧版更易引发构建失败。

直接用 rustup 安装,别碰系统包管理器或手动编译——它会漏 cargo、没文档、不能切工具链,还容易和系统已装的旧版冲突。
为什么 apt install rustc 一定不行
Ubuntu/Debian 仓库里的 rustc 包通常滞后 1–2 个大版本(比如系统提供 1.78,而 stable 已是 1.86),且不带 cargo、rust-docs、rustfmt 等关键组件。执行 cargo build 会直接报 command not found: cargo,不是你漏敲命令,是根本没装上。
更麻烦的是,如果之后再装 rustup,两个 Rust 会共存,rustc --version 可能显示旧版,但 cargo 又只认新版路径,项目构建失败时很难排查。
- Debian 13/Trixie 和 Ubuntu 24.04+ 虽然自带较新 Rust,但依然缺
cargo和工具链管理能力,不推荐依赖 -
sudo apt install rust(注意不是rustc)在部分新版发行版中确实会装cargo,但无法用rustup default切换 nightly/stable,也不支持rustup component add rust-src
curl | sh 安装必须加的两个参数
执行这行命令:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
漏掉 --proto '=https' 或 --tlsv1.2,在 CentOS 7、某些企业网络或老旧 OpenSSL 环境下会报错:
curl: (35) error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version
这不是 Rust 的问题,是 curl 拒绝用 TLS 1.0/1.1 握手。加了这两个参数才能和 sh.rustup.rs 正常通信。
- 安装脚本默认选
1) Proceed with installation,回车就行;别手抖选2) Customize installation,新手不需要动组件开关 - 装完后终端不会自动生效,必须立刻运行:
source "$HOME/.cargo/env"(引号防路径含空格) - 想永久生效,把这行加进
~/.bashrc或~/.zshrc末尾,再source ~/.bashrc
cargo build 报 error: linker `cc` not found 怎么办
Rust 编译器本身不带链接器,cargo build 默认调系统命令 cc(通常是 gcc)。最小化安装的 Linux(云服务器、Docker 镜像、WSL2 基础版)几乎都不带 C 工具链,所以这个错误和 Rust 是否装对完全无关。
- Ubuntu/Debian:
sudo apt install build-essential(只装gcc不够,build-essential还含make、libc6-dev等) - CentOS/RHEL/Fedora:
sudo dnf groupinstall "Development Tools"(旧版用yum groupinstall) - Arch Linux:
sudo pacman -S base-devel - WSL2 用户建议额外加装:
sudo apt install libssl-dev,否则reqwest、openssl类 crate 会编译失败
验证是否修好:cc --version 能输出版本号,且 cargo build 不再报 linker 错误。
国内用户卡在 downloading component 'rustc' 的真实原因和解法
不是“网络不通”,而是官方 CDN static.rust-lang.org 在国内被限速或 DNS 污染,下载会卡住或超时。中断后残留半截安装,再跑脚本也没用。
- 先清理:
rm -rf $HOME/.rustup $HOME/.cargo - 设镜像(仅当前 shell 有效):
export RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup - 再执行安装命令,不要跳过
source "$HOME/.cargo/env" - 装完顺手配
cargo镜像(影响 crate 下载):mkdir -p $HOME/.cargo && echo '[source.crates-io]\nreplace-with = "ustc"\n\n[source.ustc]\nregistry = "https://mirrors.ustc.edu.cn/crates.io-index"' > $HOME/.cargo/config.toml
真正容易被忽略的是:环境变量只对当前 shell 生效,关终端重开就失效;cargo 镜像和 rustup 镜像必须分开配,只改一个,另一个照样卡。











