Run "rvm alias create default 2.0" command? - ruby

I have a Chef recipe which installs RVM and Ruby. in the recipe I am using the execute resource to run the following command:
rvm alias create default 2.0
but this does not set my default Ruby version to 2.0. If I run this command directly on the host it does work because when I type ruby -v the version is set to 2.0 but when I try to run it through the Chef recipe ruby -v still returns 1.8.
This is my resource declaration:
execute "set_ruby_to_version_#{version}" do
command "rvm alias create default #{version}"
action :run
end
How come it doesn't work through the Chef recipe? I know that there is a cookbook available to deal with RVM but I still want to know why this is not working.

since rvm 1.25 rvm does not create wrappers in $rvm_path/bin if you depend on this wrappers (it's already in your PATH) then run this after creating alias:
rvm wrapper default --no-prefix
if you need a wrapper for specific gem add it's name after the command, ex:
rvm wrapper default --no-prefix haml
it will create only this wrapper: $rvm_path/bin/haml
other approach would be adding the automatically generated wrappers to PATH:
PATH=$rvm_path/gems/ruby-2.1.0/wrappers:$PATH

rvm alias create default 2.1.2
rvm use 2.1.2
Works for me.

Related

Issue with creating a gemset via Chef - .ruby-gemset and .ruby-version not getting created

I have included rvm --create --ruby-version ruby-2.3.3#storesview in my recipe to create a gemset. Running the kitchen converge does not result in the creation of .ruby-gemset and .ruby-version. But when I run rvm --create --ruby-version ruby-2.3.3#storesview directly on the VM terminal, the files are getting created. What could be the reason? Thanks.
Here's what my bash code looks like :
bash "somename" do
user 'root'
cwd "/var/www/html/APIStoresView/"
code <<-EOH
rvm --create --ruby-version ruby-2.3.3#storesview
EOH
end
In general use of rvm on servers should be discouraged, and Chef+rvm is dicey at the best of times. More specifically, appbundler (used by Chef to create marginally bullet proof binary stubs) sets a lot of environment variables that can confuse rvm.
If you need a specific version of Ruby for a project, I would highly recommend using poise-ruby+poise-ruby-build and Bundler rather than rvm and gemsets.

Using RVM via bash script

I wanted to write a small bash script to automatically setup the correct ruby version and gemset for a new project. For testing I have rvm installed with ruby 2.3.1 and two gemsets: foo and bar.
Even a simple test like this doesn't return what expected:
$ rvm current
ruby-2.3.1#foo
$ ./script
Using ruby-2.3.1 with gemset bar
Using /(...)/.rvm/gems/ruby-2.3.1 with gemset bar
ruby-2.3.1#bar
$ rvm current
ruby-2.3.1#foo
Contents of the script:
#!/bin/bash --login
rvm gemset use bar
rvm use 2.3.1#bar
rvm current
Other operations like installing rubies or creating gemsets seem to work, I just can't get use to modify the current version used by the script caller. Any ideas?
The answer was obviously simple, I should have called the script using
. ./script

How do I use 'rvm use' with puppet?

I'm trying to use Puppet to set up RVM on a variety of systems. Everything works fine until I try to specify which Ruby to use.
Running rvm use 1.9.3 with a Puppet exec yields an error, because 'rvm is not a function', since Puppet's exec forces all commands to be fully qualified.
How would I use Puppet to set the system Ruby through RVM? Is this even possible?
When you install rvm you need to source rvm.sh in order to get it working right away. The exact path to this file is usually disclosed in the installation messages.
You are getting good error message, it tells you that RVM can not be used interactively. This means even if RVM ignored the problem and set current ruby it would make no sense because running RVM as binary is a separate execution of shell which will not be able to set parent process (the shell / puppet) environment. To be able to set environment RVM has to be loaded as function in shell so it can change environment of current process.
So there are few ways to make it work:
subshell with multiple commands:
bash -c "source ~/.rvm/scripts/rvm ; rvm ..."
RVM set operation:
~/.rvm/bin/rvm {ruby-name} do {command}...
Some operations do not require above tricks (like setting default ruby):
~/.rvm/bin/rvm alias create default {ruby-name}
An extra explanation - default ruby is not a system ruby, it is a ruby that will be loaded when you source RVM, if you aim for availability of ruby in multiple places use alias and wrappers:
rvm alias create {my_app} {ruby-version}
rvm wrapp {ruby-version} --no-links --all
PATH=~/.rvm/environments/{my_app}:$PATH
This will create:
an alias - so it is easy to reference application ruby and no changes are needed in scripts to change ruby - just update alias
create wrappers for all gems installed in that ruby - includes wrappers for ruby and gem commands
add the PATH=... on top of any script that should work with ruby for your application.

How to use Bundler with path specified Ruby version

I am on a VM (Lucid 64b) with a system Ruby version of 1.9.3p0.
I have a Ruby script that creates a .deb file -- The script needs to use Ruby 1.8.7 which I have installed in /foo/ruby/1.8.7.
There is an existing Gemfile to be used with Bundler
I can't use RVM and I can't install gems at the system level.
My .bashrc includes (and has been sourced)
export PATH=$PATH:/foo/ruby/1.8.7/bin
but ruby -v still gives me
ruby 1.9.3p0 (2011-10-30) [x86_64-linux]
Questions
How can I change the Ruby version for my user to use Ruby 1.8.7?
I've run: bundle install --path vendor/bundle
So in that directory (actually ./vendor/bundle/ruby/1.8/cache/gems) are all the gems I need but, when I run the Ruby script it doesn't find the required gems. I run the script like so /foo/ruby/1.8.7 script_to_gen_deb_file.rb
How can I get ruby to see/use the bundled gems?
Update
I was able to solve it. I needed to use
/foo/ruby1.8.7/bundle exec /foo/ruby1.8.7/ruby script_to_gen_deb_file.rb
I had tried this before, but I got an unrelated error and believed there was an environment problem.
Change your path so the special ruby gets precedence?
export PATH=/foo/ruby/1.8.7/bin:$PATH

RVM: Create a gemset for whatever ruby is currently running

I want to use per project gemsets. But I don't want to specify the ruby version.
Something like:
#.rvmrc
rvm --create use "#project"
But this give me the following error:
error: Unknown ruby interpreter string component: 'ruby 1.9.2 p136'
info: Now using system ruby.
Is there a way to do what i want?
I'm not sure if that's the case but perhaps
rvm --create use doesn't support params without ruby version.
#.rvmrc
rvm gemset create "project"
rvm gemset use "project"
I didn't check this, just saw at:
http://rvm.beginrescueend.com/gemsets/creating/

Resources