在 Debian 上遇到 Rust 冲突,通常不是 Rust 本身的问题,而是系统包管理器(apt)安装的 Rust 和 官方 rustup 安装的 Rust 冲突,或者多个 Rust 版本共存导致的。
下面按最常见情况给你一套清晰、可操作的解决方案。
which rustc
rustc --version
常见情况:
/usr/bin/rustc → Debian apt 安装~/.cargo/bin/rustc → rustup 安装PATH 里 → 冲突来源适用于:开发、编译项目
sudo apt remove rustc cargo
sudo apt autoremove
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env
或加到 ~/.bashrc / ~/.zshrc:
export PATH="$HOME/.cargo/bin:$PATH"
which rustc
rustc --version
✅ 输出应该是:
/home/你的用户/.cargo/bin/rustc
某些 Debian 软件依赖系统 Rust(如部分 dev 包)。
sudo apt install rustc cargo
然后:
which rustc
# 应该是 /usr/bin/rustc
⚠️ 不要同时安装 rustup
⚠️ 版本通常较旧
echo $PATH
确保:
~/.cargo/bin 在 /usr/bin 前面
修改 ~/.bashrc:
export PATH="$HOME/.cargo/bin:$PATH"
然后:
source ~/.bashrc
如果你用 rustup:
rustup toolchain list
rustup default stable
rustup update
切换版本:
rustup default nightly
✅ 开发环境:rustup
✅ 系统服务 / Debian 打包:apt rustc
❌ 不要混用
你可以告诉我:
apt 还是 rustupcargo build 报错)我可以直接给你针对你机器的命令。