select rvm gemset in script header select rvm gemset in script header shell shell

select rvm gemset in script header


tcurdt's post is a little misleading. On my system I just set up rvm (ree, 1.8.7, & 1.9.2 rubies) on the system as root. My /usr/local/bin looked like this:

lrwxrwxrwx 1 root rvm   46 Mar 13 06:50 ree-1.8.7-2011.03 -> /usr/local/rvm/wrappers/ree-1.8.7-2011.03/rubylrwxrwxrwx 1 root rvm   44 Mar 13 06:42 ruby-1.8.7-p334 -> /usr/local/rvm/wrappers/ruby-1.8.7-p334/rubylrwxrwxrwx 1 root rvm   44 Mar 11 22:42 ruby-1.9.2-p180 -> /usr/local/rvm/wrappers/ruby-1.9.2-p180/ruby

I didn't have a gemset called system as in tcurdt's example. So I believe the appropriate way to achieve what you're after would be like so:

#!/usr/bin/env /usr/local/bin/ruby-1.9.2-p180

Some explanation of what rvm is doing here: rvm creates wrappers scripts (/usr/local/rvm/wrappers/*) which set things like GEM_PATH & GEM_HOME. These are needed so that gems can get loaded correctly as part of a specific version of ruby. Links to these wrapper scripts are created under /usr/local/bin, when you do a system installation of rvm. If you've installed rvm as yourself they're located here: $HOME/.rvm/bin.

It's critical to prefix the shebang line (#!/....) with the /usr/bin/env. If you just try and point it directly to a ruby (for e.g. #!/usr/local/bin/ruby-1.9.2-p180) will not suffice. This is because these wrappers are not actually the ruby interpreter, they are shell scripts that are sourcing an environment setup prior to calling your script as an argument to the ruby interpreter, like so:

source "/usr/local/rvm/environments/ruby-1.9.2-p180" exec ruby "$@"

the $@ is your shell script name being passed to ruby.

Finally here's an example script that I put together which I run in a cgi-bin directory:

#!/usr/bin/env /usr/local/bin/ruby-1.9.2-p180puts "Content-Type: text/html"putsputs "<html>"puts "<body>"puts "<h1>Hello Ruby!</h1>"puts "<p>shebang: #!/usr/bin/env /usr/local/bin/ruby-1.9.2-p180</p>"puts "<p>RUBY_VERSION: " + RUBY_VERSION + "</p>"puts "<p>RUBY_PLATFORM: " + RUBY_PLATFORM + "</p>"puts "<p>RUBY_RELEASE_DATE: " + RUBY_RELEASE_DATE + "</p>"puts "</body>"puts "</html>"


#!/usr/bin/env rvm 1.9.3@mygemset do ruby


This is the way to do it...

#!/usr/bin/env /usr/local/bin/ruby-1.9.2-p180@system