Saturday, March 03, 2012

Ruby on Rails, Ubuntu style

I've been going through the Ruby on Rails tutorial on my work laptop, but it's hardly ideal: it's not my machine, it's often at work, and unlike my home computer it's a Windows box. So I had the bright idea of installing Oracle Virtual Box, Ubuntu (like at home), and move the sample app I'm building onto a Dropbox shared folder.

So:
1. Install Virtual Box - painless, and very similar to VMWare
2. Install Ubuntu - basically painless, although the CD I was using was 10.10, and it required 2 further updates to bring it up to the latest version. Not sure why Ubuntu couldn't have just skipped to the latest without having to install 11.04 first - I didn't bother to check out the system update options, so this may just have been me being lazy.
3. Install Rails. And here's where things started to go wrong...

Installing Rails on Windows was easy: there's a ready made installer that covers everything, double click, next-next-next and you're done. There's even a cool video that runs you through setting up an account on Engine Yard, Github and so on. It's basically foolproof. On Ubuntu, however, and as with nearly everything else you install on it, you need to do a bit more work.

So, here's a simple how-to for a clean, new Ubuntu 11.10 install.

1. sudo apt-get install ruby1.9.1-full

2. export GEM_HOME=/home/jim/.gem (Here you need to set GEM_HOME to a directory that your user has write permissions for, I used the standard gem directory)

3. gem install rails

4. gem install bundle

EDIT==: after trying to follow these instructions on a different Ubuntu machine, I needed to include the $GEM_HOME/bin directory to my path (in bashrc), for whatever reason the install didn't leave a link to bundle in the /usr/local/bun/ directory... Given the amount of crap I installed and uninstalled when trying this the first time, I'm not overly surprised. == END_EDIT

Unfortunately, the basic bundle script currently defaults to ruby1.8, you need to modify it:

5. sudo vi /usr/local/bin/bundle (modify the shebang)

6. sudo vi /usr/local/bin/rails (modify the shebang)

If you're about to make a clean, shiny new Rails project you're probably done at this point, but as I wanted to run my sample app I had to do a couple more things to get it running.

I tried to install and fire up my sample app - developed as you'll recall on Windows. While I'd been following the tutorial fairly closely, I'd been forced to make a couple of changes to get the console to work correctly under cmd.exe. Unfortunately, these changes weren't directly compatible with Ubuntu, and I had to go through Gemfile and Gemfile.lock deleting the win32 dependencies. Further, there were a series of other dependencies that failed due to missing libraries:

1. nokigiri dependencies:

sudo apt-get install libxml2-dev
sudo apt-get install libxslt1-dev
sudo apt-get install libpq-dev

2. spork dependencies:
sudo apt-get install nodejs

EDIT==
3. Sqlite3:
sudo apt-get install sqlite3
sudo apt-get install libsqlite3-dev
== END_EDIT

... finally, all was well:

rails s - no problem entering in http://127.0.0.1:3000
bundle exec spork and bundle exec rspec spec - 40 something tests run perfectly, and 0 failures :)

No comments: