基本命令 #

命令概览 #

RubyGems 提供了丰富的命令行工具来管理 Ruby 包。本节将详细介绍最常用的命令及其选项。

获取帮助 #

bash
# 查看所有命令
gem help commands

# 查看特定命令帮助
gem help install

# 查看命令用法
gem install --help

安装命令 #

gem install #

安装 Gem 包到本地系统。

bash
# 基本安装
gem install rails

# 安装特定版本
gem install rails -v 7.0.4

# 安装最新版本
gem install rails --version latest

# 安装版本范围
gem install rails -v '~> 7.0'

# 安装预发布版本
gem install rails --pre

# 安装到用户目录
gem install rails --user-install

# 安装到指定目录
gem install rails -i /path/to/gems

# 安装时跳过文档
gem install rails --no-document

# 只安装 rdoc 文档
gem install rails --document rdoc

# 安装时显示详细信息
gem install rails --verbose

# 安装时忽略依赖
gem install rails --ignore-dependencies

# 强制重新安装
gem install rails --force

# 从本地文件安装
gem install ./rails-7.0.4.gem

# 从 Git 仓库安装
gem install specific_install
gem specific_install https://github.com/rails/rails.git

安装选项详解 #

选项 描述
-v, --version 指定版本
--pre 安装预发布版本
--user-install 安装到用户目录
-i, --install-dir 指定安装目录
-n, --bindir 指定可执行文件目录
--no-document 不生成文档
--document 指定文档类型
--verbose 显示详细信息
--ignore-dependencies 忽略依赖
--force 强制重新安装
--clear-sources 清除源后安装
-s, --source 使用指定源

更新命令 #

gem update #

更新已安装的 Gem 包。

bash
# 更新所有 Gem
gem update

# 更新特定 Gem
gem update rails

# 更新到特定版本
gem update rails -v 7.0.4

# 更新系统(RubyGems 本身)
gem update --system

# 更新到特定系统版本
gem update --system 3.4.0

# 更新时跳过依赖
gem update rails --ignore-dependencies

# 更新时跳过文档
gem update rails --no-document

# 只更新到最新稳定版
gem update rails --conservative

更新选项详解 #

选项 描述
--system 更新 RubyGems 本身
--no-document 更新时不生成文档
--ignore-dependencies 忽略依赖更新
--conservative 保守更新策略
--minimal-deps 最小依赖更新

卸载命令 #

gem uninstall #

卸载已安装的 Gem 包。

bash
# 卸载 Gem
gem uninstall rails

# 卸载特定版本
gem uninstall rails -v 7.0.4

# 卸载所有版本
gem uninstall rails --all

# 卸载时忽略依赖检查
gem uninstall rails --ignore-dependencies

# 卸载时确认
gem uninstall rails --abort-on-dependent

# 卸载可执行文件
gem uninstall rails --executables

# 卸载用户安装的 Gem
gem uninstall rails --user-install

# 批量卸载
gem uninstall rails rack sinatra

卸载选项详解 #

选项 描述
-v, --version 指定卸载版本
--all 卸载所有版本
-I, --ignore-dependencies 忽略依赖检查
-x, --executables 卸载可执行文件
--abort-on-dependent 有依赖时中止
--user-install 卸载用户安装的

查看命令 #

gem list #

列出已安装的 Gem 包。

bash
# 列出所有已安装的 Gem
gem list

# 列出特定 Gem
gem list rails

# 使用正则表达式搜索
gem list ^rails

# 列出所有版本
gem list --all

# 列出本地和远程 Gem
gem list --remote

# 列出远程特定 Gem
gem list rails --remote

# 列出详细信息
gem list --details

# 只显示版本号
gem list --versions

gem query #

查询 Gem 信息。

bash
# 查询本地 Gem
gem query --local

# 查询远程 Gem
gem query --remote

# 查询特定名称
gem query --name-matches rails

# 查询详细信息
gem query --details

gem info #

显示 Gem 的详细信息。

bash
# 显示 Gem 信息
gem info rails

# 显示详细信息
gem info rails --verbose

# 显示远程信息
gem info rails --remote

搜索命令 #

搜索远程仓库中的 Gem 包。

bash
# 搜索 Gem
gem search rails

# 精确搜索
gem search rails --exact

# 搜索并显示详细信息
gem search rails --details

# 搜索预发布版本
gem search rails --prerelease

# 限制搜索结果数量
gem search rails --limit 10

# 搜索所有页面
gem search rails --all

搜索选项详解 #

选项 描述
-r, --remote 搜索远程仓库
-l, --local 搜索本地
--exact 精确匹配
--details 显示详细信息
--prerelease 包含预发布版本
--limit 限制结果数量
--all 显示所有结果

信息命令 #

gem specification #

显示 Gem 的规范信息。

bash
# 显示 Gem 规范
gem specification rails

# 显示特定版本
gem specification rails -v 7.0.4

# 显示特定字段
gem specification rails version

# 显示远程规范
gem specification rails --remote

# 输出 YAML 格式
gem specification rails --yaml

# 输出 Ruby 格式
gem specification rails --ruby

# 显示所有信息
gem specification rails --all

gem contents #

显示 Gem 的文件列表。

bash
# 显示 Gem 文件
gem contents rails

# 显示所有文件
gem contents rails --all

# 只显示可执行文件
gem contents rails --lib-only

# 显示前缀路径
gem contents rails --prefix

gem which #

查找 Gem 文件位置。

bash
# 查找库文件
gem which rails

# 查找所有匹配文件
gem which rails --all

# 只显示第一个匹配
gem which rails --first

依赖命令 #

gem dependency #

显示 Gem 的依赖关系。

bash
# 显示 Gem 依赖
gem dependency rails

# 显示特定版本依赖
gem dependency rails -v 7.0.4

# 显示反向依赖
gem dependency rails --reverse-dependencies

# 显示远程依赖
gem dependency rails --remote

# 显示管道格式
gem dependency rails --pipe

清理命令 #

gem cleanup #

清理旧版本的 Gem。

bash
# 清理所有旧版本
gem cleanup

# 清理特定 Gem
gem cleanup rails

# 干运行(只显示将删除的)
gem cleanup --dryrun

# 清理时确认
gem cleanup -i

# 清理所有版本(保留最新)
gem cleanup --all

环境命令 #

gem environment #

显示 RubyGems 环境信息。

bash
# 显示所有环境信息
gem environment

# 显示 Gem 安装目录
gem environment gemdir

# 显示 Gem 路径
gem environment gempath

# 显示版本
gem environment version

# 显示远程源
gem environment remotesources

# 显示平台
gem environment platform

# 显示用户配置目录
gem environment user_gemdir

# 以 YAML 格式输出
gem environment --yaml

源管理命令 #

gem sources #

管理 Gem 源。

bash
# 列出所有源
gem sources -l

# 添加源
gem sources --add https://gems.ruby-china.com/

# 删除源
gem sources --remove https://rubygems.org/

# 更新源缓存
gem sources -u

# 清除所有源
gem sources -c

# 添加源时强制更新
gem sources --add https://gems.ruby-china.com/ --update

打包命令 #

gem build #

从 gemspec 构建 Gem 包。

bash
# 构建 Gem
gem build mygem.gemspec

# 构建并指定输出目录
gem build mygem.gemspec -o ./pkg/

# 强制覆盖
gem build mygem.gemspec --force

gem push #

发布 Gem 到 RubyGems.org

bash
# 推送 Gem
gem push mygem-1.0.0.gem

# 推送到私有源
gem push mygem-1.0.0.gem --host https://gems.example.com

# 推送时指定 API 密钥
gem push mygem-1.0.0.gem --key my_key

gem yank #

RubyGems.org 撤销 Gem 版本。

bash
# 撤销特定版本
gem yank mygem -v 1.0.0

# 从私有源撤销
gem yank mygem -v 1.0.0 --host https://gems.example.com

gem owner #

管理 Gem 的所有者。

bash
# 添加所有者
gem owner mygem --add user@example.com

# 删除所有者
gem owner mygem --remove user@example.com

# 显示所有者
gem owner mygem

证书命令 #

gem cert #

管理签名证书。

bash
# 生成证书
gem cert --build user@example.com

# 列出证书
gem cert --list

# 添加证书
gem cert --add cert.pem

# 签名 Gem
gem build mygem.gemspec --sign

Bundler 命令 #

bundle install #

安装 Gemfile 中的依赖。

bash
# 安装依赖
bundle install

# 安装到指定目录
bundle install --path vendor/bundle

# 安装生产环境依赖
bundle install --without development test

# 安装时使用并行
bundle install --jobs 4

# 安装时重试
bundle install --retry 3

# 安装时显示详细
bundle install --verbose

# 静默安装
bundle install --quiet

# 只部署(不更新)
bundle install --deployment

# 清理未使用的 Gem
bundle install --clean

bundle update #

更新依赖。

bash
# 更新所有依赖
bundle update

# 更新特定 Gem
bundle update rails

# 更新到最新版本
bundle update --full-index

# 只更新 patch 版本
bundle update --patch

# 只更新 minor 版本
bundle update --minor

# 只更新 major 版本
bundle update --major

# 更新时保守处理
bundle update --conservative

# 更新时严格处理
bundle update --strict

bundle exec #

在 Bundle 环境中执行命令。

bash
# 执行 Ruby 脚本
bundle exec ruby app.rb

# 执行 Rails 命令
bundle exec rails server

# 执行 Rake 任务
bundle exec rake db:migrate

# 执行测试
bundle exec rspec

# 执行 IRB
bundle exec irb

bundle outdated #

检查过时的依赖。

bash
# 检查过时的 Gem
bundle outdated

# 只检查特定 Gem
bundle outdated rails

# 只检查 patch 更新
bundle outdated --patch

# 只检查 minor 更新
bundle outdated --minor

# 只检查 major 更新
bundle outdated --major

# 显示严格模式
bundle outdated --strict

bundle show #

显示已安装的 Gem 信息。

bash
# 显示所有 Gem
bundle show

# 显示特定 Gem 路径
bundle show rails

# 显示 Gem 版本
bundle show rails --version

bundle list #

列出已安装的 Gem。

bash
# 列出所有 Gem
bundle list

# 只显示名称
bundle list --name-only

# 显示 Gem 路径
bundle list --paths

bundle check #

检查依赖是否满足。

bash
# 检查依赖
bundle check

# 检查并显示缺失
bundle check --dry-run

bundle clean #

清理未使用的 Gem。

bash
# 清理未使用的 Gem
bundle clean

# 强制清理
bundle clean --force

# 干运行
bundle clean --dry-run

bundle package #

打包依赖。

bash
# 打包所有依赖
bundle package

# 打包时包含 git 依赖
bundle package --all

# 只打包 gem 依赖
bundle package --gemfile

bundle init #

创建 Gemfile。

bash
# 创建 Gemfile
bundle init

# 指定 gem 名称
bundle init --gemspec=mygem.gemspec

bundle gem #

创建新的 Gem 骨架。

bash
# 创建 Gem
bundle gem mygem

# 创建带测试的 Gem
bundle gem mygem --test=rspec

# 创建带 MIT 许可证的 Gem
bundle gem mygem --mit

# 创建带可执行文件的 Gem
bundle gem mygem --exe

# 创建带 GitHub Actions 的 Gem
bundle gem mygem --ci=github

# 创建 Rails 引擎
bundle gem myengine --engine

bundle config #

配置 Bundler。

bash
# 查看所有配置
bundle config

# 设置配置
bundle config set path 'vendor/bundle'

# 设置全局配置
bundle config set --global path 'vendor/bundle'

# 设置本地配置
bundle config set --local path 'vendor/bundle'

# 删除配置
bundle config unset path

# 查看特定配置
bundle config get path

# 设置镜像源
bundle config set mirror.https://rubygems.org https://gems.ruby-china.com

bundle viz #

生成依赖图。

bash
# 生成依赖图
bundle viz

# 指定输出文件
bundle viz --file=dependencies

# 指定格式
bundle viz --format=png

# 不显示版本
bundle viz --no-version

# 不显示依赖
bundle viz --no-requirements

命令速查表 #

gem 命令 #

命令 描述
gem install 安装 Gem
gem update 更新 Gem
gem uninstall 卸载 Gem
gem list 列出已安装的 Gem
gem search 搜索远程 Gem
gem info 显示 Gem 信息
gem build 构建 Gem
gem push 发布 Gem
gem cleanup 清理旧版本
gem environment 显示环境信息
gem sources 管理源

bundle 命令 #

命令 描述
bundle install 安装依赖
bundle update 更新依赖
bundle exec 执行命令
bundle outdated 检查过时依赖
bundle show 显示 Gem 信息
bundle list 列出 Gem
bundle check 检查依赖
bundle clean 清理 Gem
bundle package 打包依赖
bundle init 创建 Gemfile
bundle gem 创建 Gem 骨架
bundle config 配置 Bundler

下一步 #

现在你已经掌握了 RubyGems 的基本命令,接下来学习 Gemfile 详解 了解如何声明和管理项目依赖!

最后更新:2026-03-28