In Rails, Bundler is a dependency management tool that ensures your application has the correct versions of the required gems. A gem in Ruby is a packaged Ruby library that provides functionality for Ruby applications. Gems are hosted on RubyGems.org, which is the official Ruby package repository.
The main role of Bundler is to manage and install the gem dependencies specified in the Gemfile.
When a new application is created, Rails does the following behind the scenes:
- Generates the Gemfile with default gems.
- Runs bundle install automatically, which:
- Resolves dependencies.
- Installs required gems.
- Creates the Gemfile.lock file to lock exact gem versions.
Gemfile
When you create a new Rails application and run rails server, Rails internally calls Bundler to load the required gems. The rails new command sets up the Rails project and generates various files, including Gemfile.
Here’s a section of the Gemfile created automatically by Rails new command:
source "https://rubygems.org"# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" gem "rails","~> 8.0.1"# The modern asset pipeline for Rails [https://github.com/rails/propshaft] gem "propshaft"# Use sqlite3 as the database for Active Record gem "sqlite3",">= 2.1"# Use the Puma web server [https://github.com/puma/puma] gem "puma",">= 5.0"# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails] gem "importmap-rails"# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev] gem "turbo-rails"# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev] gem "stimulus-rails"# Build JSON APIs with ease [https://github.com/rails/jbuilder] gem "jbuilder"
It also runs bundle install, which resolves dependencies and creates Gemfile.lock.
Gemfile.lock
This file is auto-generated by Bundler when bundle install is run. It locks the exact versions of the installed gems to ensure consistency. For example −
GEM remote: https://rubygems.org/ specs:actioncable (8.0.1) actionpack (=8.0.1) activesupport (=8.0.1) nio4r (~>2.0) websocket-driver (>=0.6.1) zeitwerk (~>2.6) actionmailbox (8.0.1) actionpack (=8.0.1) activejob (=8.0.1) activerecord (=8.0.1) activestorage (=8.0.1) activesupport (=8.0.1) mail (>=2.8.0)</pre>
However, you need to run bundle install manually if you manually modify Gemfile (e.g., adding a new gem).
If you pull the project from GitHub, you have to run bundle install since Gemfile.lock might not match your system. Sometimes, a required gem is missing when running rails server. You need to add a new gem to a Rails application when you require additional functionality that is not provided by the Rails framework itself.
To add a New Gem, you can add it directly using Bundler −
bundle add gem_nameOr manually, add it to Gemfile −
gem 'gem_name'Then run:
bundle installCommon Bundler commands are as follows:
- bundle install − Installs all gems listed in Gemfile.
- bundle update − Updates gems to the latest versions allowed by the Gemfile.
- bundle exec <command> − Runs a command using gems from Bundler (e.g., bundle exec rails server).
- bundle add <gem_name> − Adds a gem to the Gemfile and installs it.
- bundle remove <gem_name> − Removes a gem from the Gemfile.
Leave a Reply