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:

The Gemfile

Ruby on Rails Gemfile — how to include / exclude gems in your Rails project

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

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:

bundle install

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:

source 'https://rubygems.org'

	# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
	gem 'rails', '4.0.2'

	# Use sqlite3 as the database for Active Record
	gem 'sqlite3'

	# Use SCSS for stylesheets
	gem 'sass-rails', '~> 4.0.0'

	# Use Uglifier as compressor for JavaScript assets
	gem 'uglifier', '>= 1.3.0'

	# Use CoffeeScript for .js.coffee assets and views
	gem 'coffee-rails', '~> 4.0.0'

	# See https://github.com/sstephenson/execjs#readme for more supported runtimes
	# gem 'therubyracer', platforms: :ruby

	# Use jquery as the JavaScript library
	gem 'jquery-rails'

	# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
	gem 'turbolinks'

	# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
	gem 'jbuilder', '~> 1.2'

	group :doc do
		# bundle exec rake doc:rails generates the API under doc/api.
		gem 'sdoc', require: false
	end

	# Use ActiveModel has_secure_password
	# gem 'bcrypt-ruby', '~> 3.1.2'

	# Use unicorn as the app server
	# gem 'unicorn'

	# Use Capistrano for deployment
	# gem 'capistrano', group: :development

	# Use debugger
	# gem 'debugger', group: [:development, :test]
	

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.0 / >= 2.0 Greater-than 2.0 / greater-than or equal to 2.0
  • ~> 2.0 Greater than 2.0 AND less than 3.0 (having ~> 2.0.1 would 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.

TL;DR

  • Gems are only added to a Rails project if they’re in /Gemfile
  • bundle install updates the gems in .bundler/.
  • You can delete the .bundler directory if you need to (re-populate it with another bundle install
  • Learn more about Bundler at bundler.io
Drew Powers is a frontend developer at Envy.