Subscribe to Jekyll Casts
The terms gem, Gemfile and the bundler are often used in the Ruby community. So what exactly do they mean and how do we use them?
What is a gem?
A gem is are little bundles of code we can be included in our Ruby projects. This allows us to take someone else’s code and drop it straight into our own project. Gems can perform functionality such as:
- Converting a Ruby object to JSON
- Interacting with APIs such as Github
What is a Gemfile?
A Gemfile is Ruby’s dependency management system or in other words, a list of gems a Ruby project needs to run. We use Gemfiles on Jekyll sites when we have Jekyll plugins.
What does a Gemfile look like?
Let’s create a Gemfile for a Jekyll site using the jekyll-feed and jekyll-seo-tag plugins.
A Gemfile requires at least one source which tells us where to download the gems. Unless we have an advanced use case rubygems.org will be fine.
Next we specify the gems we’re using. We can include a version number if want a specific version. It’s important to always include Jekyll in our Gemfile.
Usually we’d have to also specify our plugin gems in
_config.yml so Jekyll knows about them. We can avoid this by putting our plugin gems in a “jekyll_plugins” group which Jekyll includes automatically.
source 'https://rubygems.org' gem 'jekyll', '3.1.6' group :jekyll_plugins do gem 'jekyll-feed' gem 'jekyll-seo-tag' end
What is the bundler and how do we use it?
The bundler is the program which reads the Gemfile and downloads the Gems. We can install the bundler by running:
gem install bundler
When we create or change a Gemfile, we need to run
bundle install which performs two tasks:
- Creates a
Gemfile.lockfile if it doesn’t exist. This file is auto-generated and includes all the gems in
Gemfilewith the addition of a version number even if it wasn’t specified. This ensures that other people we share the source code to will have the same version of the gems.
- Downloads the gems in
Usually when we run jekyll we’d do something like this:
When we’re using a Gemfile we need to run Jekyll slightly differently. We might have multiple versions of the jekyll-feed plugin on our machine and if we run
jekyll serve, it might use the wrong version. We can solve this using
bundle exec which makes only the gems in the Gemfile available. For example if we want to run
jekyll serve we’d run:
bundle exec jekyll serve
Using Gemfiles and the bundler makes dealing with different versions of plugins much easier and ensures we can have a consistent environment for our site across multiple machines.