温馨提示×

centos配置rust注意什么

小樊
36
2026-09-29 23:32:07
栏目: 编程语言

在 CentOS 上配置 Rust 开发环境时,有几个常见坑和注意点,下面按“系统版本 + 安装方式 + 常见问题”来说明。


一、先确认 CentOS 版本

cat /etc/redhat-release
  • CentOS 7:默认 glibc 较老(2.17),部分新 Rust 版本/依赖可能不支持
  • CentOS 8 / Stream:情况好很多,但官方支持已结束,建议用 Rocky / Alma 替代

二、推荐安装方式(不要用 yum 自带的 rust)

✅ 官方推荐:rustup

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

验证:

rustc --version
cargo --version

三、必须安装的依赖(非常重要)

CentOS 默认 没有完整编译工具链

CentOS 7

sudo yum groupinstall "Development Tools"
sudo yum install -y openssl-devel curl-devel

CentOS 8 / Stream

sudo dnf groupinstall "Development Tools"
sudo dnf install -y openssl-devel

⚠️ 如果缺少 gcc / make,编译 Rust 项目会直接失败


四、OpenSSL 相关的坑(最常见)

很多 Rust 项目用 reqwest / tokio / sqlx,默认依赖系统 OpenSSL。

情况 1:正常链接系统 OpenSSL

确保:

openssl-devel

已安装

情况 2:CentOS 7 老版本 OpenSSL

可能报错:

error: failed to run custom build command for `openssl-sys`

解决方式(任选):

# 指定版本(推荐)
export OPENSSL_DIR=/usr

或使用 vendored:

[dependencies]
openssl = { version = "0.10", features = ["vendored"] }

五、glibc 兼容性问题(CentOS 7 重点)

  • Rust 官方 不再保证在 CentOS 7 上长期可用
  • 新 Rust 编译出的二进制可能在老系统无法运行

✅ 建议:

  • 使用 较旧 Rust 版本
  • 或在 Docker 中编译
  • 或升级到 Rocky Linux 8/9

六、cargo 国内镜像(提速)

mkdir -p ~/.cargo
cat > ~/.cargo/config.toml <<EOF
[source.crates-io]
replace-with = "rsproxy"

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

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

七、防火墙 & SELinux(部署时)

如果是 Web 服务:

sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload

SELinux 可能导致 bind 失败:

sudo setsebool -P httpd_can_network_connect 1

八、推荐组合(稳定方案)

✅ CentOS 7

  • Rust ≤ 1.70
  • openssl vendored
  • Docker 编译

✅ CentOS Stream / Rocky 9

  • 最新 Rust
  • 系统 OpenSSL

如果你告诉我:

  • CentOS 具体版本
  • 是否用于 编译 / 运行 / 部署
  • 是否使用 Docker

我可以给你一套更精确的配置方案。

0 踩