温馨提示×

ubuntu rust配置有问题咋办

小樊
47
2026-10-28 07:09:52
栏目: 编程语言

你可以先告诉我具体表现(比如报错内容、现象),不过我先把 Ubuntu 上 Rust 常见“配置有问题”的情况和排查办法给你,基本能覆盖 90% 的情况。


一、最常用先看一眼:环境是否装好

rustc --version
cargo --version
  • 如果命令找不到 → Rust 没装或 PATH 没配
  • 如果版本很老 → 建议升级

二、推荐安装 / 修复方式(最重要)

不要用 apt 直接装 rust(容易版本旧、缺组件)

正确方式(官方)

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"

然后:

rustup update
rustup default stable

三、常见“配置有问题”的情况

1️⃣ cargo: command not found

说明 PATH 没生效

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

如果是 zsh:

echo 'source "$HOME/.cargo/env"' >> ~/.zshrc
source ~/.zshrc

2️⃣ 编译时报缺少 C 编译器(非常常见)

报错类似:

cc not found
linker `cc` not found

解决:

sudo apt update
sudo apt install -y build-essential

3️⃣ 国内网络拉依赖慢 / 失败

配置镜像(推荐):

mkdir -p ~/.cargo

~/.cargo/config.toml:

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

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

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

4️⃣ rustup 本身坏了

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

5️⃣ VSCode 识别不到 Rust

  • 安装扩展:rust-analyzer
  • 确保终端能跑 cargo
  • 重启 VSCode

四、你可以这样给我信息,我直接帮你定位

把下面任意一个贴出来:

  1. 报错截图 / 文字
  2. cargo build 的完整输出
  3. 你做了什么操作(装了啥、改了啥)

我可以直接告诉你哪一行错了、怎么改。

0 踩