This guide assumes you already have Ruby installed.
If you're using Ruby < 1.9
you'll have to manually install RubyGems as it won't be included natively.
To install a ruby gem, enter the command:
gem install [gemname]
If you are working on a project with a list of gem dependencies, then these will be listed in a file named Gemfile
. To install a new gem in the project, add the following line of code in the Gemfile
:
gem 'gemname'
This Gemfile
is used by the Bundler gem to install dependencies your project requires, this does however mean that you'll have to install Bundler first by running (if you haven't already):
gem install bundler
Save the file, and then run the command:
bundle install
The version number can be specified on the command live, with the -v
flag, such as:
gem install gemname -v 3.14
When specifying version numbers in a Gemfile
, you have several options available:
gem 'gemname')
-- Will install the latest version which is compatible with other gems in the Gemfile
.gem 'gemname', '3.14'
) -- Will only attempt to install version 3.14
(and fail if this is incompatible with other gems in the Gemfile
).gem 'gemname', '>=3.14'
) -- Will only attempt to install the latest version which is compatible with other gems in the Gemfile
, and fails if no version greater than or equal to 3.14
is compatible. The operator >
can also be used.gem 'gemname', '~>3.14'
) -- This is functionally equivalent to using gem 'gemname', '>=3.14', '<4'
. In other words, only the number after the final period is permitted to increase.As a best practice: You might want to use one of the Ruby version management libraries like rbenv or rvm. Through these libraries, you can install different versions of Ruby runtimes and gems accordingly. So, when working in a project, this will be especially handy because most of the projects are coded against a known Ruby version.