Question

I want to provision Ubuntu Server machine with latest Ruby and Ruby Gems version using Ansible.

How do I do this?

Was it helpful?

Solution

Solution #1: Using APT and Symlinks

I recommend to use this solution if it's OK to install Ruby 2.0 and Ruby Gems globally (for all users). If you want to install another version or isolate it from other users - please see solution #2.

Here's a simple Ansible playbook that will install latest Ruby 2.0 and Ruby Gems for you:

Ubuntu 14.04 (Trusty Tahr)

- name: Latest version of Ruby is installed
  apt: pkg={{ item }} state=latest
  with_items:
    - ruby2.0
    - ruby2.0-dev

- name: Symlink exists for Ruby 2.0
  file: src=/usr/bin/ruby2.0 dest=/usr/local/bin/ruby state=link

- name: Symlink exists for Ruby Gems 2.0
  file: src=/usr/bin/gem2.0 dest=/usr/local/bin/gem state=link

Ubuntu 13.10 (Saucy Salamander)

- name: Latest version of Ruby is installed
  apt: pkg={{ item }} state=latest
  with_items:
    - ruby2.0
    - ruby2.0-dev

- name: Making Ruby 2.0 the default one
  command: update-alternatives --set ruby /usr/bin/ruby2.0

- name: Making Gem 2.0 the default one
  command: update-alternatives --set gem /usr/bin/gem2.0

Provided playbooks must be executed with sudo: yes for obvious reasons.

Solution #2: Using RVM

RVM is installing ruby and it's gems locally for current user. So it leads to a problems in multi-user environment. In my use-case it was not working correctly for some reason. So I would recommend to stick to the first solution if possible. Although, if you know what you are doing and understand the complications here goes the RVM solution.

I suggest to create a simple shell script to install current version of Ruby and Ruby Gems with RVM and invoke it later on a provisioned machine.

Here's the Bash script:

#!/usr/bin/env bash

# Checking if RVM is installed
if ! [ -d "~/.rvm" ]; then
    echo "Installing RVM..."
    \curl -sSL https://get.rvm.io | bash -s stable
    source ~/.rvm/scripts/rvm
    echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc
else
    echo "Updating RVM..."
    rvm get stable
fi;

echo -n "RVM version is: "
rvm --version

echo "Installing Ruby..."
rvm install ruby

echo "Making installed Ruby the default one..."
rvm use ruby --default

echo "Installing latest version of Ruby Gems..."
rvm rubygems current

This script will install RVM (or update it to a latest stable version if it's already installed) and it will install latest stable versions of Ruby and Ruby Gems.

And here's the playbook to copy provided script to the provisioning machine and invoke it:

- file: path=~/provision/ruby state=directory
- copy: src=../../files/ruby/install.sh dest=~/provision/ruby/install.sh mode=775

- name: Latest Ruby is installed
  shell: /usr/bin/env bash ~/provision/ruby/install.sh

Just place your script near the Ansible's playbook and update the paths.

I hope it will help someone.

OTHER TIPS

There's now an official Ruby/RVM playbook: https://github.com/rvm/rvm1-ansible

To install: ansible-galaxy install rvm_io.rvm1-ruby -p ROLES_PATH

Example playbook:

---

- name: Configure servers with ruby support
  hosts: all
  roles:
    - { role: rvm_io.rvm1-ruby, tags: ruby, become: True }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top