温馨提示×

Rust在Debian怎么配置

小樊
50
2026-07-17 17:36:17
栏目: 编程语言

在 Debian 上配置 Rust,一般推荐用 rustup,这是官方且最常用的方式。下面按步骤给你一份常见配置指南。


一、用 rustup 安装 Rust(推荐)

1. 安装依赖

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

2. 安装 rustup

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

安装过程中会问你:

  • 一般直接选 1) Proceed with installation (default)

完成后会安装到:

~/.cargo/bin

3. 让环境生效

source ~/.cargo/env

或者重新打开一个终端即可。

4. 验证安装

rustc --version
cargo --version

二、配置国内镜像(加速下载)

Rust 更新包在国内可能很慢,可以配置中科大或清华镜像。

1. 设置环境变量

编辑 ~/.bashrc~/.zshrc

export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rustup
export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rustup/rustup

然后:

source ~/.bashrc

2. 配置 crates 镜像(可选)

创建或编辑:

~/.cargo/config.toml

内容:

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

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

三、升级与卸载

升级 Rust

rustup update

卸载 Rust

rustup self uninstall

四、安装特定版本(可选)

安装 stable / nightly

rustup install stable
rustup install nightly

切换默认版本

rustup default stable

五、在 Debian 下常见问题

1. 缺少 C 编译器

Rust 编译部分依赖可能需要 gcc:

sudo apt install -y gcc

2. SSL / CA 证书问题

sudo apt install -y ca-certificates

六、验证一个简单示例

cargo new hello
cd hello
cargo run

输出:

Hello, world!

如果你需要:

  • Debian Server(无 GUI)
  • ARM / aarch64
  • CI / Docker 环境
  • 与 C / C++ 混合编译

可以告诉我你的具体使用场景,我可以给你更针对性的配置方案。

0