Ubuntu 22.04 安装 Rust 1.98 环境指南(rustup/国内源)

在 Ubuntu 22.04 上通过 rustup 安装 Rust 稳定版 1.98、配置 cargo 国内源(rsproxy.cn),并验证编译与运行。本文档所有命令与回显均为 Ubuntu 22.04 实测结果

RustRustrustupcargo2026/09/27

Ubuntu 22.04 安装 Rust 1.98 环境指南(rustup/国内源)

目标:在 Ubuntu 22.04 上通过 rustup 安装 Rust 稳定版 1.98、配置 cargo 国内源(rsproxy.cn),并验证编译与运行。本文档所有命令与回显均为 Ubuntu 22.04 实测结果。

环境信息

  • 操作系统:Ubuntu 22.04.5 LTS(x86_64)
  • Bash:GNU bash 5.1.16(1)
  • rustc:1.98.1(2026-09-01)
  • cargo:1.98.1(2026-08-05)
  • rustup:1.29.1(2026-08-13)
  • rustfmt:1.9.0-stable
  • clippy:0.1.98
  • 编译依赖:curl、gcc、build-essential

前置条件

apt-get update
apt-get install -y curl wget git build-essential pkg-config

💡 说明:build-essential 提供 cc(gcc),Rust 编译链接需要;缺失或缺 cc 工具链会在 cargo build 时提示找不到 cc。

版本选型说明

rustc 版本通道类型说明
stable 1.98当前稳定版推荐生产
beta测试版(下一发布候选)尝鲜
nightly夜间版(每天构建)实验特性

Rust 官方提供三条发布通道(stable/beta/nightly),用 rustup 管理,可自由切换与多版本共存。

部署步骤

步骤 1. 下载 rustup 安装脚本

官方脚本地址 sh.rustup.rs 在部分公网环境不可达,可改用国内镜像 rsproxy.cn 提供的同名脚本:

curl -fsSL https://rsproxy.cn/rustup-init.sh -o /tmp/rustup-init.sh

步骤 2. 非交互安装稳定版工具链

export RUSTUP_DIST_SERVER=https://rsproxy.cn
export RUSTUP_UPDATE_ROOT=https://rsproxy.cn/rustup
export RUSTUP_INIT_SKIP_PATH_CHECK=yes
sh /tmp/rustup-init.sh -y --default-toolchain stable --profile default

💡 说明:

  • RUSTUP_DIST_SERVER / RUSTUP_UPDATE_ROOT 让 rustup 从国内分发源下载工具链组件,规避直连 static.rust-lang.org 慢/不通。
  • -y 接受默认;--default-toolchain stable 默认装稳定版;--profile default 安装 rustc/cargo/rust-std/rustdoc 等默认组件。
  • 安装完成后输出 rustup-init.sh 的提示:新 shell 需 source 环境文件(见步骤 3)。

步骤 3. 加载环境变量

官方安装脚本会将该行追加进 ~/.bashrc,当前会话需手动执行一次:

source "$HOME/.cargo/env"

步骤 4. 验证版本

rustc --version
cargo --version
rustup --version

实测输出:

rustc 1.98.1 (48a229cea 2026-09-01)
cargo 1.98.1 (797e8a9bc 2026-08-05)
rustup 1.29.1 (d95a37b6a 2026-08-13)

配置说明

配置 cargo 国内源(Crates.io 镜像)

国内访问 crates.io 慢,追加 ~/.cargo/config.toml 将默认源替换为 rsproxy:

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

[source.rsproxy]
registry = "sparse+https://rsproxy.cn/index/"

[registry]
default = "rsproxy"

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

💡 说明:~/.cargo/config.toml 为全局配置;sparse+ 走稀疏索引,比 git 拉取索引更快。无需 HTTPS_PROXY 即可拉取依赖。

添加常用组件(格式与静态检查)

rustup component add rustfmt clippy
rustfmt --version
cargo clippy --version

实测输出:

rustfmt 1.9.0-stable (48a229ceae 2026-09-01)
clippy 0.1.98 (48a229ceae 2026-09-01)

验证流程

创建并编译首个项目:

mkdir -p /data/demo && cd /data/demo
cargo new hello_world
cd hello_world
cargo build
./target/debug/hello_world

实测输出:

Creating binary (application) `hello_world` package
Compiling hello_world v0.1.0 (/data/demo/hello_world)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.20s
Hello, world!

✅ 说明:cargo new 自动生成 Cargo.toml 与 src/main.rs;cargo build 拉取并编译依赖(此处无第三方依赖,故未走网络源);运行产物输出 Hello, world!,安装与国内源配置验证通过。

多版本管理与更新

rustup 支持 stable/beta/nightly 多通道共存与切换:

# 查看当前工具链与默认设置
rustup show
rustup toolchain list

# 安装其他通道/版本
rustup install nightly
rustup install beta

# 切换默认工具链
rustup default nightly

# 为单项目指定工具链(写该目录 .rust-toolchain.toml 或 override)
rustup override set nightly

# 临时用某工具链执行
cargo +nightly build

# 更新所有已装工具链(含 rustup 自身)
rustup update

⚠️ 说明:cargo +nightly build 仅本次构建用 nightly,不改全局默认;rustup override set nightly 记录到当前目录,仅对该目录及其子目录生效。

注意事项

  • 非交互 shell(bash -c、脚本)默认不加载 ~/.cargo/env,需显式 source "$HOME/.cargo/env";交互式登录 shell 由 ~/.bashrc 自动生效。
  • RUSTUP_DIST_SERVER / RUSTUP_UPDATE_ROOT 只在安装/更新 rustup 组件时生效,日常 cargo build 不受影响(由 config.toml 控制源)。
  • 若需在服务器上用 Docker 部署 Rust 产物,本指南覆盖到「本机编译」即可;部署打包不在此范围。

常见问题

  • sh.rustup.rs 连接超时/不可达:按步骤 1 改用 https://rsproxy.cn/rustup-init.sh 下载脚本,并按步骤 2 设置分发镜像环境变量。
  • rustc: command not found(新开 shell):~/.bashrc 未含 ~/.cargo/env 加载段,或当前 shell 未 source "$HOME/.cargo/env"。
  • cargo build 报 linker cc not found:缺编译工具链,执行 apt-get install -y build-essential。
  • 拉取 crates.io 慢/超时:确认 ~/.cargo/config.toml 已替换 rsproxy 源。验证:cargo config get source.crates-io.replace-with 应输出 rsproxy。

卸载方式

# 完整移除 rustup 及所有工具链
rustup self uninstall

# 仅移除某个工具链
rustup toolchain uninstall stable

# 清理配置文件(rustup self uninstall 后手工清理残留)
rm -rf ~/.rustup ~/.cargo
sed -i '/.cargo\/env/d' ~/.bashrc

参考链接