在 CentOS 上配置 Rust 开发环境时,有几个常见但容易踩坑的点,下面按「系统环境 → 安装 → 编译 → 常见坑」来说明。
cat /etc/centos-release
查看 gcc 版本:
gcc --version
Rust 编译依赖 C 工具链:
# CentOS 7 / 8
sudo yum groupinstall -y "Development Tools"
sudo yum install -y gcc gcc-c++ make
如果是 CentOS 8 / Stream(用 dnf):
sudo dnf groupinstall -y "Development Tools"
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
1) Proceed with installation (default)source $HOME/.cargo/env
验证:
rustc --version
cargo --version
ldd --version
| CentOS | glibc |
|---|---|
| 7 | 2.17 |
| 8 | 2.28 |
⚠️ Rust 官方预编译二进制对 glibc 要求 ≥ 2.17
✅ 建议:
# 使用当前系统 target
rustup target add x86_64-unknown-linux-gnu
如果你要发布 Rust 程序(特别是给别的 Linux 用):
rustup target add x86_64-unknown-linux-musl
cargo build --target x86_64-unknown-linux-musl
✅ 优点:
⚠️ 缺点:
CentOS 上很多 crate 依赖 openssl-sys
sudo yum install -y openssl-devel
export OPENSSL_DIR=/usr
或使用 rustls(无需系统 OpenSSL):
[dependencies]
tokio = { version = "1", features = ["tls"] }
如果是运行 web 服务:
# 防火墙
sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload
# SELinux(调试时)
getenforce
⚠️ SELinux 可能导致 Rust 程序无法绑定端口或访问文件
mkdir -p ~/.cargo
cat > ~/.cargo/config.toml << EOF
[source.crates-io]
replace-with = 'tuna'
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
EOF
✅ 不要直接升级 glibc ✅ 不要强行删除系统 python / gcc
推荐方案:
rustup 管理 Rustmusl 编译发布rustc --version
cargo --version
gcc --version
openssl version
ldd --version
CentOS 上配置 Rust,核心是:
装好 gcc + openssl-devel,注意 glibc 版本,生产环境优先用 musl 静态编译。
如果你愿意,可以告诉我:
我可以给你一套 完全针对你环境的最佳配置方案。