linux 安装 rust 应使用 curl | sh 安装 rustup,禁用系统包管理器;安装后需 source 环境变量并执行 rustup default 设置默认工具链,卸载须用 rustup self uninstall。

用 curl 装 rustup 是唯一推荐方式
Linux 上装 Rust 不该用包管理器(比如 apt install rustc 或 dnf install rust),因为系统仓库的版本通常滞后、不带 cargo 完整工具链,且无法切换工具链。官方只维护 rustup 这一套安装/更新/管理机制。
执行这行命令即可:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
它会下载并运行安装脚本,自动把 rustc、cargo、rustup 放进 $HOME/.cargo/bin。别忘了让 shell 读取新路径:
- 如果是 Bash:运行
source $HOME/.cargo/env,再加到$HOME/.bashrc - 如果是 Zsh:加到
$HOME/.zshrc,然后source ~/.zshrc - 如果终端新开后报
command not found: cargo,说明$HOME/.cargo/bin没进$PATH
rustup default 和 rustup toolchain list 必须会查
装完默认是 stable 工具链,但很多人不知道它其实没真正“激活”——得靠 rustup default 显式设为默认,否则可能意外用上系统残留的老版本。
- 查当前所有已安装工具链:
rustup toolchain list - 设稳定版为默认:
rustup default stable - 想试 nightly 版(比如用
async新特性):rustup toolchain install nightly,再rustup default nightly - 项目级覆盖(进项目目录后生效):
rustup override set stable
常见错误现象:cargo build 报错说找不到 std,或提示 rustc 1.63.0 (unknown hash) —— 很可能是工具链没设 default,或者 rustup 没初始化完成。
cargo new 后别急着 cargo run,先确认 Cargo.toml 里没乱改
新建项目时 cargo new hello 生成的默认配置足够跑通,但有人会手动编辑 Cargo.toml 加 [dependencies] 或改 edition,结果编译失败。
-
edition = "2021"是当前推荐,别写成"2018"后又用let else这类 2021 特性 - 加依赖后忘记运行
cargo fetch或网络不通,cargo build会卡在 “Downloading …” - 如果本地有代理,需设环境变量:
export HTTP_PROXY=http://127.0.0.1:7890(对应你的代理端口) - 国内用户常遇
failed to download `xxx v0.1.0`:可临时切镜像源,改$HOME/.cargo/config.toml加[source.crates-io]配置
卸载不是 rm -rf ~/.cargo 就完事
直接删目录会留下环境变量残留,下次开终端还试图加载不存在的路径,导致 command not found 或 shell 启动变慢。
- 正确卸载方式:
rustup self uninstall,它会清理二进制、toolchain、env 配置 - 删完检查:
which rustc、which cargo应无输出;echo $PATH里不应含.cargo/bin - 如果之前把
source $HOME/.cargo/env写进了 shell 配置文件,记得手动删掉那行
最容易被忽略的是 shell 配置文件里的残留 source 行——它不会报错,但会让每次打开终端都多一次失败的文件读取。










