Dan Stutzman

How to install RBEnv on EC2 using the Amazon Linux AMI

Published 2012-09-23

Explanations

  • Amazon's Elastic Compute Cloud (EC2) is a popular low-cost way to rent a virtual server for pennies per hour.
  • An Amazon Machine Image (AMI) lets you choose the operating system to install on your EC2 instance. The Amazon Linux AMI is the default choice.
  • RBEnv is a set of shell scripts that make it easy to switch between different versions of Ruby. It's an alternative to RVM.
  • The reason someone would want to install RBEnv on an EC2 instance is probably so they can run a particular version of Ruby.
  • In my case, I wanted to run Capistrano scripts from an Amazon EC2 instance because I could assign the instance a static IP address and have that address whitelisted for a client's firewall, which I couldn't do with my home Internet connection's dynamic IP address.

Install Git

  • sudo yum install -y git

Setup access to your own Git repo (optional)

  • ssh-keygen -t rsa -C your-email-username@your-email-domain.com
  • cat ~/.ssh/id_rsa.pub
  • Add that SSH key to GitHub as follows:
    • Sign in to github.com
    • Click Account Settings (wrenches in upper-right)
    • Click SSH Keys
    • Click Add SSH Key
    • Type in a title
    • Paste in the SSH key from the cat output earlier
  • git clone location-of-your-repo

Install RBEnv and the ruby-build plugin for it

  • git clone git://github.com/sstephenson/rbenv.git .rbenv
  • echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
  • echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
  • mkdir -p ~/.rbenv/plugins
  • cd ~/.rbenv/plugins
  • git clone git://github.com/sstephenson/ruby-build.git
  • exit out of the shell and ssh in again

Install Ruby

  • sudo yum install -y gcc make zlib zlib-devel openssl openssl-devel
  • rbenv install 1.9.3-p194
    Warning: this step took me 38 minutes on a micro-sized instance. The reason is because compiling Ruby from source is CPU-intensive, and micro-sized instances have their CPU resources throttled back drastically, with the exception of the first two seconds. If this is too long for you, consider resizing your instance to "small" until this step finishes.
  • rbenv rehash
  • rbenv global 1.9.3-p194

Test Ruby including some libraries you'll want later

  • irb
    • require 'openssl'
      This should return true
    • require 'zlib'
      This should return true
    • exit