Ruby RubyGems

RubyGems is a package manager for Ruby. It provides a standard format for distributing Ruby programs and libraries, and also provides a tool for managing package installation.

RubyGems is designed to conveniently manage the installation of gems, as well as servers for distributing gems. This is similar to apt-get under Ubuntu, yum for CentOS, and pip for Python.

RubyGems was created around November 2003, and has been part of Ruby's standard library since Ruby 1.9.

If your Ruby version is lower than 1.9, you can also install it manually:

Update RubyGems command:

$ gem update --system          # 需要管理员或root用户

Gem

Gem is a package manager for Ruby modules (called Gems). It contains package information as well as files used for installation.

Gems are usually built from a ".gemspec" file, which is a YAML file containing information about the Gem. Ruby code can also build Gems directly, in which case Rake is typically used.

gem command

The gem command is used to build, upload, download, and install Gem packages.

gem usage

RubyGems is very similar in functionality to apt-get, portage, yum, and npm.

Install:

gem install mygem

Uninstall:

gem uninstall mygem

List installed gems:

gem list --local

List available gems, for example:

gem list --remote

Create RDoc documentation for all gems:

gem rdoc --all

Download a gem without installing it:

gem fetch mygem

Search available gems, for example:

gem search STRING --remote

Building gem packages

The gem command is also used to build and maintain .gemspec and .gem files.

Build a .gem file from a .gemspec file:

gem build mygem.gemspec

Change to a domestic mirror source

Due to network conditions in China (you know why), resource files hosted on Amazon S3 by rubygems.org intermittently fail to connect.

So you may find that `gem install rack` or `bundle install` takes a long time to respond. You can use `gem install rails -V` to see the execution process.

Therefore, we can change it to a domestic download source:https://gems.ruby-china.com

First, check the current source:

$ gem sources -l
*** CURRENT SOURCES ***

https://rubygems.org/

Then remove https://rubygems.org/ and add the domestic download source https://gems.ruby-china.com/.

$ gem sources --remove https://rubygems.org/
$ gem sources -a https://gems.ruby-china.com/
$ gem sources -l
*** CURRENT SOURCES ***

https://gems.ruby-china.com/
# 请确保只有 gems.ruby-china.com
$ gem install rails

If you use Gemfile and Bundler (e.g., Rails projects)

You can use bundle's gem source mirror command.

$ bundle config mirror.https://rubygems.org https://gems.ruby-china.com/

This way, you don't need to change the `source` in your Gemfile.

source 'https://rubygems.org/'
gem 'rails', '4.1.0'
...
Other extensions