Ruby on Rails Gems Gemsets


Example

If you are using RVM(Ruby Version Manager) then using a gemset for each project is a good idea. A gemset is just a container you can use to keep gems separate from each other. Creating a gemset per project allows you to change gems (and gem versions) for one project without breaking all your other projects. Each project need only worry about its own gems.

RVM provides (>= 0.1.8) a @global gemset per ruby interpreter. Gems you install to the @global gemset for a given ruby are available to all other gemsets you create in association with that ruby. This is a good way to allow all of your projects to share the same installed gem for a specific ruby interpreter installation.

Creating gemsets

Suppose you already have ruby-2.3.1 installed and you have selected it using this command:

rvm use ruby-2.3.1

Now to create gemset for this ruby version:

rvm gemset create new_gemset

where the new_gemset is the name of gemset. To see the list of available gemsets for a ruby version:

rvm gemset list

to list the gems of all ruby versions:

rvm gemset list_all

to use a gemset from the list (suppose new_gemset is the gemset I want to use):

rvm gemset use new_gemset

you can also specify the ruby version with the gemset if you want to shift to some other ruby version:

rvm use ruby-2.1.1@new_gemset

to specify a default gemset for a particular ruby version:

rvm use 2.1.1@new_gemset --default

to remove all the installed gems from a gemset you can empty it by:

rvm gemset empty new_gemset

to copy a gemset from one ruby to another you can do it by:

rvm gemset copy 2.1.1@rails4 2.1.2@rails4

to delete a gemset:

rvm gemset delete new_gemset

to see the current gemset name:

rvm gemset name

to install a gem in the global gemset:

rvm @global do gem install ...

Initializing Gemsets during Ruby Installs

When you install a new ruby, RVM not only creates two gemsets (the default, empty gemset and the global gemset), it also uses a set of user-editable files to determine which gems to install.

Working in ~/.rvm/gemsets, rvm searchs for global.gems and default.gems using a tree-hierachy based on the ruby string being installed. Using the example of ree-1.8.7-p2010.02, rvm will check (and import from) the following files:

~/.rvm/gemsets/ree/1.8.7/p2010.02/global.gems
~/.rvm/gemsets/ree/1.8.7/p2010.02/default.gems
~/.rvm/gemsets/ree/1.8.7/global.gems
~/.rvm/gemsets/ree/1.8.7/default.gems
~/.rvm/gemsets/ree/global.gems
~/.rvm/gemsets/ree/default.gems
~/.rvm/gemsets/global.gems
~/.rvm/gemsets/default.gems

For example, if you edited ~/.rvm/gemsets/global.gems by adding these two lines:

bundler
awesome_print

every time you install a new ruby, these two gems are installed into your global gemset. default.gems and global.gems files are usually overwritten during update of rvm.