VagrantFile inline script and rvm provisioning - ruby

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 :)

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:
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.
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.
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.
Added a conditional block around the installation of the ruby version. This prevents the warning regarding the package is already installed, use reinstall.
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

Related

RVM is not a function Ubuntu/Mina/rvm

I've just setup an ubuntu server and need to deploy a small rails app. I'm using mina.
When I execute "mina setup", the following part of the code gives me a message(you can find it below the code) But the setup process finishes successfully.
deploy.rb
task :environment do
invoke :'rvm:use[ruby-2.2.2#default]'
end
-
$ rvm use "ruby-2.2.2#default" --create
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.
How can I get rid of this message?
I found a few similar topics but it didn't solve my problem.
here's how I installed rvm:
$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
$ curl -sSL https://get.rvm.io | sudo bash -s stable
$ sudo usermod -a -G rvm `whoami`
ps
For the setup process I used info from the following articles:
Passenger library
Setting Up Ruby on Rails On Digital Ocean
I noticed that deploy.rb from second article has .bash_profile. I dont have this file( only .profile)
task :environment do
queue 'source ~/.bash_profile'
# For those using RVM, use this to load an RVM version#gemset.
invoke :'rvm:use[ruby-2.1.4]'
end
This is most probabaly because rvm is still not loaded to your bash. Load it using:
source ~/.rvm/scripts/rvm
followed by:
rvm use "ruby-2.2.2#default" --create
A permanent fix would be to add following line in your ~/.bashrc :
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

setting up an EC2 server with rvm via scripting

I'm looking for a way to install rvm, install a specific ruby version (using rvm) and set this installed ruby version as default. Before I can install rvm I have to install gcc and some other very basic software packages. What I've tried so far:
1) Using net/ssh
I have to simulate a pseudo tty to be able to sudo some commands and up to now,
I could not figure out, how to tell a success full command completion from a not success full one.
after installing rvm, I've stumbled over problems using rvm ("rvm is not a function", error messages, leading to not being able to set a default ruby version).
2) Using capistrano
In the ssh output are newlines inserted so that a progress bar for example is been printed in a new line every time, some progress is made. That's something I can live with.
Same Problems with rmv, I'm able to install rvm, but I'm unable to set a default: rvm --default use 1.9.2 for example. No error message, but when I've log in later, no default is set and ruby -vshows the old system ruby.
3) Using capistrano and rvm-capistrano
Now I ran into the problem, that very task that I try to execute prior to installing rvm fails, because there seems to be some magic that fiddles with the shell default:
* executing "sudo -p 'sudo password: ' yum install --assumeyes git gcc-c++ autoconf automake make patch zlib-devel libtool bzip2-devel"
servers: ["ec2-54-247-142-214.eu-west-1.compute.amazonaws.com"]
[ec2-54-247-142-214.eu-west-1.compute.amazonaws.com] executing command
** [out :: ec2-54-247-142-214.eu-west-1.compute.amazonaws.com] bash: /home/ec2-user/.rvm/bin/rvm-shell: No such file or directory
command finished in 2094ms
failed: "rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell 'default' -c 'sudo -p '\\''sudo password: '\\'' yum install --assumeyes git gcc-c++ autoconf automake make patch zlib-devel libtool bzip2-devel'" on ec2-54-247-142-214.eu-west-1.compute.amazonaws.com
rake aborted!
Here the commands I issue to install rvm/ruby:
run 'curl -L https://get.rvm.io | bash -s stable'
run 'rvm install ruby-1.9.2-p320'
run 'echo "[[ -s \"\$HOME/.rvm/scripts/rvm\" ]] && source \"\$HOME/.rvm/scripts/rvm\"" >> .bashrc'
run 'rvm --default use ruby-1.9.2-p320'
run 'which ruby && ruby -v'
and here the error messages that is issued as response to rvm --default use 1.9.2
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
You need to change your terminal settings to allow shell login.
Please visit https://rvm.io/workflow/screen/ for example.
4.1) Using capistrano and rvm-capistrano and hacking a little bit
Update: With the help from mpapis at the RVM chat, I was able to come up with this working solution now:
require "rvm/capistrano"
role :server, ENV[ 'base_image_setup_server' ] if ENV[ 'base_image_setup_server' ]
default_run_options[:pty] = true
default_run_options[:shell] = :bash
set :rvm_ruby_string, 'ruby-1.9.2-p320'
set :rvm_type, :user
def rvm_bin
'$HOME/.rvm/bin/rvm'
end
namespace :images do
task :install_basics do
run "#{sudo} yum install --assumeyes git gcc-c++ autoconf automake make patch zlib-devel libtool bzip2-devel"
run "#{sudo} yum update --assumeyes"
end
task :install_ruby do
rvm.install_rvm
rvm.install_ruby
run "#{rvm_bin} alias create default #{rvm_ruby_string}"
run 'echo "source ~/.rvm/environments/default" >> $HOME/.bashrc'
run 'which ruby && ruby -v'
end
...
desc 'build the base-image'
task :base_image do
install_basics
install_ruby
install_boost
install_rake_and_rack
install_sioux
test_sioux
end
The main different is, that RVM is not used as a function, but the program direct.
kind regards,
Torsten
Check RVM site for Capistrano integration https://rvm.io/integration/capistrano
There are tasks to install RVM and Ruby:
after 'deploy:setup', 'ubuntu:install'
after 'deploy:setup', 'rvm:install_rvm' # do it only with deploy setup
before 'deploy', 'rvm:install_ruby' # do it on every deploy
namespace :ubuntu do
desc "setup ubuntu system"
task :install do
run "apt-get install -y make ...", :shell => "sh"
...
end
end
And you run the standard:
cap deploy:setup
cap deploy:cold
Also you might want to have a look on my example rails app for simple and working deployment script: https://github.com/mpapis/ad and my blog post about it: http://niczsoft.com/2012/03/fast-deployment-using-capistrano-rvm-and-more/

Impossible to install single-user version of RVM

Question is very simple:
I cannot install RVM (single-user installation), as if I follow the instructions on the RVM website, that is:
$ curl -L https://get.rvm.io | bash -s stable
I get a permission denied error at line 360 of the installation script file (the line that starts with echo):
# Perform the actual installation, first we obtain the source using whichever
# means was specified, if any. Defaults to head.
case "${version}" in
(head)
echo "${branch}" > "$rvm_path/RELEASE"
install_head ${branch:-master} || exit $?
;;
Here is the error message:
olivier#~$ curl -L https://get.rvm.io | bash -s stable
bash: line 360: /usr/local/rvm/RELEASE: Permission denied
If I add "sudo" before "bash" in the command above, it works fine, but it is then the multi-user install ...
Indeed, I solved this by uninstalling old versions of RVM: sudo rvm implode and then deleting the file /etc/rvmrc. Once done, I could install the single-user version and everything worked fine!
Sorry Remear, I wanted to edit your answer or complete it via my comment but I could not (comment can only be edited within 5 minutes...at least I upvoted...).
Once you have a system-wide install, you can't run a single-user install as it will detect your system-wide install first and try to update it.
But it is possible to have a root install & then user install by specifying the installation path :
curl -sSL https://get.rvm.io | bash -s stable --path $HOME/.rvm
You will also have to set the single-user install path in your user path manually as RVM does not create it when you already have a system-wide install (not really an intended use) :
# .bashrc
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
And also load your user rvm :
# .bash_profile
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
Are you setting rvm_path in ~/.rvmrc, or in /etc/rvmrc, or in one of your bash scripts? I'd recommend removing both of those files as well as ~/.rvm and then try installing rvm again WITHOUT sudo.
this work for me, first uninstall all versions and then as user (NO root) follow the next commands:
For example, I'm using Ruby 2.6.3 and Rails 5.2.2 and setting up helping a little in these web pages:
https://gorails.com/setup/ubuntu/20.04
https://rvm.io/rvm/install
$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
$ curl -sSL https://get.rvm.io | bash -s stable --ignore-dotfiles
$ source ~/.rvm/scripts/rvm
$ rvm install 2.6.3
$ rvm use 2.6.3 --default
$ ruby -v
$ gem install rails -v 5.2.2
$ rails -v
Check the paths:
$ which -a gem
$ which -a ruby
Now you can Add some gems (inside you app directory):
$ gem install faker
All works great for me, hope this helps!

Cannot set up RVM in Xubuntu on VMware

I am attempting to install RVM on Xubuntu and am having no luck getting the OS/terminal to recognize rvm as something that it can call.
I have been following the instructions here: http://beginrescueend.com/rvm/install/
I first call:
sudo bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )
This seems to be executing correctly. I then run:
sudo echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bashrc
The text is appended to the end of the file.
Next I close out the terminal completely and reopen it. I try running:
type rvm | head -1
It responds:
bash: type: rvm: not found
If I try just calling rvm it responds:
The program 'rvm' is currently not installed. You can install it by typing: sudo apt-get install ruby-rvm
Clearly I am doing something wrong, I cannot seem to determine what that is. Thank you.
By using the sudo version of the install script, you have installed the multi user version of rvm, and then you ran the single user "sourcing" script with sudo...
When using the multi user version, rvm will not install into $HOME/.rvm, but in /usr/local/rvm.
Therefore you will have to use the multi user way to source the rvm script, and that is to add your user to the rvm group.
As shine already posted though, you probably didn't want to install system wide rvm to start with.
Don't do it with sudo. You have installed it for a root and you're not a root.
RVM should be installed in your home folder, you don't need superuser privileges for that.

Ruby Installation

Silly question. I went to http://rubyinstaller.org/downloads/ and installed Ruby 1.9.2, but when I pull up command prompt and type ruby -v it's not recognized as a command. Am I doing something wrong? I had version 1.8.6 installed, but I couldn't figure out how to upgrade my Ruby version so I uninstalled it and tried a fresh install.
You need to add Ruby to your path variable, regardless of your operating system.
Say you're using Windows, and Ruby is installed in C:\Program Files\ruby1.9.2\ You'll need to find out which folder the ruby executable is in (ruby.exe). Sometimes it'll be in the main folder, but usually for open source packages it'll be in the subfolder bin. To add ruby to your path, then, you'll need to use C:\Program Files\ruby1.9.1\bin
You can do this on the command line like so:
path = %PATH%;C:\Program Files\ruby1.9.2\bin
Note that %PATH% has a percent sign on either side, and that there's a semi-colon separating it from the new value. You'll have to type it in each time you start a new command line window, but it might be a good idea to try this the first time, because any mistakes in typing it in won't be permanent.
To change it permanently you can find it in Control Panel > System > Advanced System Settings. Switch to the Advanced tab, then click Environment Variables... Find path under System Variables, and add ;C:\Program Files\ruby1.9.2\bin to the end. Note that you still need a semi-colon to separate the new value from everything else, and that you don't need %PATH% this time (in face, the value you see is what %PATH% represents). Once you've done that, restart any command windows you had open, and you should be able to access things just fine!
These instructions will be different if you're using Linux or a Mac - try googling environment variables if you'd like to know more!
Did you add your bin Directory to the PATH?
Ok, when you install it, it will go in a directory:
ex: c:/program files/ruby1.9.x/
inside you will have a /bin directory which contains all the command for the command line.
To be able to use ruby in the command line, you must add it to your $PATH variable in the OS environment.
ex:
path=c:/program files/ruby1.9.x/bin;etc...
Install RVM with RUBY
sudo apt-get install curl
after install
install rvm also ruby
\curl -sSL https://get.rvm.io | bash -s stable --ruby
if you face issue with the above line (Failed to connect to get.rvm.io port 443: Network is unreachable)
in browser go to https://get.rvm.io save the page in any location
make it executable file name rvm-installer
chmod +x rvm-installer
then do the following
bash rvm-installer stable --ruby
source /home/username/.rvm/scripts/rvm
then check rvm list
note the version of ruby listing on terminal
if you failed to install due to the dependency
sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev
if the above steps not installed ruby latest version you can do it by
rvm install ruby-2.1.1
installation using rvm
sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
curl -L https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc
rvm install 2.1.1
rvm use 2.1.1 --default ruby -v
**change terminal to login shell. open a new terminal **
then rvm use 2.1.1 => ( 2.1.1 version )
then its done!
pd#admin:~$ ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]
programmers keep on coding with ruby.

Resources