Search code examples
rubyrubygemsbundlerrbenv

Bundle not working with rbenv and cannot gem install bundler


I was running into an issue with bundler when using rbenv. I had switched from ruby 2.3.1 to 2.5.1 a while ago, but I noticed the following error when trying to run bundler inside of a shell script in an ansible task.

``` rbenv: bundle: command not found

The `bundle' command exists in these Ruby versions:
  2.2.2
  2.3.1

```

I saw some solutions to this where you had to install bundler when on a specific ruby version. However even uninstalling bundler and reinstalling it did not resolve the issue. It seemed due to some gem issue that I could not install bundler on specific ruby versions, thus reinstalling bundler did nothing for me.


Solution

  • However I did find a very manual solution to this.

    I noticed how rbenv whence bundle specified that bundle was active in some ruby versions, but not others. I tracked down the portion of rbenv whence command to find out where it was looking and saw that it looked under the following path for each ruby version installed under rbenv: ~/.rbenv/versions/$VERSION/bin/

    If you look under the path for the gem without bundler (for me this was ~/.rbenv/versions/2.5.1/bin/) you will notice that it is not there. However under other versions that whence can recognise you will see the executable is there (for me it was there under ~/.rbenv/versions/2.3.1/bin/bundle and ~/.rbenv/versions/2.3.1/bin/bundle).

    So I copied the executable over from the working ruby version to the one that was broken. Then at the top of the file you will notice sha-bang is set to the other ruby version (for me this was #!/Users/myuser/.rbenv/versions/2.3.1/bin/ruby). Change the version path to the ruby version you want to fix and then it will all magically start working.