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:
- Use Locomotive – an all-in-one standalone developer package
- Official sources – configure, compile and install each
- MacPorts – package management for free software
- Fink – package management for free software
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.
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:
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.
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.
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?|
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.
- Watch these great Rails screencasts
- Run through some of these 12 tutorials
- Read the Agile Web Development with Rails book
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)