温馨提示×

Debian怎样安装Rust依赖

小樊
37
2026-08-27 20:31:22
栏目: 编程语言

在 Debian 系统上安装 Rust 及其依赖,常见做法如下(推荐、最常用、也最安全):


✅ 一、使用官方推荐方式安装 Rust(首选)

1. 安装 Rust 工具链(rustup)

Debian 官方仓库里的 Rust 版本通常较旧,推荐使用 rustup

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

安装过程中选择:

  • 默认安装(直接回车)

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

source "$HOME/.cargo/env"

验证:

rustc --version
cargo --version

✅ 二、安装 Rust 编译所需的系统依赖(非常重要)

Debian 下编译 Rust 项目通常需要以下依赖:

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

常见额外依赖(按需安装)

场景 依赖
使用 OpenSSL libssl-dev
使用 SQLite libsqlite3-dev
使用 PostgreSQL libpq-dev
使用 MySQL libmysqlclient-dev
使用系统库(C/C++) clang / cmake

示例:

sudo apt install -y cmake clang

✅ 三、安装 Rust 组件(常用)

rustup component add rustfmt clippy
  • rustfmt:代码格式化
  • clippy:代码检查工具

✅ 四、使用 Cargo 安装 Rust 依赖(项目级)

1. 创建项目

cargo new hello_rust
cd hello_rust

2. 添加依赖(例如 reqwest

编辑 Cargo.toml

[dependencies]
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }

然后:

cargo build

Cargo 会自动下载并编译依赖。


✅ 五、Debian 下常见问题

1. 编译慢 / 报错 SSL

✅ 确保已安装:

sudo apt install libssl-dev pkg-config

2. 使用旧版 Debian(如 Debian 10)

推荐:

  • 使用 rustup
  • 不要使用 apt install rustc(版本太旧)

3. 查看 Rust 安装路径

rustup show

✅ 六、卸载 Rust(如需要)

rustup self uninstall

✅ 七、总结(最简流程)

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

如果你有 具体项目(如 cargo build 报错、某个 crate 编译失败),可以把错误信息贴出来,我可以帮你精确分析依赖问题。

0