Bundler helps Ruby projects manage and install the right versions of libraries they need. It makes sure your project works the same on any computer.
0
0
Bundler for dependency resolution in Ruby
Introduction
When you start a new Ruby project and want to use external libraries safely.
When you want to share your project with others and ensure they use the same library versions.
When you need to update or add libraries without breaking your project.
When you want to keep your project organized with a list of all libraries it uses.
Syntax
Ruby
source 'https://rubygems.org' gem 'gem_name', '~> version_number'
The source line tells Bundler where to get gems (libraries).
The gem line lists a library and its version rules.
Examples
This example sets Rails version 7.0.x as the library to use.
Ruby
source 'https://rubygems.org' gem 'rails', '~> 7.0.0'
This example uses Nokogiri gem with versions from 1.12 up to but not including 2.0.
Ruby
source 'https://rubygems.org' gem 'nokogiri', '>= 1.12', '< 2.0'
Sample Program
This example shows a Gemfile that uses the 'colorize' gem to print colored text. After running bundle install, the example Ruby file uses the gem to print green text.
Ruby
# Gemfile source 'https://rubygems.org' gem 'colorize', '~> 0.8.1' # Run in terminal: # bundle install # example.rb require 'colorize' puts 'Hello, Bundler!'.colorize(:green)
OutputSuccess
Important Notes
Always run bundle install after changing the Gemfile to update your libraries.
Bundler creates a Gemfile.lock file to lock exact versions for consistency.
You can run your Ruby program with bundle exec ruby example.rb to use the bundled gems.
Summary
Bundler manages Ruby project libraries and their versions.
It uses a Gemfile to list needed gems and their versions.
Running bundle install installs and locks these gems for your project.