温馨提示×

centos配置rust要注意啥

小樊
43
2026-09-14 22:39:48
栏目: 编程语言

CentOS 上配置 Rust 环境,有几个常见坑和注意点,下面按“是否推荐 / 容易踩坑”给你总结一下。


一、推荐的安装方式(最重要)

不要用 yum 安装 rust
CentOS 自带仓库里的 rust 版本通常非常旧,很多 crate 根本编译不了。

官方推荐:rustup

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

安装后:

source ~/.cargo/env
rustc --version
cargo --version

二、系统依赖(CentOS 特有坑)

1️⃣ 安装编译工具链(必须)

sudo yum groupinstall "Development Tools"
sudo yum install -y gcc gcc-c++ make

有些 crate 会用到 C/C++ 编译器。


2️⃣ SSL / TLS 相关(很常见)

如果你的 Rust 项目用到:

  • reqwest
  • hyper
  • openssl

一定要装:

sudo yum install -y openssl-devel

否则会报类似:

could not find openssl

3️⃣ 如果是 CentOS 7(特别注意)

CentOS 7 默认:

  • glibc 较老
  • 部分新版 Rust 已不再支持

✅ 建议:

  • Rust 1.70+ 仍然支持 CentOS 7
  • 不要升级到太新的 Rust

查看支持情况:

rustc --version
ldd --version

三、镜像加速(国内服务器必看)

默认 crates.io 很慢,建议配置国内镜像。

编辑或新建:

mkdir -p ~/.cargo
vi ~/.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"

或使用清华镜像也行。


四、防火墙 / 网络问题

如果你在:

  • 云服务器
  • 内网环境

可能遇到:

  • rustup 下载失败
  • crates.io 超时

解决:

export RUSTUP_DIST_SERVER=https://rsproxy.cn
export RUSTUP_UPDATE_ROOT=https://rsproxy.cn/rustup

五、systemd 服务(部署时)

如果是写服务:

[Service]
Environment="PATH=/root/.cargo/bin:/usr/local/bin:/usr/bin"

否则 systemd 找不到 cargo / rustc


六、常见错误速查

问题 原因
linker cc not found 没装 gcc
openssl not found 没装 openssl-devel
GLIBC_2.18 not found CentOS 太老
cargo 很慢 没配镜像

七、总结一句话

CentOS 配 Rust:用 rustup,别用 yum,装 gcc + openssl-devel,配国内镜像。

如果你愿意,可以告诉我:

  • CentOS 几(7 / 8 / Stream)
  • 用途(开发 / 服务器部署 / 编译别的 Rust 项目)

我可以给你一套精确命令

0