Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use an older version of Bundler for a Ruby project?

...without uninstalling the latest version.

I'm working on a project that uses Bundler 1.10, but I use Bundler 1.11 for all my other projects. Since this project is on 1.10, whenever I run any bundle commands, my Gemfile.lock changes due to the different formatting introduced by Bundler 1.11.

The only solution I've found so far is running all my commands like so:

bundle _1.10.6_ [command]

(from this answer here: https://stackoverflow.com/a/4373478/1612744)

Is there a better way? I'm using rbenv, not rvm.

like image 500
magni- Avatar asked Apr 25 '16 05:04

magni-


2 Answers

You could use the rbenv-gemset plugin. Since an older version of a gem in an rbenv gemset doesn't override a newer version in the default gemset, you'd need to install bundler for each project, which is rather cumbersome.

  • Install the plugin (cd ~/.rbenv/plugins; git clone git://github.com/jf/rbenv-gemset.git) OR, if you use homebrew, brew install rbenv-gemset
  • gem uninstall -xI bundler
  • For each project that can use bundler 1.11,
    • cd to the project
    • echo bundler-1.11 > .rbenv-gemsets ("bundler-1.11" is the name of the gemset)
  • In one of those projects, gem install bundler to get the current version, 1.11.*
  • For the project that needs bundler 1.10,
    • cd to the project
    • echo bundler-1.10 > .rbenv-gemsets ("bundler-1.10" is the name of the gemset)
    • gem install bundler -v 1.10.6
like image 76
Dave Schweisguth Avatar answered Nov 12 '22 12:11

Dave Schweisguth


I ended up solving this with an alias and a bash function. In my ~/.bashrc, I added this:

versioned_bundle() {
  version_file=${PWD}/.bundle/version
  if [[ -r $version_file ]]; then
    version=$(<$version_file)
    bundle _${version}_ "$@"
  else
    bundle "$@"
  fi
}
alias bundle=versioned_bundle

As you can guess from the code, I also added a version file to the .bundle/ directory in my project. That file's contents:

1.10.6

Whenever I run bundle, my shell checks for a version file in the current directory, and if it finds it, runs that version of bundle.

$ cd project-with-old-bundle-version
$ bundle --version
1.10.6
$ cd anywhere-else
$ bundle --version
1.11.2
like image 22
magni- Avatar answered Nov 12 '22 14:11

magni-