Understanding Ruby Gemfiles

Corey Gardner
2 min readMar 29, 2022

A Gemfile is a format for understanding the dependencies of a Ruby project. Using dependencies allows developers to build on top of the work of other developers, usually in the form of open source projects. In ruby these dependencies are called Gems.

The Gemfile is placed in the root of our project directory, where it will be evaluated as Ruby code. The first line of code in our Gemfile tells our project which repository it should grab the gems from and the second line specifies the Ruby version that we’re using. Usually you’ll see something like this:

source "https://rubygems.org"
ruby "3.0.0"

To import a gem we use the Gem keyword followed by the gems name followed by its version. Versioning Gems is optional and when you don’t specify a version the latest version will be installed. If you do choose to manually manage Gem versions be sure to use a tool like Dependabot to automatically detect vulnerability updates.

gem 'devise' # latest version 
gem 'devise', '~> 4.2' # Newer than 4.2 but not 4.3
gem devise', '>= 4.2' # newer or equal to 4.2

Separate environments are used to modify an application in different context. Traditionally their are four separate environments development, test, staging and production. If I wanted to use SQLite in production and PostGres in test I would do something like:

group :production do
gem 'sqllite3'
end
group :test do
gem 'pg'
end

Once our Gemfile is configured we can install them with bundler by running bundle install. Now our project can utilize leverage with Free Software!

Thanks for reading & be sure to checkout Corey’s Corner, a podcast about life.

--

--