Lesson Weekend

A gem is a package of Ruby code we can use in our Ruby programs or run from the command line. We’ll be using many gems throughout Ruby and Rails.

We’ll manage the gems in our projects using a gem called Bundler. Since Ruby version 2.6, Bundler comes installed with Ruby. Before Ruby 2.6, we would have to install Bundler to our local environment by running gem install bundler in the terminal. (You won't need to do this unless you are using a version of Ruby before 2.6.)

In fact, we can install any gem with the gem install <gem-name> command. We can also uninstall gems with gem uninstall <gem-name>. Generally, though, we should install gems directly in our project with Bundler.

We can manage a project’s gems with a Gemfile. To do this, we add a file called Gemfile to the root directory of our project. Gemfile needs to be capitalized and it never has an extension. All of your Ruby projects should always include a Gemfile with all required gems.

Here’s a sample Gemfile:

source 'https://rubygems.org'

gem 'rspec'
gem 'pry'

The first line of our Gemfile is always the source. Otherwise Ruby can’t fetch the gems we need. The two gems included in this project are popular in the Ruby community and we’ll be using both starting in this course section. rspec is used for running tests while pry is for debugging code.

To actually install gems in our project, we run bundle install in the root directory of the project. We can also run bundle as well. Each time we make a change to our Gemfile, we need to run this command before any new changes take effect.

When you run Bundler for the first time in a project, it creates a Gemfile.lock file. We can open this file and see all the gems from our Gemfile as well as the gems they depend on and their versions.

When you want to update your gems, you can run $ bundle update to update everything. Generally, this is a bad idea. You should only update one gem at a time with $ bundle update <gem-name>, then make sure everything works before updating any other gems. This way, if a gem update breaks something in your code, you'll know which gem caused the damage.

Often, developers will specify versions in their Gemfile itself:

source 'https://rubygems.org'

gem 'rack', '~> 2.0.1'

Most gems are available on GitHub along with a README that includes documentation. (The gem mentioned above provides a basic interface for building web applications; both Sinatra and Rails are built on rack.) The gems we use at Epicodus are well vetted and very popular in the Ruby and Rails community. However, anyone can make a gem and put it on GitHub, so not all gems are useful.

In general, when considering a new gem, it’s a good idea to do a little research on it to make sure you need it. It’s also helpful to take a look at the number of commits and the latest commit. That way, you can see when the project was last updated and if it’s getting regular contributions. You can get information on gems from Ruby Gems, including the number of times a gem has been downloaded. There’s even a stats page that shows the all-time most downloaded gem for Ruby and Rails!

Gem: A package of Ruby code we can use in our Ruby programs or run from the command line.

To add gems to a project, create a Gemfile in the root directory of that project. Here's a sample Gemfile:

source 'https://rubygems.org'

gem 'rspec'
gem 'pry'

Then run bundle or bundle install in the root directory. This will create a Gemfile.lock file and all gems will be installed in the project.

Lesson 9 of 10
Last updated August 7, 2022