3

I am trying to set versión ruby version in the vagrant user with rvm using the following script into the vagrant file:

 config.vm.provision "shell", inline: <<-SHELL
     sudo apt-get -y update
     sudo apt-get -y install git ruby libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
 nodejs
     #Install ruby environment
     curl -sSL https://rvm.io/mpapis.asc | gpg --import -
     curl -L https://get.rvm.io | bash -s stable
     sudo "source ~/.rvm/scripts/rvm"
     echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc
     sudo /usr/local/rvm/bin/rvm install 2.1.5
     rvm 2.1.5 --default
     sudo chown -R vagrant:vagrant /usr/local/rvm/gems/
     gem install middleman   
     git clone  XXXX
     SHELL
END   

Vagrant is returning me the following message:

 /tmp/vagrant-shell: line 10: rvm: command not found

But if i run the command with the full path it returns me the following message:

/usr/local/rvm/bin/rvm 2.1.5 --default

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for an example.

Is there anyway to execute the inline script in logging mode or other way to use rvm to set the default version?

Thanks :)

POLLOX
  • 302
  • 1
  • 4
  • 17
  • Possible duplicate of http://stackoverflow.com/questions/9336596/rvm-installation-not-working-rvm-is-not-a-function – axiopisty Jan 19 '15 at 20:18
  • It is useful link to understand why it doesn't work. But not is the same problem , the problem is how to launch vm with ssh login into a Vagrantfile. – POLLOX Jan 23 '15 at 12:29

1 Answers1

6

The alterations to the script provided would prevent the code above from exiting with an bad exit status.

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"

  config.vm.provision "shell", inline: <<-SHELL
    RUBY_VERSION="2.1.5"
    sudo apt-get -y update
    sudo apt-get -y install git nodejs
    # Install ruby environment
    if ! type rvm >/dev/null 2>&1; then
      curl -sSL https://rvm.io/mpapis.asc | gpg --import -
      curl -L https://get.rvm.io | bash -s stable
      source /etc/profile.d/rvm.sh
    fi

    if ! rvm list rubies ruby | grep ruby-${RUBY_VERSION}; then
      rvm install ${RUBY_VERSION}
    fi

    rvm --default use ${RUBY_VERSION}
    rvm all do gem install middleman
    git clone <REPLACE_WITH_YOUR_REPO>
SHELL
end

Explanation of changes:

  1. There is no need to source "~/.rvm/scripts/rvm". Firstly, it fails because it doesn't exist as mentioned previously due to vagrant running as a non-login shell. Secondly the installation of rvm creates startup files in /etc/profile.d/rvm.sh which handles this case for you. See https://rvm.io/integration/gnome-terminal. You will still see a warning (in red) in the vagrant's console output but the script doesn't fail because of a bad exit status. As a result we need to source /etc/profile.d/rvm.sh immediately after installing RVM because our current shell hasn't loaded rvm.sh yet. This was mentioned in the RVM installation output. An alternative is to break up the script into multiple parts to force trailing scripts to pick up the new path.
  2. As vagrant is running as a non-login shell there is no need to change ownership of the /usr/local/rvm/gems/ folder. I think at some point vagrant did run scripts as the vagrant user but this has changed in more recent versions. Scripts will actually run as root; this occurs by default due to the privileged option on shell scripts; see Shell Provisioner. If you are uncertain which user is running you can do a whoami in the script. The script will run as the vagrant user if you de-escalate the privileges by setting privileged => false.
  3. Added a conditional block around the rvm GPG key and installation. Unless you need rvm installed on every provision. An argument could be made that you might be trying to keep it up-to-date but that could potentially introduce unknowns and break the repeatable results from one day to the next.
  4. Added a conditional block around the installation of the ruby version. This prevents the warning regarding the package is already installed, use reinstall.
  5. Cleaned up the packages you were installing. You might re-look at this but the packages you were installing with apt-get will be automagically installed by the rvm installer and add extra fluff to your scripts.

Alternative and bit more flexible method

If you aren't stuck on using inline scripts. I would go the route as described in Using RVM with Vagrant. Some of the suggestions I made above I would re-apply to the general logical of the scripts in this article. One change that would be REQUIRED is in the install-rvm.sh script. Add the GPG key import; shown below:

#!/usr/bin/env bash

curl -sSL https://rvm.io/mpapis.asc | gpg --import -
curl -sSL https://get.rvm.io | bash -s $1
Ryan Skoblenick
  • 464
  • 5
  • 7