Olivier Lacan
Olivier Lacan

Reputation: 2639

Ruby script shebang with rbenv and uninstalled project-required version

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?

Upvotes: 3

Views: 1402

Answers (1)

AL the X
AL the X

Reputation: 1051

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.

Upvotes: 1

Related Questions