Adding A Gem To Your Project

Adding a Gem to your project is pretty simple. You just need to do three things:

  1. Edit your Gem file

Add a line that includes the Gem in your gemset (or gems that are included as requirements for your project to work)

gem 'my_gem_name'

Once, you’ve added a new line, save the changes

  1. Update your Gemset

Then in the command line (Terminal on OSX or Command Prompt on Windows), update your gemset by typing

bundle install

You should see the Gem you just added listed as an addition to the project (highlighted in green)

  1. Require it

In your code file (app.rb), you need to tell the project to include the functionality of the gem. We do this by adding a line at the top of the code file.

require 'my_gem_name'

It’s important this appears right at the top of the codefile, so that the server includes this functionality right as it launches.

This is the workflow for almost all Gems.

You should check the documentation for any gem you want to use in your project. Some of the gems you want to work with may have additional steps like adding some configuration information (e.g. additional instructions on how the gem is used or setup with the code). Most of the developers who share gems will take the time to share these instructions in the README file of their code repository. Take a look there before you use any gem to know what’s involved.



The latest tutorials sent straight to your inbox.