温馨提示×

如何在ubuntu装好rust配置

小樊
46
2026-10-28 07:10:48
栏目: 编程语言

下面给你一份在 Ubuntu 上安装并配置 Rust 的标准做法(2024 年仍适用,适用于 Ubuntu 20.04 / 22.04 / 24.04)。


一、安装 Rust(推荐方式:rustup)

1. 更新系统

sudo apt update
sudo apt upgrade -y

2. 安装依赖

sudo apt install -y build-essential curl git

3. 使用官方脚本安装 Rust

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

安装过程中会让你选择:

  • 默认选 1) Proceed with standard installation

二、配置环境变量

安装完成后执行:

source "$HOME/.cargo/env"

✅ 建议把下面这行加进 ~/.bashrc(或 ~/.zshrc):

export PATH="$HOME/.cargo/bin:$PATH"

然后:

source ~/.bashrc

三、验证安装

rustc --version
cargo --version

看到类似输出说明成功:

rustc 1.80.1 (...)
cargo 1.80.1 (...)

四、常用配置(推荐)

1. 设置为稳定版(默认就是)

rustup default stable

2. 添加常用组件

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

3. 换国内镜像(可选,加速)

编辑 ~/.cargo/config.toml:

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

[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"

[net]
git-fetch-with-cli = true

五、第一个 Rust 项目

cargo new hello
cd hello
cargo run

六、常见问题

❓ sudo: cargo: command not found

Rust 是用户级安装,不要用 sudo cargo

❓ 想卸载 Rust

rustup self uninstall

如果你是要:

  • 配置 VSCode Rust 环境
  • 做 嵌入式 / WASM / Linux 系统编程
  • 或 服务器部署 Rust 程序

可以继续告诉我你的使用场景,我可以给你更针对性的配置。

0 踩