在 Debian 上安装 Rust,通常不推荐直接用 apt 安装,原因和注意事项如下:
apt install rustc cargo✅ 推荐方式:使用 rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
或(更稳妥):
curl https://sh.rustup.rs -sSf | sh
安装后:
source "$HOME/.cargo/env"
sudo apt update
sudo apt install -y build-essential
sudo apt install -y pkg-config libssl-dev
libssl-dev:网络 / HTTPS 项目常用pkg-config:链接系统库可能还需要:
sudo apt install -y libclang-dev clang
可以配置 镜像:
[source.crates-io]
replace-with = "rsproxy"
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"
或使用 ustc / tuna 镜像。
musl 静态编译rustup target add x86_64-unknown-linux-musl
cargo build --target x86_64-unknown-linux-musl
~/.cargoecho 'source "$HOME/.cargo/env"' >> ~/.bashrc
如果是非交互 shell(CI / cron),要显式 source。
rustup update
cargo update
卸载:
rustup self uninstall
| 问题 | 原因 | 解决 |
|---|---|---|
cargo: command not found |
PATH 没生效 | source cargo/env |
| 编译失败 | 缺 build-essential | 安装基础编译工具 |
| 网络超时 | 官方源慢 | 配国内镜像 |
| apt 装的 rust 太旧 | 仓库滞后 | 用 rustup |
如果你愿意,可以告诉我:
我可以给你一套最合适的安装命令。