温馨提示×

如何在Debian配置Rust

小樊
49
2026-07-17 17:35:12
栏目: 编程语言

在 Debian 上配置 Rust 主要有两种方式:官方推荐方式(rustup)系统包管理器方式(apt)。下面分别说明,并给出常见配置建议。


一、推荐方式:使用 rustup(官方工具链管理器)

这是 最常用、最灵活 的方式,适合开发 Rust 程序。

1. 安装依赖

sudo apt update
sudo apt install -y curl build-essential gcc

2. 安装 Rust(通过 rustup)

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

安装过程中会询问安装方式,一般直接按 Enter 选择默认即可。

3. 配置环境变量

安装完成后执行:

source ~/.cargo/env

或重新登录终端。

4. 验证安装

rustc --version
cargo --version

成功示例:

rustc 1.78.0 (9b00956e5 2024-04-29)
cargo 1.78.0

二、使用 Debian 官方仓库安装(不推荐用于开发)

适合只需要运行 Rust 程序或系统维护。

sudo apt update
sudo apt install -y rustc cargo

⚠️ 注意:

  • Debian 仓库中的 Rust 版本通常 较旧
  • 不适合需要最新特性的开发

三、配置 Rust 工具链(常用操作)

1. 更新 Rust

rustup update

2. 安装指定版本

rustup install stable
rustup install nightly

3. 切换默认工具链

rustup default stable

4. 为特定项目设置工具链

在项目目录中:

rustup override set nightly

四、配置国内镜像(加速下载,推荐国内用户)

编辑或创建配置文件:

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

添加以下内容(以中国科学技术大学镜像为例):

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

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

其他可选镜像:

  • 清华:https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git
  • 字节跳动:https://rsproxy.cn/index/

五、创建并运行第一个 Rust 项目

cargo new hello_rust
cd hello_rust
cargo run

输出:

Hello, world!

六、常见问题

1. 编译失败(缺少 C 编译器)

sudo apt install -y gcc

2. 卸载 Rust

rustup self uninstall

如果你告诉我:

  • 使用的是 Debian 11 / 12 / 13
  • ARM / x86_64
  • 用于 嵌入式 / Web / 系统开发

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

0