3

I have a script in a Ruby application at bin/setup that begins with:

#!/usr/bin/env ruby

env tells what executables are accessible, and if there is a Ruby version available on $PATH, the script should use it to execute.

Suppose I am using rbenv and I have a global Ruby version set (either system which falls back to /usr/bin/ruby on OS X) or something else. system or my global Ruby version is different from the .ruby-version declared in the direction within which this bin/setup script is being executed. The .ruby-version file contains 2.1.6. This causes the script to instantly abort with:

rbenv: version `2.1.6' is not installed (set by /Users/olivierlacan/project/.ruby-version)

This means there is no way to rely on env to dynamically fetch the appropriate version of Ruby this bin/setup script should be executed with.

The only alternative I found is:

#!/usr/bin/ruby

But this is not portable to any Linux or Windows OS that doesn't have Ruby installed in /usr/bin/ like OS X. Is there a way around this?

sawa
  • 165,429
  • 45
  • 277
  • 381
Olivier Lacan
  • 2,636
  • 2
  • 24
  • 22
  • 2
    I don't understand what is the problem here. It seems that everything is working as designed. Your project is configured to use Ruby 2.1.6 which isn't installed. Thus, any attempt to run Ruby scripts from that project will fail. If rbenv allowed a different Ruby version to be used here other than the one specified in `.ruby-version` file, then rbenv would be broken and not doing its job. – mislav Dec 24 '15 at 22:23

1 Answers1

1

Looks like there's a similar issue in rbenv when installing a gem in a specific Ruby version and also globally, due to the way that rbenv-which resolves commands.

The rbenv-which-ext extension by GitHub user @yyuu, the maintainer of the Python fork pyenv, solves at least that problem. An additional patch might be necessary to treat ruby as a command in the same way, but it's worth a try. I'll attempt to recreate your problem locally and do the same to verify.

AL the X
  • 1,004
  • 11
  • 15