More articles about Development

More articles about Ruby on Rails

Setting up Rails on Leopard (Mac OS X 10.5)

By Christopher Haupt

Published April 23, 2008  |   6 comments

This guide walks you through setup instructions for preparing a Mac OS X 10.5 (aka Leopard) development machine to be used for general Ruby on Rails coding. This baseline setup is what we use for our LearningRails online course and the in-person seminars we offer.

You will end up with a development machine with the following baseline components:

  1. Ruby and all basic Ruby utilities
  2. Ruby Gems package manager
  3. Subversion client
  4. Native development tools (Xcode, C compiler)
  5. MacPorts native code package manager
  6. MySQL database client utilities and server
  7. Gems for Ruby on Rails, Capistrano, Mongrel, Mongrel Cluster, and MySQL
  8. Programmer’s editor or IDE

Note: In the command sequences we illustrate here, command line prompts are shown as a dollar sign ($).

Prerequisites

This guide assumes you have a Macintosh computer running the current Leopard operating system with up-to-date System Update patches applied. It also assumes you have not set up alternate Ruby on Rails tools prior to running through this guide. If you have, then small adjustments may be required as you walk through the following instructions.

You will need to have access to an Internet connection to complete various download steps.

You will need to have administrator access to your computer to complete this guide. We will be using the sudo command to run various command line programs and some of the Mac OS X native installers will also ask you for your password.

It is helpful if you have access to your operating system installation discs.

The Recipe

Follow this recipe in sequence. If you have previously installed a particular component, you can usually skip the associated step.

Ruby and Ruby Utilities (irb, ri, rdoc)

Leopard comes pre-installed with Ruby 1.8.6 and its associated utilities. You can use these programs as is. To check them out, open a Terminal window and type:

    $ which ruby
    /usr/bin/ruby
    $ ruby -v
    ruby 1.8.6 (2007-09-24 patchlevel 111) [universal-darwin9.0]

You should seem similar responses. The important one is the first one which reports back the path of the Ruby interpreter program. If you are using the built-in Ruby, it will be located in /usr/bin/.

Ruby Gems Package Manager

Leopard comes with a pre-installed version of gem. Be sure you have the latest version:

    $ gem -v
    1.0.1

You need 1.0.1 or newer. If you have an older version, you can update with the command line: sudo gem update --system.

Native Development Tools (Xcode 3.0)

You will need a native compiler to build many of the gems’ native libraries you are going to use. You will also occasionally build other native tools via the MacPorts tool described below. Apple provides a free native compiler tool set called Xcode. If you have your Leopard installation DVD, load it now. If you don’t have an installation DVD, you can download the Xcode 3.0 tools at Apple’s Developer Web Site. (Note: Apple developer accounts are free.)

Open the Optional Installs folder, and then the Xcode Tools folder. Double click on the XcodeTools.mpkg installer and select a standard install. This will take a few minutes to run:

MacPorts

MacPorts is a native code package manager for Macintosh software. This guide uses MacPorts to setup an installation of MySQL. There are many other tools available in the MacPorts library, so it is well worth checking out.

Download the Leopard Universal version (1.6.0 at the time of this writing) and double click the MacPorts DMG file to open it up. Double click on “MacPorts-1.6.0.pkg” to start the installer and select the default options.

After MacPorts completes installation, you need to adjust your command line PATH environment variable so you can run the port command.

Fire up the Terminal program and enter the command:

    $ open .bash_profile

Note that there is a period in front of “bash_profile”. The bash shell configuration file should open in the TextEdit program.

(By the way, we recommend iTerm as a nice open source replacement for the Apple Terminal program.)

Inside of .bash_profile, find the line that starts with export PATH=, if present. You are going to insert the new directories used by MacPorts into your path:

    export PATH="/opt/local/bin:/opt/local/sbin:$PATH" 

If you don’t have a line that exports your PATH, use the text exactly as above. If you do already have such a line, add the /opt/local/bin:/opt/local/sbin: (note colons) after the first quote, but before any other paths. Here is an example:

    export PATH="/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:$PATH

Save the file and close TextEdit. Open a new terminal window and have MacPorts update itself with the command:

    $ sudo port selfupdate

Installing MySQL via MacPorts

By leveraging MacPorts to install MySQL, maintenance of the software is slightly easier, especially when you want to upgrade over time. We are using MySQL on our development machine as we prefer to have identical software across our environments. Rails 2.0.2 uses SQLite by default, which is fine for development and experimentation, but not appropriate for production code.

To get started, type into your terminal window:

    $ sudo port install mysql5 +server

This command downloads and installs the baseline MySQL client programs and server software. Next, you want to configure MySQL’s server so it launches when your computer boots up:

    $ sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist

launchctl is an Apple tool that administers the system daemon that controls the boot process and background programs. Here you are loading the instructions for how to manage MySQL.

A fresh MySQL installation requires its database storage area to be initialized, so you do that next:

    $ sudo mysql_install_db5 --user=mysql

When you configure the storage area with this command, you’re making sure it is owned by the user “mysql”.

MySQL creates a special system file used for program to program communication, called the “socket” file. By default, the MacPorts installation of MySQL server creates this in the directory ”/opt/local/var/run/mysql5/mysqld.sock”. Ruby on Rails applications can deal with this just fine if you change the settings in your database.yml file to include a socket entry that points to the correct place. However, we are going to tweak things so all applications can find the file in a fairly standard place: /tmp/mysql.sock with little or no modification.

First, you need to move the default configuration file, my.cnf, to the correct place:

    $ sudo mv /opt/local/etc/my.cnf /opt/local/etc/mysql5/my.cnf

If the installation program didn’t put a file into /opt/local/etc/, try this instead:

    $ sudo mv /opt/local/share/mysql5/mysql/my-medium.cnf /opt/local/etc/mysql5/my.cnf

Now, you need to edit the configuration file to change where the socket file is stored:

    $ sudo pico /opt/local/etc/mysql5/my.cnf

If you aren’t familiar with the pico command line editor, we explain the few commands you will need here.

Inside of pico, use your arrow keys to move down to the line where you first see “[client]” and make the following changes (this is just a small part of the whole file):

    ...
    # In this file, you can use all long options that a program supports.
    # If you want to know which options a program supports, run the program
    # with the "--help" option.

    [mysqld_safe]
    socket          = /tmp/mysql.sock

    # The following options will be passed to all MySQL clients
    [client]
    #password       = your_password
    port            = 3306
    socket          = /tmp/mysql.sock

    # Here follows entries for some specific programs

    # The MySQL server
    [mysqld]
    port            = 3306
    socket          = /tmp/mysql.sock
    ...

You are adding the “[mysqld_safe]” section (2 lines) just above “[client]” and then changing the two instances of the “socket = ” lines in the “[client]” and “[mysqld]” sections to be /tmp/mysql.sock. Once done, press Control-X, answer Y when asked to save, and press return to accept the default file name (“my.cnf”).

Now you start the server up manually:

    $ cd /opt/local ; sudo /opt/local/lib/mysql5/bin/mysqld_safe &     

You can confirm that MySQL is running by trying to fire it up:

    $ mysql5 -p -u root
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 1
    Server version: 5.0.45 Source distribution

    Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

    mysql> exit

When you reboot your computer, MySQL should start automatically in the future.

Gems

Leopard conveniently pre-installs the base collection of gems you will be using, but you need to make sure they are up to date:

    $ sudo gem update

All installed gems (including Ruby on Rails and its dependencies, Rake, Capistrano, Mongrel, and Mongrel_Cluster) will get updated.

The MySQL adapter gem needs to be installed, and it is a little finicky due to our use of MacPorts.

If you are running Leopard on an Intel processor, use this command (on one line):

    $ ARCHFLAGS="-arch i386" sudo gem install mysql -- --with-mysql-config=/opt/local/bin/mysql_config5

and if you are on a PowerPC processor, use this command:

    ARCHFLAGS="-arch ppc" sudo gem install mysql -- --with-mysql-config=/opt/local/bin/mysql_config5

Once the command completes, you should be all set with the baseline gems you will need for the LearningRails courses.

Code Editing Tools

While you can get by with using a plain text editor like TextEdit, or even Apple’s Xcode IDE, you will be more productive if you use a programming editor that is highly tuned to Ruby on Rails development.

We use the commercial TextMate programmer’s editor for much of our day-to-day work. TextMate is highly extensible through a collection of community supplied “bundles”. Many add-ons accelerate development by enhancing the editor (for example, adding language specific short cuts to reduce your typing) or by tying in to other utilities, such as Subversion or Rake, to allow you to quickly get tasks done without leaving the editor’s environment.

There are a variety of good open source or free programmer editors available too. On the open source side, Leopard comes pre-installed with both vim and emacs. jEdit is a very extensible open source editor written in Java. TextWrangler is a free programmer’s editor from BareBones.

Whatever editor you choose, be certain that it provides easy navigation among a large number of open files. Working with Rails applications generally involves dealing with a lot of small files, and that process needs to be efficient.

If you prefer an all-in-one tool, ook at one of the integrated development environments for Ruby. We use Netbeans when we aren’t using TextMate, but the numerous other options listed at BuildingWebApps.com are worth a look.


Add your comment on this article






Reader comments on this article

From: nate       Date: 04/27/08 11:23 PM

Subject: $ open -e .bash_profile

will open (if it exists) the .bash_profile file in TextEdit. This will be simpler than trying to figure out vi if you don't already know it. Without the "-e" option, your shell will try to run .bash_profile as a program.

From: Pattie       Date: 04/27/08 10:10 AM

Subject: Can't start up the server

I get to this part: $ cd /opt/local ; sudo /opt/local/lib/mysql5/bin/mysqld_safe & and then it gives me this output: [5] 38397 Macintosh:local Pattie$ Starting mysqld daemon with databases from /opt/local/var/db/mysql5 STOPPING server from pid file /opt/local/var/db/mysql5/Macintosh.local.pid 080427 13:53:13 mysqld ended any ideas?

From: Vdub       Date: 04/18/08 11:11 AM

Subject: error trying to install mysql

when i type into terminal sudo port install mysql5 +server i get the error ---> Building zlib with target all Error: Target org.macports.build returned: shell command " cd "/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_archivers_zlib/work/zlib-1.2.3" && make all " returned error 127 Command output: sh: make: command not found Error: The following dependencies failed to build: openssl zlib Error: Status 1 encountered during processing.

From: Snowman11       Date: 04/16/08 05:17 PM

Subject: liquidoz

I read somewhere to type touch .bash_profile to create a file. I did that and added the path and now it works... In His Service, Snowman

From: Christopher Haupt       Date: 04/10/08 05:17 PM

Subject: Setting you path

When you first fire up terminal, make sure you are in your home directory. Type (in lowercase) "cd" and press return. Then, type "ls -a" (that's an el). If you don't have a .bash_profile file, you can create one with "touch .bash_profile". You should be able to continue with the "open .bash_profile" command. If not, you can use "vi", a command line editor. In vi, if the file already contains a line that starts "export PATH=", then move down to just after the "=" sign and open quote. In vi, the commands for moving are the letter keys "hjkl" (left, down, up, right". Press "i" to insert after that quote the following: "/opt/local/bin:/opt/local/sbin:" (not including the quotes here, of course). Now, press "ctrl-[" (control and open square bracket). Then ":" (colon), and "wq" and return. You should be done. Close the terminal window, open a new one, and type "set". You should see the PATH variable with your new values. Try to type the command "which port". It should be there and you can start using it.

From: liquidoz       Date: 04/10/08 09:09 AM

Subject: This is fantastic! (but)

Thanks to BWA for posting this piece! I'm a new to Mac and RoR, so the level of detail was what I needed. I'm looking forward to what you have planned. That said, I ran into a problem at "Fire up the Terminal program and enter the command: $ open .bash_profile". The file wasn't found. It seems that I wasn't the only one (http://trac.macports.org/projects/macports/ticket/13742). Unfortunately, I can't quite decipher the solution (http://trac.macports.org/projects/macports/browser/branches/release_1_6/base/portmgr/dmg/postflight), I understand what needs to be done...just not sure how. Any suggestions?