温馨提示×

怎样在ubuntu配置rust

小樊
36
2026-10-28 06:16:48
栏目: 编程语言

在 Ubuntu 上配置 Rust 通常分为 安装 Rust 工具链、配置环境、验证安装 以及(可选)安装常用组件 几个步骤。下面是最常见、最推荐的做法。


一、使用官方推荐方式安装(rustup)

1. 更新系统

sudo apt update
sudo apt upgrade -y

2. 安装依赖

sudo apt install -y build-essential curl

3. 使用 rustup 安装 Rust

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

安装过程中会让你选择:

  • 默认推荐:1) Proceed with installation (default)
  • 回车即可

二、配置环境变量

安装完成后,执行:

source $HOME/.cargo/env

为了让每次打开终端都自动生效,可加入 .bashrc 或 .zshrc:

echo 'source $HOME/.cargo/env' >> ~/.bashrc
source ~/.bashrc

三、验证安装

rustc --version
cargo --version

看到类似输出说明成功:

rustc 1.80.0 (xxxxxx 2024-xx-xx)
cargo 1.80.0 (xxxxxx 2024-xx-xx)

四、更新与管理 Rust

更新 Rust

rustup update

查看已安装版本

rustup toolchain list

安装 Nightly(可选)

rustup toolchain install nightly
rustup default nightly

五、创建第一个 Rust 项目

cargo new hello_rust
cd hello_rust
cargo run

输出:

Hello, world!

六、常见问题

1. 网络问题(无法访问 rustup.rs)

可配置国内镜像(如清华源):

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

再执行安装脚本。

2. 需要 C 编译器

Rust 某些库依赖 C:

sudo apt install -y gcc clang

七、推荐编辑器

  • VS Code + 插件 rust-analyzer
  • Neovim / Vim + rust.vim

如果你告诉我:

  • 是 桌面版还是服务器版 Ubuntu
  • 是否需要 交叉编译 / WASM / 嵌入式
  • 是否使用 国内网络环境

我可以给你更定制化的配置方案。

0 踩