I would like to specify which ruby interpreter and which gemset to use inside the header of my ruby script. Something along the lines of
#!/usr/bin/env source /usr/local/lib/rvm && rvm ruby-1.9.2@system
would be great.
Any suggestion on how to do this?
Step 1: rvm gemset create [name of gemset] Step 2: rvm --rvmrc [ruby version here]@[name of gemset] # Note: You can check your current ruby version by running "ruby -v" from your console. Step 3: Refresh your directory. # You can simply do this by checking out of your directory and going back to that directory again.
RVM gives you compartmentalized independent ruby setups. This means that ruby, gems and irb are all separate and self-contained - from the system, and from each other. You may even have separate named gemsets. Let's say, for example, that you are testing two versions of a gem, with ruby 2.1.
Gemsets are little libraries for individual projects, where each gem used in the project is stored. You tell Ruby which gems you need for a project, and it stores them in a gemset so you can quickly switch project to project and have all the gems you need (and only the gems you need for each project).
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/ruby
lrwxrwxrwx 1 root rvm 44 Mar 13 06:42 ruby-1.8.7-p334 -> /usr/local/rvm/wrappers/ruby-1.8.7-p334/ruby
lrwxrwxrwx 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-p180
puts "Content-Type: text/html"
puts
puts "<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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With