温馨提示×

rust在ubuntu怎么配置好

小樊
43
2026-10-28 06:44:51
栏目: 编程语言

下面按「从零开始」的思路,给你一份在 Ubuntu 上配置 Rust 开发环境 的实用指南,适用于 Ubuntu 20.04 / 22.04 / 24.04。


一、安装 Rust(推荐方式)

不要直接用 apt 安装(版本旧),用官方工具链:

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

安装过程中选择:

  • 1) Proceed with standard installation(默认即可)

完成后:

source "$HOME/.cargo/env"

或重新打开终端。

验证:

rustc --version
cargo --version

二、配置环境变量(一般已自动完成)

确认 ~/.bashrc 或 ~/.zshrc 中有:

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

如果用了 zsh:

source ~/.zshrc

三、更换国内镜像(国内用户强烈推荐)

1. 设置 cargo 镜像

mkdir -p ~/.cargo

编辑 ~/.cargo/config.toml:

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

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

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

或使用清华源:

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

[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

四、安装常用工具

1. 代码格式化 & lint

rustup component add rustfmt
rustup component add clippy

使用:

cargo fmt
cargo clippy

2. 构建加速(可选)

cargo install cargo-watch
cargo install cargo-udeps

五、安装系统依赖(很重要)

如果你要写系统级 / GUI / 网络程序,先装:

sudo apt update
sudo apt install -y build-essential pkg-config libssl-dev

常见额外依赖:

  • GUI(gtk):libgtk-3-dev
  • 数据库:libsqlite3-dev
  • 音频:libasound2-dev

六、IDE / 编辑器配置

VS Code(最推荐)

安装插件:

  • rust-analyzer(官方推荐)
  • CodeLLDB(调试用)

设置中启用:

"rust-analyzer.check.command": "clippy"

七、创建第一个项目

cargo new hello
cd hello
cargo run

八、升级与卸载

升级:

rustup update

卸载:

rustup self uninstall

九、常见问题

1. error: linker cc not found

sudo apt install build-essential

2. 交叉编译(如 Windows)

rustup target add x86_64-pc-windows-gnu

如果你告诉我:

  • Ubuntu 版本
  • 是否国内网络
  • 用来干什么(Web / 系统 / 嵌入式 / 游戏)

我可以给你更针对性的配置方案。

0 踩