温馨提示×

centos配置rust需注意啥

小樊
41
2026-09-26 23:57:49
栏目: 编程语言

在 CentOS 上配置 Rust 开发环境,有几个常见坑和注意点,下面按「稳定性 / 版本 / 依赖 / 权限」来总结。


一、系统版本相关(很重要)

1. CentOS 7

  • glibc 较老(2.17)
  • 新版 Rust(1.70+)一般还能用,但:
    • 某些 crate(如 tokio、openssl)可能要求新 glibc
  • 建议使用 Rust 稳定版,不要盲目追 nightly

2. CentOS 8 / Stream

  • 基本没问题
  • 但 CentOS 8 已 EOL,建议转向 Rocky / AlmaLinux

二、安装方式(推荐)

✅ 官方推荐:rustup

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

注意:

  • 需要能访问外网(国内可加镜像)
  • 安装后执行:
source $HOME/.cargo/env

三、国内网络加速(强烈建议)

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"

或(清华):

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

四、编译依赖(最容易踩坑)

1. 必须安装的包

sudo yum install -y gcc gcc-c++ make cmake \
  openssl-devel curl-devel \
  perl pkgconfig

2. 如果用到 openssl

  • CentOS 默认 OpenSSL 1.0/1.1
  • 某些 crate(如 reqwest)建议:
openssl = { version = "0.10", features = ["vendored"] }

避免系统 OpenSSL 不兼容


五、glibc / musl 选择

默认(gnu)

  • 适合 CentOS
  • 依赖系统 glibc

musl(静态)

rustup target add x86_64-unknown-linux-musl
  • 适合打包发布
  • 但 CentOS 上编译 musl 可能需要额外工具链

六、权限与 PATH

  • 不要用 root 直接 rustup
  • 推荐普通用户安装
  • 确保:
echo $PATH | grep cargo

七、升级与维护

rustup update
cargo update

CentOS 上建议:

  • 固定 Rust 版本(生产环境)
rustup default 1.75.0

八、常见错误速查

问题 原因
linker cc not found 没装 gcc
openssl not found 没装 openssl-devel
编译极慢 没开并行 / 机器弱
运行报 glibc 错误 在低版本 CentOS 跑高版本编的二进制

如果你用的是 CentOS 7 还是 8 / 服务器还是容器 / 是否交叉编译,我可以给你更精确的建议。

0 踩