Marrying Mac and Ubuntu A Tale of Rails, TextMate, and VirtualBox

22 Dec 2010 Pete DeLaurentis

This may sound familiar. I develop Ruby on Rails apps using TextMate on my Mac. Production is a pack of Ubuntu servers running in Amazon’s cloud.

The Problem

But there’s a problem with this setup: running Rails apps on Mac isn’t always easy, and there are slight differences with Ubuntu in the cloud. Ruby gems with native extensions rarely build properly out of the box, and I’ve spent many a late night hacking to get them working on Mac.

So, one night, when faced with a particularly difficult Ruby gem, I decided to try something different. It worked, so I wanted to share.

The New Setup

I’m now editing my Rails app on Mac using TextMate, sharing the app folder, and running a test-server on the same machine using VirtualBox and Ubuntu 9.10.

VirtualBox lets you run Linux, Mac, or Windows in a virtual computer. It’s free, fast, and works on any OS.

Installing Ubuntu on VirtualBox

This was incredibly easy. I setup a virtual machine with 5GB HD, 1024 RAM, then installed an Ubuntu 9.10 server edition ISO. Be sure to select Bridged Network mode, so you can access the virtual server from your Mac.

Sharing Files from Mac

Before starting the virtual machine, click the Settings button and go to the Sharing tab.

The development folder on my Mac is called /deploy, and I want it to appear as /deploy under Ubuntu too. But a trick is required.

I created a mapping with the Folder Path “/deploy” and the Folder Name “deploy-share”. The Folder Path and Folder Name cannot be the same, which is why I appended the “-share”.

Mapping Shared Folders in Ubuntu

This was the least straightforward part. Folder Sharing support requires VirtualBox to make some updates to Ubuntu. First, go to the VirtualBox Devices Menu, and click “Install Guest Additions”

Nothing will happen, but that’s okay. Next, go to Ubuntu terminal and type the following commands:

sudo apt-get install -y build-essential linux-headers-$(uname -r)
sudo mount /dev/cdrom /mnt/
sudo /mnt/VBoxLinuxAdditions-amd64.run

Next, you can mount the share folder. The instructions below create a folder called /deploy on Ubuntu, which maps to the same folder on Mac.

sudo mkdir /deploy
sudo mount -t vboxsf -o uid=1000,gid=1000 deploy-share /deploy
sudo echo “deploy-share /deploy vboxsf uid=1000,gid=1000,rw 00”» /etc/fstab

Preparing TextMate

TextMate adds meta-data to files which can cause problems on Ubuntu. You can fix this from the console on your Mac (fix is courtesy of CloudSpace’s Blog):

Mac$ sudo defaults write com.macromates.textmate OakDocumentDisableFSMetaData 1

Using Mac Terminal

You could use the built-in Ubuntu server edition terminal, but Mac terminal is prettier and supports copy/paste. To access from your Mac, first you need the IP address of the virtual server.

Ubuntu$ ifconfig | grep 192.168.  # The IP address labeled "inet addr" is the right one.
Mac$ ssh root@192.168.x.x

Happily Ever After

I’ve been using the setup for a week, and found it to be very fluid.

It’s the best of both worlds. I can use all my favorite Mac editing tools. The code runs in the same environment as the production servers on the cloud. Native gems install without issue. The shared folder is accessed quickly by either OS. I can even manage source control on the /deploy folder in both Mac and Linux.

And there was a nice little surprise too: my Mac is snappier when running the Rails app on VirtualBox than it ever was running it natively.

If this helps you, please pass it onto others.