温馨提示×

Debian装Rust要注意什么

小樊
50
2026-09-11 03:37:41
栏目: 编程语言

在 Debian 上安装 Rust,通常不推荐直接用 apt 安装,原因和注意事项如下:


一、最重要的一点:优先用官方 rustup,而不是 apt

为什么不推荐 apt install rustc cargo

  • Debian 仓库里的 Rust 版本往往很旧
  • 更新慢,跟不上生态(很多 crate 需要较新 Rust)
  • 工具链管理不方便(不能并行多版本)

推荐方式:使用 rustup

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

或(更稳妥):

curl https://sh.rustup.rs -sSf | sh

安装后:

source "$HOME/.cargo/env"

二、安装前需要的环境依赖

1. 编译工具链(必须)

sudo apt update
sudo apt install -y build-essential

2. 常见额外依赖(视项目而定)

sudo apt install -y pkg-config libssl-dev
  • libssl-dev:网络 / HTTPS 项目常用
  • pkg-config:链接系统库

3. 如果是 GUI / 系统级项目

可能还需要:

sudo apt install -y libclang-dev clang

三、网络与镜像问题(国内用户特别注意)

官方源可能很慢

可以配置 镜像

cargo 镜像(~/.cargo/config.toml)

[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 镜像。


四、系统架构与 glibc 注意

  • Debian 稳定版(如 Debian 12)glibc 较旧
  • 编译出的二进制 依赖当前系统 glibc
  • 若要在老系统运行,需:
    • 在老系统编译,或
    • 使用 musl 静态编译
rustup target add x86_64-unknown-linux-musl
cargo build --target x86_64-unknown-linux-musl

五、权限与 PATH 问题

  • rustup 默认安装在 ~/.cargo
  • 不要使用 root 安装 rustup
  • 确保:
echo '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

如果你愿意,可以告诉我:

  • Debian 版本(如 11 / 12)
  • 用途(服务器 / 桌面 / 嵌入式 / CI)
  • 是否国内网络

我可以给你一套最合适的安装命令

0