Rails Bundle Install and Gemfile

Rails Bundle Install and Gemfile

Bundler makes sure that Ruby applications always use the exact gems and versions that you need while keeping a consistent environment and gem dependencies satisfied. This is done by ensuring that the gems you need are present in the development, staging and the production.

Setting up a Bundler

With this three simple steps that every begginer should know to get your application up and running with bundler:

  1. Install (or update) bundler with the following command: $gem install bundler
  2. Specify your dependencies in a Gemfile:
gem "carrierwave", "~> 1.0"
gem "devise"
gem "pundit"
  1. Install all the gems Rails will need to get started: $bundle install

Bundle Install

Before you start, you will need to install Ruby on Rails on your machine. Once the installation is finished run bundle install, this will create Gemfile.lock in your repository. This ensures that other developers on your Ruby application run the same third-party code on every machine.

Options

There are multiple options you can use to change the way bundle install works. We will cover the most commonly used ones.

bundle install [--deployment][--force][--frozen][--path PATH]
               [--standalone[=GROUP[ GROUP...]]]
               [--without=GROUP[ GROUP...]][--with=GROUP[ GROUP...]]
  • --deployment: Runs bundle in the production environment. Activating deployment mode on development machines will cause an error when the Gemfile is modified.
  • --force: Downloads every gem, even if the required versions are already available.
  • --frozen: Gemfile.lock will not be updated after this install.
  • --path <path_to_folder>: The specific locations where gems are going to be installed.
  • --standalone <gem_list>: Creates a bundle that can work without depending on Bundler at runtime.
  • --without <gem_list>: Groups of gems to skip during installation.
  • --with <gem_list>: Groups of gems to install during installation.

Gemfile

A Gemfile is a file we created which is used for describing gem dependencies for Ruby programs. The Gemfile is located in the root of the project directory.

source 'https://rubygems.org'

gem 'rails', '4.2.6'
gem 'sqlite3'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc

group :development, :test do
  gem 'byebug'
end

group :development do
  gem 'web-console', '~> 2.0'
  gem 'spring'
end

Setting up the Gemfile

Using source will explain the Gemfile where to look for gems: source https://rubygems.org. There is no need for more than one source per project. git and path are also valid gem sources.

Bundler first looks if a source location is set up for the gem. If nothing has been explicitly set, it will look at the sources you have defined, starting at the first one.

If the application requires a specific Ruby version or engine we can set this in the Gemfile in the following way:
ruby '2.0.0', :patchlevel => '353', :engine => 'jruby', :engine_version => '1.6.7'

  • :patchlevel: Patch level for Ruby.
  • :engine: Type of Ruby engine.
  • :engine_version: Version of the Ruby engine.

Setting up the Gems

Gems default syntax is: gem 'gem_name'. You can improve the default syntax in the following way:

  • Setting versions: gem 'gem_name', '~> 1.3'
    If you don’t set a gem version, bundler will set it for you. To avoid that, you can use operators (= Equal To , != Not Equal To, > Greater Than, < Less Than, >= Greater Than or Equal To, <= Less Than or Equal To, ~> Pessimistically Greater Than or Equal To).

    The ~> operator ensures that your application will safely work with future versions of a gem.
    gem 'gem_name', '~> 1.0' is the same as: gem 'gem_name', '>= 1.0', '< 2.0'

  • Setting gem to required: gem 'gem_name', require: false
    In order to require gems in your Gemfile, you will need to call Bundler.require in your application. You can prevent bundler from requiring the gem with require: false, but it will still install and maintain the gem.

  • Adding gem source: gem 'gem_name', source: 'https://gem_site.com'
    You can add source to your gems using source: 'https://gem_site.com', a git repository git: 'ssh@githib.com/git_repository/gem_name', or a system path :path => '.../path_to_gem/gem_name'.

    When installing gems using git, you can set either a branch, tag, or ref for the gem. The default branch is branch: 'master'.
    gem 'gem_name', git: 'ssh@githib.com/git_repository/gem_name', branch: gem_branch

    Another gem source can be :gist, it is used if the project is hosted on Github as a gist. Gist ID is used as the path: gem 'gem_name', :gist => '8eb650c7065', branch: 'gem_branch'

  • Grouping gems: gem 'gem_name', group: :development
    Gems can belong to one or more groups. When it doesn’t belong to any groups it is located in the :default group.

    group :development, :test do
      gem 'gem_name'
      gem 'gem_another_name'
    end
    

    You can specify certain groups to not bundle install, this can considerably speed up the installation time. bundle install --without development test

  • Setting a platform: gem 'gem_name', platform: :jruby
    You can specify in the Gemfile if a gem is used on a specific platform (or set of platforms).
    When developers use different Operating Systems during development, they will need different versions of gems depending on what is supported by their system.

    platforms [:mingw, :mswin, :x64_mingw, :jruby] do
      gem "gem_name"
      gem "gem_another_name"
    end
    

Hope this text helped you to gain a better understanding of gems and bundle install. Thank you for reading!

Gems are precious but so is our newsletter! Be sure to subscribe to it!