Should Gemfile.lock be included in .gitignore? Should Gemfile.lock be included in .gitignore? ruby ruby

Should Gemfile.lock be included in .gitignore?


Assuming you're not writing a rubygem, Gemfile.lock should be in your repository. It's used as a snapshot of all your required gems and their dependencies. This way bundler doesn't have to recalculate all the gem dependencies each time you deploy, etc.

From cowboycoded's comment below:

If you are working on a gem, then DO NOT check in your Gemfile.lock. If you are working on a Rails app, then DO check in your Gemfile.lock.

Here's a nice article explaining what the lock file is.


The real problem happens when you are working on an open-source Rails app that needs to have a configurable database adapter. I'm developing the Rails 3 branch of Fat Free CRM. My preference is postgres, but we want the default database to be mysql2.

In this case, Gemfile.lock still needs be checked in with the default set of gems, but I need to ignore changes that I have made to it on my machine. To accomplish this, I run:

git update-index --assume-unchanged Gemfile.lock

and to reverse:

git update-index --no-assume-unchanged Gemfile.lock

It is also useful to include something like the following code in your Gemfile. This loads the appropriate database adapter gem, based on your database.yml.

# Loads the database adapter gem based on config/database.yml (Default: mysql2)# -----------------------------------------------------------------------------db_gems = {"mysql2"     => ["mysql2", ">= 0.2.6"],           "postgresql" => ["pg",     ">= 0.9.0"],           "sqlite3"    => ["sqlite3"]}adapter = if File.exists?(db_config = File.join(File.dirname(__FILE__),"config","database.yml"))  db = YAML.load_file(db_config)  # Fetch the first configured adapter from config/database.yml  (db["production"] || db["development"] || db["test"])["adapter"]else  "mysql2"endgem *db_gems[adapter]# -----------------------------------------------------------------------------

I can't say if this is an established best practice or not, but it works well for me.


My workmates and I have different Gemfile.lock, because we use different platforms, windows and mac, and our server is linux.

We decide to remove Gemfile.lock in repo and create Gemfile.lock.server in git repo, just like database.yml. Then before deploy it on server, we copy Gemfile.lock.server to Gemfile.lock on server using cap deploy hook