Can I rebuild a Gemfile from a Gemfile.lock file?

1.2k Views Asked by At

I would like to replicate an older app, but bundle install fails since Gemfile became outdated and does not catch compatible versions anymore. I guess I can try correct Gemfile by err and trial, yet can I somehow use Gemfile.lock automatically generated in another older and working installation for information about compatible versions?

Is there way to generate a Gemfile (with precise versions) from the Gemfile.lock or update the existing one? It looks like this file uses a different syntax from a Gemfile.

Update. I did not find any script to build Gemfile from Gemfile.lock, however after I copied Gemfile.lock from a working installation build install worked

1

There are 1 best solutions below

0
On

Use the Source, Luke!

If you have source control, look back into your history to find your Gemfile or the gemspec for your application or gem. If you just have a Gemfile.lock, then you should be able to build the application with bundle install as long as Bundler has valid gem sources it can access.

You can also grep your source code for the require keyword, and at least identify what modules you're looking for. You can also look at the top-level items in your Gemfile.lock to see what was in your Gemfile or gemspec when the lockfile was built.

As an example, part of the Gemfile.lock for Sinatra lists the following dependencies:

DEPENDENCIES
  activesupport (~> 5.1.6)
  json
  minitest (~> 5.0)
  nokogiri (!= 1.5.0)
  puma
  rack!
  rack-protection!
  rack-test (>= 0.6.2)
  rake
  sinatra!
  sinatra-contrib!
  twitter-text (= 1.14.7)
  yard

The other sections have to do with sources, constraints, and other dependencies that get pulled in as a result of the core dependencies. Everything you need to rebuild a Gemfile is there, but there's no tool that I know of that will do it for you since a Gemfile.lock is built from a Gemfile, not the other way around.

While there's no easy way to reverse-engineer a Gemfile.lock into a Gemfile or gemspec, you can certainly build one from the top-level entries under the specs: keys, as well as the DEPENDENCIES key.

See Also