setting up an EC2 server with rvm via scripting - ruby

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/

Related

cannot install ruby using rvm as an unprivileged user with no sudo privileges

According to the rvm documentation at
https://rvm.io/rvm/install
it should be possible to install rvm, and subsequently a ruby, as a non-privileged user.
Under troubleshooting at
https://rvm.io/support/troubleshooting
it states:
Any user in the rvm group can update RVM, rubies, and gemsets. There
is never a reason to use sudo post-install.
rvm installs fine as a non-privileged user (There is no rvm group):
nosudo-user#my-site.com:~$ bash ~/downloads/rvm_installer.sh stable
Downloading https://github.com/rvm/rvm/archive/1.29.12.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.12/1.29.12.tar.gz.asc
...
Installing RVM to /<user-home>/.rvm/
...
However, when I try to install a ruby version, it fails (I think) trying to do an apt-get because it needs sudo privileges:
nosudo-user#my-site.com:~$ rvm install 2.6.6
Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/ubuntu/20.04/x86_64/ruby-2.6.6.tar.bz2
...
Updating system... password required for 'apt-get --quiet --yes update':
.
Error running 'requirements_debian_update_system ruby-2.6.6',
please read /<user-home>/.rvm/log/1659735568_ruby-2.6.6/update_system.log
Requirements installation failed with status: 1.
From the log:
2022-08-05 21:39:28] requirements_debian_update_system
requirements_debian_update_system ()
{
__rvm_try_sudo apt-get --quiet --yes update || {
\typeset __ret=$?;
case ${__ret} in
100)
rvm_error "There has been an error while updating your system using \`apt-get\`.
It seems that there are some 404 Not Found errors for repositories listed in:
/etc/apt/sources.list
/etc/apt/sources.list.d/*.list
apt-get update (as a user with sudo privileges) shows no errors.
How is one supposed to install rvm so a non-privileged user can install a version of ruby?
My guess is your system is missing some basic system libraries for buiding Ruby from source. Hence RVM tries to fetch the build libraries for you. This will be a one-time update, and after your system has the proper libraries installed, RVM will not require sudo again.
However if you want you can disable apt updating completely with:
rvm autolibs disable
Note that if you have missing build requirements, then the build step of Ruby will quite likely fail. Only way to find out is to try.
In that case you can use the requirements command to help you install missing dependencies:
rvm requirements
Casper's answer above provided the missing information; there were some missing libraries. However, the steps required to be able to install as an unprivileged user were rather un-intuitive (to me, at least):
Install rvm using sudo; in that process it creates the "rvm" group.
Add the desired users to the rvm group
Note:
Attempting to install a ruby as an unprivileged user after this step still fails.
sudo to root to get the rvm magic paths installed for root, then start installing a ruby, but kill the process after the requirements installation is complete.
Log in as the desired unprivileged user and use rvm to install the desired ruby for that user.
Cmds:
sudo rvm_installer.sh stable
sudo usermod -a -G rvm the-user
sudo -i
rvm install 2.6.6
Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/ubuntu/20.04/x86_64/ruby-2.6.6.tar.bz2
Checking requirements for ubuntu.
Installing requirements for ubuntu.
Updating system..
Installing required packages: g++, gcc, autoconf, automake, bison, libffi-dev, libgdbm-dev, libncurses5-dev, libsqlite3-dev, libtool, libyaml-dev, make, pkg-config, sqlite3, libgmp-dev, libreadline-dev......................|
Requirements installation successful.
^C
exit
sudo -i -u the-user
rvm install 2.6.6

VagrantFile inline script and rvm provisioning

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

Installing RVM on existing ruby1.8 installation

I have a big problem with my Debian server configuration. Someone before me has made a big mess with ruby installation and now i can't manage to get it all working. What i'm trying to do now is to get back to environment, where ruby1.8 is installed and rails app is working correctly with apache+passenger. If it would be working on RVM it would be perfect. Here is the story:
There was ruby 1.8 installed on a server. The person has removed it with sudo apt-get remove ruby1.8 and then installed version 2.0.x manually using those commands:
mkdir /tmp/ruby && cd /tmp/ruby
curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz | tar xz
cd ruby-2.0.0-p247
./configure
make
sudo make install
Then, he has removed all the dirs listed by whereis ruby with rm command.
And here i came - i want to install RVM on this server. The problem is, no matter what i do, i get a lot of errors from all sides. Steps i've done:
# Install RVM
curl -L https://get.rvm.io | bash -s stable
# install 1.8.7 version
rvm install 1.8.7
# use 1.8.7
rvm use 1.8.7
# Install gems
bundle install
First thing (and that's just a tip of iceberg) is that i'm not able to start apache2 in cause of the following error:
apache2: Syntax error on line 203 of /etc/apache2/apache2.conf: Syntax
error on line 1 of /etc/apache2/mods-enabled/passenger.load: Cannot
load
/usr/lib/ruby/gems/1.8/gems/passenger-4.0.5/libout/apache2/mod_passenger.so
into server:
/usr/lib/ruby/gems/1.8/gems/passenger-4.0.5/libout/apache2/mod_passenger.so:
cannot open shared object file: No such file or directory Action
'configtest' failed.
Looks like there is even no /usr/lib/ruby/ dir on my system right now.
The question is: is there any way to reset all ruby-releated libraries on debian to the original state (debian without ruby installed) and install working RVM on top of that? Thanks in advance for any help.
Verify if, Is ruby installed correct by ruby -v
Install phusion passenger
$ sudo gem install passenger
$ sudo passenger-install-apache2-module
and then follow onscreen instructions

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.

It seems that I can't install capistrano with rails-less. Why is this?

I'm struggling here with no success in order to install capistrano on my Ubuntu dev machine.
Here is the list of commands issued:
1) sudo apt-get update
2) sudo apt-get install build-essential git-core curl
3) bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
4) echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc
5) (close and reopen terminal window)
6) rvm notes
(looked for: "For Ruby (MRI & ree) you should install the following OS dependencies:"
and did:
7) sudo aptitude install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev
Now we can install ruby by doing:
8) rvm install 1.9.2
Then we tell rvm to use it:
9) rvm 1.9.2
And to do it always:
10) rvm --default use 1.9.2
With ruby properly instaled we can now:
11) gem install capistrano
And then:
12) gem install railsless-deploy
Despite all those steps being successful steps, when I do: cap -T
I'm unable to see the deploy tasks listed as expected.
I only getting: cap invoke cap shell
listed.
Why?
Do I need a Capfile ? With some require settings? If so, then, does that mean that we need to issue or capistrano command line commands only after we move the project directory? Please advice, I'm absolutely newbie on Capistrano here.
Thanks a lot,
MEM
Yes you still need a Capfile.
STEP 1. Create a Capfile with the following command, running it from within the project directory:
capify .
(This will also create config/deploy.rb)
STEP 2. Edit this new Capfile as per the railsless-deploy readme, so that it has the following content:
require 'rubygems'
require 'railsless-deploy'
load 'config/deploy'
At this point you can run
cap -T
and you should see tasks such as "cap deploy" listed. However, capistrano knows nothing about your target server(s), etc, yet.
STEP 3. Edit config/deploy.rb to provide the details of your SCM, server name(s), etc.
For help on this, see the section "Configuration" in the Capistrano "From The Beginning" wiki.
You may also override the deploy task here if the default provided by railsless-deploy is unsuitable.
A minimal config/deploy.rb might look as follows:
set :user, "myuser"
set :application, "myapp"
set :repository, "https://github.com/me/myapprepo"
set :scm, :git
default_run_options[:pty] = true
role :web, "myhost.example.com"
role :app, "myhost.example.com"
Of course you must correct the above with your app, host and login details.
You can then follow the typical path of
cap deploy:setup
cap deploy:check
cap deploy
# ... etc
However, after cap deploy:setup has created the deploy directory tree (by default something like /u/apps/myapp/) on the remote server(s), you may have to alter ownership (or permissions) in order for cap deploy to run. The following will recursively change the owner and group of /u/apps/myapp/ to user "myuser".
sudo chown -R myuser. /u/apps/myapp
On our servers, I actually create this /u/apps/ directory and change its ownership on server preparation (say, with puppet), long before capistrano sees them. That way, developers can use cap deploy:setup and cap deploy, etc, without hitting these issues.
"does that mean that we need to issue or capistrano command line commands only after we move the project directory?"
I'm not sure I understand this part of your question. If you move your project directory, you'll still be able to execute capistrano commands, but you must always execute them from within the project directory.

Resources