Search code examples
rubyrubygemsbundlerrbenv

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.


Solution

  • 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