Developing in Ruby with Rails and a Virtual Machine

In order to work with Rails, I’ve setup the freely available Oracle VirtualBox on both of my Mac OS X (personal) and Windows 7 (work) machines to serve as a development environment. I’ve created a new VM running Ubuntu 11.04, Ruby 1.9.2, Rails 3, MySQLPhusion Passenger, and nginx. The greatest benefit to using a virtual machine to develop in is that it eliminates much of the need to rework online tutorials to fit my specific systems – it gives me the same sandbox to play in, no matter what hardware I’m using. Of course, it also keeps my machines running clean, especially while I’m learning. Below are the steps I’ve taken to get setup.

  1. Download and install VirtualBox
  2. Download an ISO of Ubuntu 11.04
  3. Create a VM from the Ubuntu ISO
  4. Launch a terminal and prepare Ubuntu for Ruby (instructions adapted from this site)
    1. sudo -i
    2. apt-get update
    3. apt-get -y install build-essential zlib1g zlib1g-dev libxml2 libxml2-dev libxslt-dev libopenssl-ruby libcurl4-openssl-dev libssl-dev mysql-server
    4. apt-get -y install git-core
    5. apt-get -y install curl wget
  5. Now, build and install Ruby from source (sudo is implied)
    1. cd /usr/local/src
    2. wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p180.tar.gz
    3. tar -xvf ruby-1.9.2-p180.tar.gz
    4. cd ruby-1.9.2-p180
    5. ./configure
    6. make
    7. make install
  6. Install the gems, including Rails and Passenger, as well as the web server, nginx
    1. gem install rails
    2. gem install passenger
    3. passenger-install-nginx-module
  7. If you receive an OpenSSL error, follow these steps:
    1. cd /usr/local/src/ruby-1.9.2-p0/ext/openssl
    2. ruby extconf.rb
    3. make
    4. make install
  8. Configure nginx to run as a service and to start with the vm (instructions adapted from this site)
    1. adduser –system –no-create-home –disabled-login –disabled-password –group nginx
    2. gedit /etc/init.d/nginx (paste the following code in the file and save it)
    3. #! /bin/sh### BEGIN INIT INFO
      # Provides:          nginx
      # Required-Start:    $all
      # Required-Stop:     $all
      # Default-Start:     2 3 4 5
      # Default-Stop:      0 1 6
      # Short-Description: starts the nginx web server
      # Description:       starts nginx using start-stop-daemon
      ### END INIT INFOPATH=/opt/nginx/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
      DAEMON=/opt/nginx/sbin/nginx
      NAME=nginx
      DESC=nginxtest -x $DAEMON || exit 0# Include nginx defaults if available
      if [ -f /etc/default/nginx ] ; then
         . /etc/default/nginx
      fi
      set -e
      . /lib/lsb/init-functions
      case "$1" in
       start)
         echo -n "Starting $DESC: "
         start-stop-daemon --start --quiet --pidfile /opt/nginx/logs/$NAME.pid \
             --exec $DAEMON -- $DAEMON_OPTS || true
         echo "$NAME."
         ;;
       stop)
         echo -n "Stopping $DESC: "
         start-stop-daemon --stop --quiet --pidfile /opt/nginx/logs/$NAME.pid \
             --exec $DAEMON || true
         echo "$NAME."
         ;;
       restart|force-reload)
         echo -n "Restarting $DESC: "
         start-stop-daemon --stop --quiet --pidfile \
             /opt/nginx/logs/$NAME.pid --exec $DAEMON || true
         sleep 1
         start-stop-daemon --start --quiet --pidfile \
             /opt/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
         echo "$NAME."
         ;;
       reload)
           echo -n "Reloading $DESC configuration: "
           start-stop-daemon --stop --signal HUP --quiet --pidfile /opt/nginx/logs/$NAME.pid \
               --exec $DAEMON || true
           echo "$NAME."
           ;;
       status)
           status_of_proc -p /opt/nginx/logs/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
           ;;
       *)
         N=/etc/init.d/$NAME
         echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
         exit 1
         ;;
      esac
      exit 0

    4. chmod +x /etc/init.d/nginx
    5. /usr/sbin/update-rc.d -f nginx defaults
    6. /etc/init.d/nginx start
  9. Done
You now have a full-fledged development environment. Break it, twist it, do what you want. It won’t hurt your host machine, and you may save yourself some headaches following online tutorials (I hope).
About these ads

5 thoughts on “Developing in Ruby with Rails and a Virtual Machine

  1. Hi, Great Tutorial. The only issue I had is that the formatting of the script was off, and it caused some lines to run together, which made the script not work. For example the line

    fiset -e

    should be

    fi
    set -e

    Better to save the next person time and get that formatting fixed.

    Overall great tutorial though!

  2. Pingback: How to Install Latest Ruby on Rails on Ubuntu Linux | FreshSuperCool.com

    • Great question. I suppose the answer is more a matter of personal preference. While I still use VirtualBox+Ubuntu on my Windows machine, I’m now working in rails natively on my Mac. As long as I can use a terminal window on a *nix platform, I’m happy.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s