DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Rails 6: Multiple DB Support

Trending

  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  • Agile and Quality Engineering: A Holistic Perspective
  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • FIPS 140-3: The Security Standard That Protects Our Federal Data

Rails Bundle Install and Gemfile

Looking to get a better handle on the Ruby on Rails framework? Read on to learn about these two foundational building blocks.

By 
Kristina Garcia Francisco user avatar
Kristina Garcia Francisco
·
Feb. 23, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
39.0K Views

Join the DZone community and get the full member experience.

Join For Free

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 production stages.

Setting Up a Bundler

With these three simple steps that every beginner 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:
  3. gem "carrierwave", "~> 1.0"
    gem "devise"
    gem "pundit"
  4. 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

The 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: '[email protected]/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!

GEM (desktop environment)

Published at DZone with permission of Kristina Garcia Francisco, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Rails 6: Multiple DB Support

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: