Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Ruby RubyGems

Ruby RubyGems

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

RubyGems is designed to easily manage gem installation tools, as well as servers for distributing gems. This is similar to apt-get on Ubuntu, yum on Centos, and pip on Python.

RubyGems was created around November 2003 and became part of the Ruby standard library starting from Ruby 1.9.

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

Command to update RubyGems:

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


Gem

Gem is the package manager for Ruby modules (called Gems). It contains package information and the files used for installation.

Gems are usually built according to a '.gemspec' file, which contains a YAML file with information about the gem. Ruby code can also build a gem directly; in this case, Rake is usually used.

gem commands

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

gem usage

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

Installation:

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 among available gems, for example:



gem search STRING --remote


Building a gem package

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

Build a .gem from a .gemspec file:

gem build mygem.gemspec


Changing to a domestic source

Due to domestic network issues (you know what I mean), resource files stored by rubygems.org on Amazon S3 experience intermittent connection failures.

So you may encounter situations where gem install rack or bundle install produces no response for a long time. 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/

Next, 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/

# Ensure only gems.ruby-china.com

$ gem install rails

If you use Gemfile and Bundle (for example: a Rails project)

You can use bundle's gem source mirroring command.

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

This way you don't need to change your Gemfile's source.



source 'https://rubygems.org/'

gem 'rails', '4.1.0'

...