在 CentOS 上搭建 Rust 开发环境,推荐使用官方方式安装,下面按步骤说明(适用于 CentOS 7 / 8 / Stream)。
sudo yum install -y gcc gcc-c++ make curl
如果是 CentOS 8 / Stream,可用
dnf:
sudo dnf install -y gcc gcc-c++ make curl
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
安装过程中会提示:
1) Proceed with installation (default)
2) Customize installation
✅ 直接回车选择默认即可。
source $HOME/.cargo/env
验证是否安装成功:
rustc --version
cargo --version
编辑环境变量文件:
vi ~/.cargo/config
添加以下内容(使用中国科学技术大学镜像):
[source.crates-io]
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
或使用清华镜像:
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
rustup update
rustup self uninstall
cargo new hello_rust
cd hello_rust
cargo run
输出:
Hello, world!
Rust 官方二进制一般兼容 CentOS 7,如果出现问题:
sudo yum install -y glibc-devel
如果在公司内网或服务器无法访问外网:
export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
✅ 最简安装命令
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env
如果你告诉我:
我可以给你更精确的配置方案。