在 Debian 上安装 Rust 的两种常用方式
- 使用 apt 安装 Debian 官方仓库的稳定版(简单、稳定,版本通常略滞后)。
- 使用 rustup 安装官方工具链(可轻松切换稳定版/测试版/夜间版,获取最新特性)。
方式一 使用 apt 安装
- 更新索引并安装:
- sudo apt update
- sudo apt install rustc cargo
- 验证安装:
- rustc --version
- cargo --version
- 说明:
- 此方式适合追求系统一致性、无需频繁切换工具链的场景;版本由 Debian 仓库决定,可能不如官方最新稳定版新。
方式二 使用 rustup 安装(官方推荐)
- 安装 rustup(官方版本管理器):
- curl --proto ‘=https’ --tlsv1.2 -sSf https://sh.rustup.rs | sh
- 使环境变量生效(安装脚本一般会提示;若未生效,手动执行):
- source “$HOME/.cargo/env”
- 验证安装:
- rustc --version
- cargo --version
- 常用管理命令:
- 更新工具链:rustup update
- 设置默认工具链:rustup default stable(或 beta、nightly)
- 安装额外组件:rustup component add rustfmt clippy
- 为交叉编译添加目标:rustup target add armv7-unknown-linux-gnueabihf
- 说明:
- 适合需要多版本并存、尝鲜新特性或做跨平台编译的开发者。
快速验证与第一个项目
- 创建并运行项目:
- cargo new hello_world
- cd hello_world
- cargo run
- 预期输出:显示 “Hello, world!”。
常见问题与提示
- 命令未找到:确认已将 $HOME/.cargo/bin 加入 PATH,可执行:echo ‘export PATH=“$HOME/.cargo/bin:$PATH”’ >> ~/.bashrc && source ~/.bashrc(或对应 shell 的配置文件)。
- 升级策略:
- 使用 rustup:rustup update
- 使用 apt:sudo apt update && sudo apt upgrade rustc cargo
- 选择建议:
- 需要最新特性与多版本管理:优先用 rustup。
- 追求系统稳定与统一依赖管理:优先用 apt。