5

...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.

Community
  • 1
  • 1
magni-
  • 1,934
  • 17
  • 20

2 Answers2

5

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
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
  • "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." Ah yeah, that's pretty nonideal. Ideally, I'd just need to do something in this project's directory without having to do anything anywhere else... – magni- Apr 26 '16 at 00:54
5

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
magni-
  • 1,934
  • 17
  • 20