温馨提示×

debian rust环境如何建

小樊
31
2026-10-05 04:29:03
栏目: 编程语言

在 Debian 上搭建 Rust 开发环境,一般按下面步骤来即可(以 Debian 11/12 为例,其他版本类似)。


一、更新系统

sudo apt update
sudo apt upgrade -y

二、安装基础编译工具

Rust 编译需要 C 工具链(尤其是用到 C 依赖时):

sudo apt install -y build-essential curl git pkg-config libssl-dev

三、安装 Rust(官方推荐方式)

不要用 apt 里的 rustc(版本通常很旧),推荐用 rustup:

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

按提示选择:

  • 默认直接回车(推荐)
  • 安装完成后执行:
source "$HOME/.cargo/env"

验证:

rustc --version
cargo --version

四、配置国内镜像(可选,但强烈推荐)

编辑 ~/.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"

或使用清华源:

[source.crates-io]
replace-with = "tuna"

[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/crates.io-index"

五、更新 Rust 工具链

rustup update

常用组件:

rustup component add rustfmt clippy

六、创建第一个项目

cargo new hello
cd hello
cargo run

七、常见问题

1. 缺少 SSL / 系统库

sudo apt install -y libssl-dev

2. 交叉编译(如编译到 ARM)

rustup target add armv7-unknown-linux-gnueabihf

3. 使用系统 LLVM / 加速编译

可考虑安装 sccache:

cargo install sccache

如果你告诉我:

  • Debian 具体版本
  • 用途(服务端 / 嵌入式 / WASM / GUI)
  • 是否用 Docker

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

0 踩