温馨提示×

CentOS系统中Rust版本如何升级

小樊
46
2025-12-25 06:51:12
栏目: 编程语言

CentOS系统Rust版本升级指南

一、推荐方式 rustup 升级

  • 适用于CentOS 7/8/Stream/9,无需 root,管理多版本最方便。
  • 安装或更新 rustup(如已安装可跳过安装):
    • curl --proto ‘=https’ --tlsv1.2 -sSf https://sh.rustup.rs | sh
    • 安装后加载环境:source “$HOME/.cargo/env”
  • 升级到最新稳定版:
    • rustup update
  • 指定升级到某个版本:
    • rustup update 1.85.0
  • 切换默认工具链(稳定/测试/夜间版):
    • rustup default stable
    • rustup default nightly
  • 验证结果:
    • rustc --version
    • cargo --version
    • rustup show
  • 补充:如需常用工具组件
    • rustup component add rustfmt clippy

二、使用发行版包管理器升级

  • 对于CentOS 7/8/Stream,可直接用 dnf/yum 安装或升级系统提供的稳定版(版本通常较旧):
    • sudo dnf install -y rust cargo
  • 如需较新版本,优先考虑使用rustup而非强行升级系统包(发行版仓库更新滞后且跨版本升级易产生依赖冲突)。

三、常见问题与处理

  • 老系统(如CentOS 7)使用系统仓库升级常失败,原因是新版 Rust 依赖较新的LLVM、glibc、libstdc++等,强行升级会牵动系统库,风险高。建议改用rustup在用户态管理工具链,避免替换系统库。

  • 国内网络下载慢可配置镜像(示例为 rsproxy):

    • echo ‘export RUSTUP_DIST_SERVER=https://rsproxy.cn’ >> ~/.bash_profile
    • echo ‘export RUSTUP_UPDATE_ROOT=https://rsproxy.cn/rustup’ >> ~/.bash_profile
    • source ~/.bash_profile
    • 之后执行 rustup update 会走镜像加速。

四、升级后验证与回滚

  • 验证命令:
    • rustc --version
    • cargo --version
    • rustup show
    • rustup check(检查是否有可用更新)
  • 回滚到指定版本:
    • rustup default 1.82.0
    • 或 rustup toolchain install 1.82.0 && rustup default 1.82.0
  • 卸载(如需):
    • rustup self uninstall

0