温馨提示×

如何为debian配置rust环境

小樊
33
2026-09-02 04:50:50
栏目: 编程语言

下面按从零开始的思路,介绍在 Debian 上配置 Rust 开发环境的常见做法,覆盖安装、工具链管理、常用组件和验证。


一、系统准备(推荐)

先更新系统:

sudo apt update
sudo apt upgrade -y

安装基础依赖(编译 Rust 程序通常需要):

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

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

✅ 使用 rustup(最推荐)

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

安装过程中选择:

  • 1) Proceed with standard installation(默认即可)

安装完成后,加载环境变量:

source "$HOME/.cargo/env"

或重新打开终端。


三、验证安装

rustc --version
cargo --version

示例输出:

rustc 1.81.0 (xxxxxx 2024-xx-xx)
cargo 1.81.0 (xxxxxx 2024-xx-xx)

四、管理 Rust 工具链(rustup)

查看已安装工具链

rustup toolchain list

安装稳定版 / nightly

rustup install stable
rustup install nightly

设置默认版本

rustup default stable

更新 Rust

rustup update

五、配置 Cargo(可选但推荐)

Cargo 是 Rust 的包管理器和构建工具。

修改 cargo 镜像(国内加速,可选)

编辑或创建:

mkdir -p ~/.cargo
nano ~/.cargo/config.toml

示例(使用中国科学技术大学镜像):

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

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

六、安装常用组件(推荐)

1️⃣ Rust 分析工具(IDE 支持)

rustup component add rustfmt
rustup component add clippy
  • rustfmt:代码格式化
  • clippy:静态检查

使用:

cargo fmt
cargo clippy

2️⃣ 文档工具

cargo doc --open

七、创建第一个 Rust 项目

cargo new hello_rust
cd hello_rust
cargo run

输出:

Hello, world!

八、在 Debian 上开发常见补充

如果是服务器 / 无界面环境

  • 不需要 GUI
  • 推荐安装 rlwrap(方便 REPL 体验,可选)
sudo apt install rlwrap

如果是嵌入式 / 交叉编译

可后续安装 target:

rustup target add armv7-unknown-linux-gnueabihf

九、卸载 Rust(如需)

rustup self uninstall

十、总结(最简流程)

sudo apt update
sudo apt install -y build-essential curl
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"
rustc --version

如果你有特定用途(如:

  • Web(Actix / Axum)
  • 系统编程
  • WASM
  • 嵌入式
  • Debian 旧版本)

可以告诉我,我可以给你更针对性的配置方案。

0