How to use Gems, Gemfiles, and the Bundler in Shopify

Gem, Gemfile and the Bundler are those terms which are often used in the Ruby community but do you know exactly what they are and how to use them?

However, please keep reading our instructional writing on How to use Gems, Gemfiles, and the Bundler to know more deeply about the meanings of those mentioned terms and ways take advantages.

How to use Gems, Gemfiles, and the Bundler

What is a Gem? {#what-is-a-gem-?}

General speaking, a Gem is a bundle of codes that we can include in Ruby projects. This gives you permission to take anyone else’s code and drop it into your project. Gems are able to perform functionalities such as:

  • Pagination
  • Interact with APIs such as Github
  • Converting a Ruby object to JSON

Every gem has a name, a platform, and a version. For instance, the rake gem as a 0.8.7 version. Additionally, the platform of Rake is Ruby, that is to say, it works on any platform that Ruby runs on.

Inside gems are some components given below:

  • Code
  • Gemspec
  • Documentation

Every individual of gems do follow the same standard structure of code organization:

% tree freewill
freewill/
├── bin/
│   └── freewill
├── lib/
│   └── freewill.rb
├── test/
│   └── test_freewill.rb
├── README
├── Rakefile
└── freewill.gemspec

There is another thing that you may need to know: Jekyll is a Gem itself as many other Jekyll plugins such as jekyll-feed, jekyll-archives, and jekyll-seo-tag

What is a Gemfile? {#what-is-a-gemfile-?}

A Gemfile is a file created for the use of describing gem dependencies for Ruby programs. That is to say, A Gemfile is the dependency management system of Ruby or is a list of Gems that a Ruby project needs to run. It is necessary to note that your Gemfile is evaluated as Ruby code. When we have Jekyll plugins when need to use Gemfiles on Jekyll sites.

How is the look of a Gemfile? {#how-is-the-look-of-a-gemfile-?}

Firstly, please use the jekyll-feed and jekyll-seo-tag plugins to create a Gemfile for a Jekyll site.

A Gemfile needs at least one source that can tell you where to download the Gems.

The next step to do is to specify the Gems we’re using. If you want a specific version, you can include a version number. It is significant to always have Jekyll in our Gemfile.

In order to let Jekyll know about our plugin Gems in _config.yml, we would have to specify them. You are able to avoid this by putting Gems in a “jekyll_plugins” group that Jekyll automatically includes.

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 to use it? {#what-is-the-bundler-and-how-to-use-it-?}

The bundler is the program that can read the Gemfile and download the Gems. Please kindly note that Bundler is itself installed as a GemPlease run

gem install bundler

to get the bundler installed.

We also need to run bundle install to create or to change a Gemfile. This can perform 2 tasks:

  • Create a Gemffile.lock file if it is not available yet. This file is automatically generated and includes all the Gems in Gemffile with a version number even when it was not specified. This is to make sure that those people we have shared the source code will have the same version of the Gems.
  • Download the Gems in Gemfile.lock.

As usual, when we rụn Jekyll we tend to do something like this:

jekyll serve

When using a Gemfile, we need to run Jekyll in a different way. We may have various versions of the jekyll-feed plugin on the machine and if we run jekyll serve, maybe it could run the wrong version. Fortunately, we can handle this using bundle exec which makes only the Gems in the Gemfile available.

For instance, if you want to run jekyll serve, you shoulnd run:

bundle exec jekyll serve

Using Gemfiles and the bundler could make handling various versions of plugins much easier and you can be certain that you can have a consistent environment for your site across multiple machines.

Conclusion

Thanks for reading this instruction! For more information, please look at some more of our available related posts.