温馨提示×

Debian配置Rust要注意啥

小樊
40
2026-09-14 08:36:15
栏目: 编程语言

在 Debian 上配置 Rust 环境,有几个常见坑和注意点,按使用场景给你梳理一下:


一、安装方式选择(很重要)

✅ 推荐:官方 rustup

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

或离线安装 rustup-init。

原因

  • Debian 仓库里的 rustc / cargo 版本严重过时
  • rustup 可管理多版本、稳定/ nightly / beta

⚠️ 不要优先用 apt 装 Rust 做开发

sudo apt install rustc cargo  # 不推荐(除非只跑旧程序)

二、环境变量(新手最容易漏)

安装完后:

source $HOME/.cargo/env

建议写入:

echo 'source $HOME/.cargo/env' >> ~/.bashrc
# 或 ~/.zshrc

否则会出现:

cargo: command not found

三、Debian 常见依赖问题

1️⃣ C 编译器(必须)

很多 crate 依赖 C 编译:

sudo apt install build-essential

否则报错:

failed to run custom build command for `xxx`
cc not found

2️⃣ 常见系统库(按需)

功能
SSL libssl-dev
数据库 libsqlite3-dev
图像 libpng-dev
压缩 zlib1g-dev

四、glibc / musl 注意(服务器常见)

  • Debian 默认使用 glibc
  • 若想静态链接(Docker / 发布):
rustup target add x86_64-unknown-linux-musl

五、WSL / 服务器注意

  • WSL1:Rust 可用,但 IO 慢
  • WSL2 / 云服务器:完全没问题
  • 树莓派 / ARM:
rustup target add armv7-unknown-linux-gnueabihf

六、编辑器 / IDE

  • VS Code + rust-analyzer(必须)
  • 不要只用 vim 原生(太痛苦)
sudo apt install rust-analyzer  # 可选,但建议用扩展装

七、升级与维护

rustup update
cargo clean

定期清理:

cargo cache  # 第三方工具

八、Debian 特有小坑

  • 老 Debian(10 以下):glibc 太旧,新 Rust 可能不支持
  • 用 root 跑 cargo 会警告(正常)

九、最小可用环境总结 ✅

sudo apt update
sudo apt install build-essential curl
curl https://sh.rustup.rs -sSf | sh
source $HOME/.cargo/env

如果你告诉我:

  • Debian 版本(11 / 12 / 老系统)
  • 用途(Web / 系统 / 嵌入式 / Docker)

我可以给你更精确的配置方案

0