Getting started with Ruby on Rails on Mac OS X

Choosing your Ruby packages

I have decided to get to grips with Ruby on Rails and get it set-up on my MacBook Pro. There seem to be a variety of options available:

  1. Use Locomotive – an all-in-one standalone developer package
  2. Official sources – configure, compile and install each
  3. MacPorts – package management for free software
  4. Fink – package management for free software

As Scott Stevenson points out this is too many options for a system that is described as Convention over configuration.

As I already use Fink for installing the majority of free software on my Mac I decided I’d go with that. At least until Leopard ships with Ruby on Rails in-the-box.

Preparing Fink

GrinGod gave me a heads-up that we’ll need to turn on the unstable packages to get the gems package and latest version of Ruby. The details are in the Fink FAQ but in summary:

fink configure

And choose the option for unstable trees. Then type:

fink selfupdate
fink index
fink scanpackages

And make sure you have rsync or cvs updating turned on to get the descriptions.

Ruby (via Fink)

Ruby is the language we wish to use and Fink currently supports 1.6 and 1.8 The following commands will install 1.8 for us and the Ruby Gems package management system:

fink install ruby
fink install rubygems-rb18

Rails (via Gem)

Now we need the Rails web framework which will be installed using gem.

gem install rails --include-dependencies

Choosing your web server

There are a number of options available again here including Apache, LightTPD and Mongrel. I wanted to give LightTPD a shot as it is on Fink and less of a beast than Apache. I tried various combinations but even having installed the lighttpd-fastcgi module I couldn’t get the fastcgi gem to do anything but spew errors so Mongrel it is.

Mongrel (via Gem)

Back at that Terminal window type:

sudo gem install mongrel --include-dependencies

Select the mongrel 1.0.1 (ruby) and fastthread 1.0 (ruby) options as prompted.

Testing Ruby, Rails & Mongrel

Again at the Terminal window:

rails mytest

A number of files should be created if Ruby & Rails are working correctly. Now type:

cd mytest
script/server

You should see a line stating Starting Mongrel listening at 0.0.0.0:3000

Open your browser of choice and navigate to http://localhost:3000

Choosing a database

You can go with MySQL, PostgreSQL or SQLite from Fink alone. I went with MySQL for a change.

MySQL 5.0

Whilst Mac OS X Server comes with MySQL the standard edition does not. Rather than use the Fink port I went with the official MySQL 5.0 package primarily to get the preferences pane that allows me to start and stop it as required however it also includes a helpful monitoring widget.

To install download and run the MySQL…x86 package.

Copy MySQL.prefPane to /Library/PreferencePanes and re-open System Preferences. Head into the new MySQL pane and start MySQL up.

MySQL Tools

To manage MySQL and download the Mac OS X version of the GUI management tool.

Run the MySQL Administrator application and connect to localhost on the default port of 3306 with the user name root and a blank password. Don’t forget to change the password by heading over to the Account pane.

Ruby with MySQL

Install the native Ruby to MySQL library to improve db performance by heading back to the Terminal window and executing:

sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

Select the option for mysql 2.7 (ruby)

Testing the database

As MySQL doesn’t include a sample database head into MySQL Administrator and into the Catalogs pane. Create a new database by pressing the + button and giving it the name mytest.

Select mytest from the left then choose Create Table… from the Table Actions drop-down list.

Enter the table name products then use the + button to create the following columns:

Name Type Primary Key? Nullable? Auto Increment?
id INT Y N Y
name VARCHAR(255) N N N

Click Apply, Execute, OK then Close.

Go to the Terminal window and cd into the mytest folder you generated earlier.

Edit the config/database.yml file using nano or TextEdit and change the database name from mytest_development to mytest. You will also need to enter the password here – either for your root account or ideally a special application-specific account you set-up in MySQL Administrator.

Back at the trusty Terminal execute:

script/generate scaffold product
script/server

Now open your browser to http://localhost:3000/products/ and try creating, editing and deleting some products.

Next steps

And of course trying to find an editor/IDE that supports Ruby as Xcode certainly doesn’t and TextMate costs money (I’m happy to pay for things when I’m committed to the tech – not before)

[)amien

4 responses

  1. Avatar for Andrew Donaldson

    It is a bit of a faff getting started, but amusingly it's a little easier to get started (at least with the base packages) on Windows!

    Try not to hate it from the start (i.e. reading too much into the marketing hype), just play with it with an open mind and you'll soon find the little things that make you smile.

    One comment on ruby versions though. From the rails page:

    We recommend Ruby 1.8.5 for use with Rails. Ruby 1.8.4 and 1.8.2 are still usable too, but version 1.8.3 is not.

    Just in case you notice anything weird happening that you can't explain.

    The main thing to remember with rails though, is that while it's Convention over configuration, that means you have to understand the rails conventions before you get the most benefit.

    Until then, it can seem slightly awkward and a bit foreign if you're used to .net (speaking from experience).

    Andrew Donaldson 9 July 2007
  2. Avatar for Damien Guard

    There is always the risk that people coming to a new platform will bring their old approach and expectations with them.

    We see C programmers immediately bypassing most of .NET heading straight to P/Invoke with disastrous results.

    It's an exercise in futility to fight against the principles of an environment.

    Damien Guard 9 July 2007
  3. Avatar for Adrian Ritchie

    One thing to note, for users that are new to Fink, is that you may need to configure Fink to install unstable packages in order to be able to install rubygems.

    Also, at time of writing this, the stable install of Ruby using Fink is 1.8.1, whereas the unstable install is 1.8.4

    Adrian Ritchie 9 July 2007
  4. Avatar for ASpanishGuy

    As ide, try netbeans 6.0M10

    ASpanishGuy 10 July 2007