Ruby 安装 - Linux
详解 Linux 与 macOS 平台通过源码编译、系统包管理器(yum/apt/Homebrew)及 RVM 多版本管理器安装 Ruby,并配置 RubyGems 与 Bundler 国内镜像源。
学完这一章,你能够
- 详解 Linux 与 macOS 平台通过源码编译、系统包管理器(yum/apt/Homebrew)及 RVM 多版本管理器安装 Ruby,并配置 RubyGems 与 Bundler 国内镜像源。
源码编译与系统包管理器安装
在 Linux 系统中进行全局安装通常需要 root 或 `sudo` 权限。若选择从官方源码包编译安装,步骤为:下载目标版本的源码压缩包(早期示例为 `ruby-2.2.3.tgz`,现代生产推荐下载 Ruby 3.x 稳定版),使用 `tar -xvzf` 解压并进入目录,依次执行 `./configure`、`make` 和 `sudo make install`,最后运行 `ruby -v` 核对输出的版本号即可确认安装成功。
在联网环境下,直接通过包管理器安装更为快捷:CentOS/Fedora/RHEL 可运行 `sudo yum install ruby`(新版系统对应 `dnf`),Debian/Ubuntu 可运行 `sudo apt-get install ruby-full`,而 macOS 用户可通过 Homebrew 执行 `brew install ruby` 安装独立于系统自带版本的较新 Ruby。
使用 RVM 管理多版本 Ruby
RVM(Ruby Version Manager)可在 Linux、macOS 及其他类 Unix 系统中并存管理多个 Ruby 版本与独立的 gem 集合(现代社区也广泛使用 `rbenv` 或 `asdf`/`mise`)。安装 RVM 时先导入官方 GPG 公钥,再运行安装脚本;安装结束提示执行 `source /etc/profile.d/rvm.sh`(或重开终端自动加载),随后用 `rvm -v` 验证 RVM 已生效。
载入 RVM 后,可通过 `rvm list known` 查看可安装版本,使用 `rvm install <版本号>`(示例为 `2.4.2`,实际建议安装 3.x)自动下载编译 Ruby 与 RubyGems。常用运维命令包括:`rvm list` 查询已安装列表、`rvm remove <版本>` 卸载指定版本、`rvm <版本> --default` 将已安装版本设为系统默认,最后分别运行 `ruby -v` 与 `gem -v` 验证解释器与包管理器版本。
配置 RubyGems 与 Bundler 国内镜像源
RubyGems 的官方默认源为 `https://rubygems.org/`(早期部分中文资料误写为 cocoapods.org,后者实为 iOS 依赖索引)。若国内直连官方源缓慢或超时,可使用 `gem source -r https://rubygems.org/` 移除默认源并 `gem source -a https://gems.ruby-china.com/` 添加镜像源,再运行 `gem sources -l` 确认列表中仅保留镜像地址,之后即可顺畅执行 `gem install rails` 等安装命令。
对于使用 `Gemfile` 与 Bundler 管理的项目(如 Rails),推荐使用 `bundle config mirror.https://rubygems.org https://gems.ruby-china.com` 配置镜像重定向。这样做无需改动团队共享的 `Gemfile` 中的 `source 'https://rubygems.org/'` 声明,即可透明加速依赖下载。
离线校验 Linux 源码编译、RVM 版本管理与 RubyGems/Bundler 镜像配置
展开查看收起
"""Offline Ruby 3.x semantic verification lab for Chapter 04 (installation-unix)."""
import json
VERSION = "CPython 3.12.14 offline Ruby 3.x semantic verifier (MRI ruby absent)"
def case_source_build_and_pkg_manager_commands():
source_steps = ["tar -xvzf ruby-3.3.6.tar.gz", "cd ruby-3.3.6", "./configure", "make", "sudo make install", "ruby -v"]
pkg_cmds = {
"rhel_centos": "sudo yum install ruby",
"debian_ubuntu": "sudo apt-get install ruby-full",
"macos_homebrew": "brew install ruby",
}
expected = {
"source_step_count": 6,
"has_configure_make_install": True,
"pkg_managers": ["debian_ubuntu", "macos_homebrew", "rhel_centos"],
}
actual = {
"source_step_count": len(source_steps),
"has_configure_make_install": "./configure" in source_steps and "make" in source_steps and "sudo make install" in source_steps,
"pkg_managers": sorted(pkg_cmds.keys()),
}
return {
"name": "source_build_and_pkg_manager_commands",
"summary": "校验 Linux/macOS 源码编译三步曲(configure/make/make install)与 yum/apt-get/brew 包管理器安装命令",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_rvm_install_and_version_lifecycle():
installed = []
default_ver = None
# Simulate rvm install / list / default / remove lifecycle
for ver in ("2.0.0", "2.4.2", "3.3.6"):
installed.append(ver)
default_ver = "3.3.6"
installed.remove("2.0.0")
expected = {"installed": ["2.4.2", "3.3.6"], "default": "3.3.6", "requires_source_script": True}
actual = {"installed": installed, "default": default_ver, "requires_source_script": True}
return {
"name": "rvm_install_and_version_lifecycle",
"summary": "模拟 RVM 载入环境、多版本安装、设置默认版本与卸载版本的完整状态流转",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_rubygems_source_mirror_replacement():
sources = ["https://rubygems.org/"]
sources.remove("https://rubygems.org/")
sources.append("https://gems.ruby-china.com/")
expected = {
"sources": ["https://gems.ruby-china.com/"],
"only_mirror_present": True,
"official_default_is_rubygems": True,
}
actual = {
"sources": sources,
"only_mirror_present": sources == ["https://gems.ruby-china.com/"],
"official_default_is_rubygems": True,
}
return {
"name": "rubygems_source_mirror_replacement",
"summary": "验证 RubyGems 源替换流程(移除 rubygems.org 并仅保留镜像源)并澄清官方默认源为 rubygems.org",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_bundler_mirror_without_gemfile_edit():
gemfile_source = "https://rubygems.org/"
bundle_config = {"mirror.https://rubygems.org": "https://gems.ruby-china.com"}
resolved = bundle_config.get(f"mirror.{gemfile_source.rstrip('/')}", gemfile_source)
expected = {
"gemfile_unchanged": "https://rubygems.org/",
"resolved_mirror": "https://gems.ruby-china.com",
}
actual = {
"gemfile_unchanged": gemfile_source,
"resolved_mirror": resolved,
}
return {
"name": "bundler_mirror_without_gemfile_edit",
"summary": "验证 bundle config mirror 配置在不改动 Gemfile 源码的前提下透明重定向下载源",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_source_build_and_pkg_manager_commands(),
case_rvm_install_and_version_lifecycle(),
case_rubygems_source_mirror_replacement(),
case_bundler_mirror_without_gemfile_edit(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
直接修改 `Gemfile` 会影响版本库记录以及海外同事或 CI 环境的构建源;使用 `bundle config mirror` 仅在本地生效,既能享受镜像加速,又能保持 `Gemfile` 与 `Gemfile.lock` 干净统一。