Should Gemfile.lock be included in .gitignore?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Whether to include Gemfile.lock in .gitignore depends on whether your project is an application or a library. Applications should always commit Gemfile.lock to ensure every developer and deployment environment uses the exact same gem versions. Libraries (gems) should gitignore it so that consumers resolve their own compatible dependency versions. This is one of the most commonly misunderstood decisions in Ruby dependency management.
Applications: Commit Gemfile.lock
For Rails apps, Sinatra apps, or any deployable application, always commit Gemfile.lock:
Gemfile.lock records the exact version of every gem (including transitive dependencies) that was resolved. This ensures:
- Every developer runs the same gem versions
- CI builds are deterministic
- Production deployments match what was tested
bundle installis faster (it skips resolution and installs locked versions)
Libraries/Gems: Gitignore Gemfile.lock
For gems and libraries, add Gemfile.lock to .gitignore:
Why? A gem's consumers will resolve dependencies based on the .gemspec version constraints, not a lock file. Committing Gemfile.lock in a gem creates problems:
If the gem commits a lock file pinning activesupport 7.0.4, contributors who run bundle install get that exact version instead of testing against the full supported range. This hides compatibility issues.
The Official Bundler Recommendation
From the Bundler documentation:
- Applications: Check in
Gemfile.lock - Gems: Do not check in
Gemfile.lock
What Gemfile.lock Contains
The lock file records:
- Exact version of every gem resolved
- Platform-specific gem selections
- Which Bundler version generated it
Updating Gemfile.lock
Platform Differences
Common Pitfalls
- Gitignoring Gemfile.lock in an application: This causes non-deterministic builds. Two developers may get different gem versions, leading to "works on my machine" bugs. Every application should commit its lock file.
- Committing Gemfile.lock in a gem: This pins development dependencies unnecessarily and hides compatibility issues with the version ranges declared in the
.gemspec. Let CI test across multiple dependency versions instead. - Running
bundle updatewithout a specific gem:bundle update(with no arguments) re-resolves all gems and can introduce breaking changes. Always specify the gem name (bundle update rails) to limit the scope of changes. - Merge conflicts in Gemfile.lock: Never manually resolve Gemfile.lock conflicts. Instead, pick one side, then run
bundle installto regenerate a valid lock file. - Missing platforms in lock file: If you develop on macOS but deploy to Linux, the lock file may not include Linux-specific gem versions. Run
bundle lock --add-platform x86_64-linuxto include them.
Summary
- Applications (Rails, Sinatra, etc.): Always commit
Gemfile.lockfor deterministic builds - Libraries/Gems: Add
Gemfile.lockto.gitignoreso consumers resolve their own versions Gemfile.lockrecords exact resolved versions of all gems including transitive dependencies- Use
bundle update <gem>to update specific gems, notbundle updatewithout arguments - Add deployment platforms with
bundle lock --add-platformto avoid cross-platform issues - Never manually edit
Gemfile.lock— let Bundler manage it withbundle installorbundle update
Related reading
- Should I add the Visual Studio 2015 .vs folder to source control?
- Should I be adding the Django migration files in the .gitignore file?
- Should I check in folder node_modules to Git when creating a Node.js app on Heroku?
- Should I git ignore xcodeproject/project.pbxproj file?
- Should I use past or present tense in git commit messages?
- Should I use SVN or Git?
- Should the mvnw files be added to the repository?
- Should you commit .gitignore into the Git repos?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.