0
0
Ruby on Railsframework~5 mins

Gemfile and dependency management in Ruby on Rails

Choose your learning style9 modes available
Introduction

A Gemfile helps you list all the extra tools (gems) your Rails app needs. It keeps your app organized and makes sure everyone uses the same versions.

When starting a new Rails project and you want to add extra features like authentication or testing.
When you want to share your project with others and ensure they install the same gems.
When updating or adding new gems to keep your app working smoothly.
When deploying your app to a server and you need to install all required gems automatically.
Syntax
Ruby on Rails
source 'https://rubygems.org'
gem 'gem_name', '~> version'

# Example:
gem 'rails', '~> 7.0.0'
gem 'puma', '~> 5.0'

The source line tells where to get gems from, usually RubyGems.org.

Each gem line names a gem and optionally its version.

Examples
This means use Rails version 7.0.x but not 7.1 or higher.
Ruby on Rails
gem 'rails', '~> 7.0.0'
This adds the Devise gem without specifying a version, so it uses the latest.
Ruby on Rails
gem 'devise'
This adds the RSpec gem only for development and test environments.
Ruby on Rails
group :development, :test do
  gem 'rspec-rails'
end
Sample Program

This Gemfile sets the source for gems, adds Rails and Puma gems for all environments, and adds RSpec only for development and test.

Ruby on Rails
source 'https://rubygems.org'

gem 'rails', '~> 7.0.0'
gem 'puma', '~> 5.0'

group :development, :test do
  gem 'rspec-rails'
end
OutputSuccess
Important Notes

Always run bundle install after changing the Gemfile to install or update gems.

Use bundle update gem_name to update a specific gem safely.

Lockfile (Gemfile.lock) keeps exact gem versions to ensure consistency across machines.

Summary

A Gemfile lists all extra tools (gems) your Rails app needs.

It helps keep everyone using the same gem versions for smooth teamwork.

Use groups to add gems only for certain environments like development or test.