在 CentOS 上编译 Rust(无论是编译 Rust 编译器本身,还是用 Rust 编译项目)经常会遇到卡住、极慢、OOM等问题。下面按常见原因给你一套排查与解决办法。
Rust 编译非常吃内存,尤其是:
rustc / cargo 源码debug 模式make 或 cargo build 卡在某一步dmesg 里有 Out of memory1️⃣ 限制并行编译数
export CARGO_BUILD_JOBS=1
# 或
cargo build -j 1
如果是编译 rust 源码:
./x.py build -j 1
2️⃣ 加 swap(强烈建议)
dd if=/dev/zero of=/swapfile bs=1M count=4096
mkswap /swapfile
swapon /swapfile
方案 A:用官方预编译 Rust(最快)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
✅ 不需要本地编译 rustc
方案 B:升级工具链(CentOS 7)
yum install centos-release-scl
yum install devtoolset-11
scl enable devtoolset-11 bash
如果你是在编译 rust-lang/rust,注意:
./x.py build library --stage 1
而不是全量 build
有时候 debug 编译反而更卡(LLVM 优化耗资源)
cargo build --release
mkdir /dev/shm/rustbuild
export CARGO_TARGET_DIR=/dev/shm/rustbuild
看是否在动:
top
htop
iostat -x 1
如果 cargo / rustc CPU 占用 > 90%,只是慢,不是卡。
✅ 不要自己编译 Rust 编译器
✅ 用 rustup 装官方二进制
✅ 小内存机器:
export CARGO_BUILD_JOBS=1
✅ 加 swap ✅ 用 release 模式
如果你愿意,可以告诉我:
我可以直接给你一套“能跑起来”的具体命令。