How Do I Install Gems to my Rails Project?
There are two related questions to which I’ll give the same answer:
1. I ran the command
gem install mygem from the Terminal in my project folder. What happened to the gem? Why Can’t I use it?
2. I have a Rails project, and I don’t want all my gems being packaged with my Rails project. How do I pick which ones get used?
The answer to both is the same:
Your Gemfile (located in your rails root folder) is a list of every gem your Rails app needs to run. Your rails app won’t start including anything but default gems unless you add it here.
So when you run a command like
gem install sass, nothing will happen to any of your rails projects. Instead, your global Ruby installation will simply have that Gem available to use in any of your projects, but won’t actually include that gem unless it’s actually written into a Gemfile somewhere. But in order to understand gems a little better, let’s spend a little time on the string that ties them together: Bundler.
Bundler is the Devil Incarnate to the uninformed and the patron saint to the enlightened. Depending on how much of the documentation you’ve read, it can either be a royal headache or an indispensable asset. But it exists to to get all your gems to play nicely together.
You can see which gems are installed in your
.bundler folder in Rails. This also contains some information about bundler and the gems installed. Note that it’s okay to delete .bundler, and in many cases this will fix problems. To ever re-install gems again and recreate .bundler, simply call out the battle-cry of Railslandia:
Note: running this command is always necessary after new gems have been added to the Gemfile, and running this never has any possible repercussions; feel free to run this whenever it may fix a problem.
The Gemfile language
Now that we’ve confirmed which gems are in our Rails app, let’s inspect our
Gemfile. You’ll find it in your root directory. My default Gemfile looks like this:
You’ll notice a pattern here:
gem 'gemname', 'version'
With some comments strewn about. For your app, you can not only specify which gems to include, but also which versions of gems you want! Incredible!
Why Does This Matter?
Since you asked, old invisible friend of inner monologue, version numbers are more or less subjective. Meaning, any update could potentially break your app. So this is nice in helping you out there.
But it doesn’t just stop there: notice that for some of the gem versions, you have little helpers that give you some flexibility:
>= 2.0Greater-than 2.0 / greater-than or equal to 2.0
~> 2.0Greater than 2.0 AND less than 3.0 (having
~> 2.0.1would yield greater than 2.0.1 and less than 2.1.
Last point: any changes you make to the Gemfile will need to be coupled with a
bundle install prerogative in your app directory in Terminal. This will not only install any gems specified, but any dependencies they may have.
- Gems are only added to a Rails project if they’re in
bundle installupdates the gems in
- You can delete the
.bundlerdirectory if you need to (re-populate it with another
- Learn more about Bundler at bundler.io