Installing Ruby on Rails and PostgreSQL on OS X, Third Edition 36
Over the past few years, I’ve helped you walk through the process of getting Ruby on Rails up and running on Mac OS X. The last version has been getting a lot of comments related to issues with the new Apple Leopard, so I’m going this post will expand on previous installation guides with what’s working for me as of January 2008.
The following guide is how our development team at Planet Argon prefers to setup our development workstations
During this installation, we’ll have what we feel is the optimal development stack for building Ruby on Rails applications with our favorite database server, PostgreSQL.
Ready? Let’s get started…
Phase One
During this initial phase, we’re going to install the underlying dependencies that we’ll be building off of.
XCode 3.0
The first thing that you’ll need to install to get far with this process is XCode tools, which is distributed by Apple. You can find this on the DVD that your Leopard installer is on. You can also download the latest version from Apple’s developer site.
The current version (3.0) is 1.1 GB.. so the download time will vary depending on your connection speed. I would encourage you to drink some tea and/or read a book
Once you finish the installation, you can move forward. The rest of these installation will not work until XCode is installed. :-)
MacPorts
In this next step, we’ll install MacPorts (formerly known as DarwinPorts). The MacPorts web site describes itself as, “an open-source community initiative to design an easy-to-use system for compiling, installing, and upgrading either command-line, X11 or Aqua based open-source software on the Mac OS X operating system.”

This tool is about to become one of the most important tools on your operating system as it’ll be used time and time again to maintain your libraries and many of the Unix tools that you’ll be using. If you’re from the Linux or BSD world, you are likely familiar with similar tools… such as: apt-get, port, and yum.
First, you’ll want to download MacPorts and install the “dmg” disk file for Leopard at the following link.
Once downloaded, you’ll want to run the installer and install it on your workstation.
Work you way through the installer until successfully installed.
Once this finishes, you can open up your favorite terminal application and run the following to test that it installed properly.
In my case, I’m now using Terminal.app.
Issue the command: /opt/local/bin/port version
If it responds with a version number like mine did in the screenshot above, we’re moving along nicely.
Environment Paths
When we install MacPorts, the command to install/update ports installed to /opt/local/bin. We had to provide the entire path as this isn’t currently showing up in the default $PATH on Leopard. Let’s quickly remedy this by modifying the file /etc/profile.
If you have Textmate installed, you can run the following from your terminal: mate /etc/profile
Add the following line to the bottom of /etc/profile.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
You can use your favorite editor to update this file. Once you save it, you’ll want to restart your terminal application (or open a new tab) to create a new session. When your new terminal opens, run the following to verify that port is showing up in your $PATH.
which port
You should see /opt/local/bin/port show up as the result of this command.
Great, let’s continue to move forward.
Hiding Apple’s Ruby, Gems, and Rails
Before we install Ruby from MacPorts, we’ll go ahead and hide Apple’s Ruby installations.
:~ robbyrussell$ sudo su -
Password:
:~ root# mv /usr/bin/ruby /usr/bin/ruby.orig
:~ root# mv /usr/bin/gem /usr/bin/gem.orig
:~ root# mv /usr/bin/rails /usr/bin/rails.orig
:~ root# logout
If you ever decide to remove MacPorts, you can just rename ruby.orig back to ruby and you’re back where you started… and the same for the others listed.
Phase Two
During this next phase, we’re going to install Ruby and Ruby on Rails.
Installing Ruby via MacPorts
Now that we have MacPorts up and running, we’re going to use it for the first time. We’ll start by using it to install Ruby and the Rubygems package.
$ sudo port install ruby rb-rubygems
Okay, this will take a little while. I’d suggest that you step out to get some fresh air.
How was it outside? What’s the weather like there today? It’s currently 2:30am PST so it’s dark and an 28F outside so I didn’t stay outside very long.
If you’re still waiting for it to install, perhaps you could watch the following video. I might encourage you to check out more of Jam, which was recommended a few years ago to me by James Adam at Canada on Rails.
Be warned… it’s a strange show, but I find strange things like this funny. :-)
If you prefer something a bit more lighthearted…
Okay… when Ruby finishes installing, you’ll want to test that you can run it.
$ ruby -v
Great, let’s move forward!
Installing Ruby on Rails via RubyGems
We’re now going to install the libraries that make up Ruby on Rails via RubyGems.
$ sudo gem install --include-dependencies rails
This will install the following gems.
- rails-2.0.2
- rake-0.8.1
- activesupport-2.0.2
- activerecord-2.0.2
- actionpack-2.0.2
- actionmailer-2.0.2
- activeresource-2.0.2
Excellent, let’s move forward…
If you haven’t already purchased it, I recommend that you take a look at The Rails Way (Addison-Wesley Professional Ruby Series) by Obie Fernandez.
Installing Mongrel via RubyGems
Let’s now install Mongrel, which is an excellent Ruby-based web server for Ruby on Rails applications. We use it in development and production at Planet Argon and it’s also what we recommend to our hosting customers.
$ sudo gem install --include-dependencies mongrel mongrel_cluster
- Note: Be sure to select the proper platform for mongrel. (hint: OS X is NOT mswin32)
Select which gem to install for your platform (i686-darwin9.1.0)
1. mongrel 1.1.3 (java)
2. mongrel 1.1.3 (i386-mswin32)
3. mongrel 1.1.3 (ruby)
4. mongrel 1.1.2 (ruby)
5. mongrel 1.1.2 (mswin32)
6. mongrel 1.1.2 (java)
7. Skip this gem
8. Cancel installation
> 3
Select which gem to install for your platform (i686-darwin9.1.0)
1. fastthread 1.0.1 (mswin32)
2. fastthread 1.0.1 (ruby)
3. Skip this gem
4. Cancel installation
> 2
Building native extensions. This could take a while...
Building native extensions. This could take a while...
Successfully installed mongrel-1.1.3
Successfully installed gem_plugin-0.2.3
Successfully installed daemons-1.0.9
Successfully installed fastthread-1.0.1
Successfully installed cgi_multipart_eof_fix-2.5.0
Installing ri documentation for mongrel-1.1.3...
Installing ri documentation for gem_plugin-0.2.3...
Installing ri documentation for daemons-1.0.9...
Installing ri documentation for fastthread-1.0.1...
No definition for dummy_dump
No definition for dummy_dump
No definition for rb_queue_marshal_load
No definition for rb_queue_marshal_dump
Installing ri documentation for cgi_multipart_eof_fix-2.5.0...
Installing RDoc documentation for mongrel-1.1.3...
Installing RDoc documentation for gem_plugin-0.2.3...
Installing RDoc documentation for daemons-1.0.9...
Installing RDoc documentation for fastthread-1.0.1...
No definition for dummy_dump
No definition for dummy_dump
No definition for rb_queue_marshal_load
No definition for rb_queue_marshal_dump
Installing RDoc documentation for cgi_multipart_eof_fix-2.5.0...
Successfully installed mongrel_cluster-1.0.5
Great, you have almost all of the essentials.. except a database.
Phase Three
In this phase, we’re going to get our database server, PostgreSQL, installed and the libraries that Ruby needs to communicate with it.
Installing PosgreSQL with MacPorts
At Planet Argon, we design and develop our applications on top of PostgreSQL. I’ve been advocating the adoption of this awesome open source database in the Rails community for quite some time now.
The current version available of PostgreSQL via MacPorts is 8.3, which is what we’ll now install with the port command.
$ sudo port install postgresql83 postgresql83-server
This will download and install the necessary libraries to run PostgreSQL server and the client utilities.
Configuring PostgreSQL
When PostgreSQL is finished installing, it’ll tell you to run the following commands to create a new database instance.
sudo mkdir -p /opt/local/var/db/postgresql83/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql83/defaultdb
sudo su postgres -c '/opt/local/lib/postgresql83/bin/initdb -D /opt/local/var/db/postgresql83/defaultdb'
Adding PostgreSQL to launchd
If you’d like to have PostgreSQL automatically startup after a system restart, you can load it into launchd, which comes with OS X. By running the following command, PostgreSQL will startup automatically on the next system restart.
sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql83-server.plist
Adding PostgreSQL to your $PATH
For some reason, the MacPort for PostgreSQL doesn’t get the programs in your path automatically, so we’ll it now.
mate /etc/profile
Modify the PATH that we changed earlier to include /opt/local/lib/postgresql83/bin@.
export PATH=/opt/local/bin:/opt/local/sbin:/opt/local/lib/postgresql83/bin:$PATH
Save the file and then open a new terminal. To test this, you should get the following output when you run which psql.
$ which psql
/opt/local/lib/postgresql83/bin/psql
Creating a new PostgreSQL user
When I’m working on Rails applications in my development environment, I really don’t want to have to specify a username and password in every config/database.yml file for each of our ongoing client projects. When PostgreSQL was installed, it created a superuser named postgres, which is great, but I’d like one that matches my system username, so that I’m not prompted at all for a username or password to connect to PostgreSQL.
To do this, we’ll use the createuser command, which comes with PostgreSQL. As you can see, I’m creating a new user with superuser privileges (and will hopefully be the last time I have to do a -U postgres).
$ createuser --superuser robbyrussell -U postgres
CREATE ROLE
Let’s take a quick moment to test this out.
# create a new database
$ createdb my_test_db
CREATE DATABASE
# drop the database
$ dropdb my_test_db
DROP DATABASE
Great, everything looks good here.
We now have a running installation of PostgreSQL with a new user account. All we need to do now is install the appropriate RubyGem to allow our Ruby applications to connect to it.
Installing PostgreSQL Libraries for Ruby
You can install postgres gem by running the following command.
$ sudo gem install --include-dependencies postgres
Great. We’ve now built a professional development environment for working with Ruby on Rails. Can you feel the excitement? :-)
Closing Thoughts
Like the previous versions, I hope that a few people find this useful. I didn’t have to make a lot of changes from the second edition, but there were enough to warrant a new post. I’ve been setting up my workstation like this for about three years now and I’m looking forward to seeing how a fresh install on Leopard works out for me.
If you have any problems, feel free to ask a question in the comments below.
Enjoying the content? Be sure to subscribe to my RSS feed.











Thanks for the write-up!
What’s your rationale for installing Ruby via MacPorts? i’ve been using the Ruby that ships with Leopard with no problems so far (I’m not using PostgreSQL, though).
Huh. That video was… Strange.
You migth also want to create databases with UTF-8 support opposing the default sql_ascii, in which case use: createdb my_test_db -E UTF8
you then need to add “encoding: unicode” line to your databases.yml sections.
Excellent manual, as always:)
After going through the steps above, TextMate is having an issue finding Ruby.
env: ruby: No such file or directory
Any suggestions?
I am having that problem, too, from a slightly different set of config steps… Any help would be appreciated.
Scott, check your hash for ruby (type ruby). I had the same problem but after installing i quit and started my terminal and it works.
sudo gem install—include-dependencies postgres ERROR: While executing gem … (OpenURI::HTTPError) 404 Not Found
Anyone getting this error?
I think Scott’s problem might have actually been that the path to Ruby is hard coded in some of the scripts in TextMate. It expects it to be at /usr/bin/ruby, but it no longer is.
I repo’d it on my own system and fixed it just by doing a “sudo ln -s /opt/local/bin/ruby /usr/bin/ruby”.
That way anything hardcoded to the system default ruby instance will instead use the MacPorts build.
l3opard, Iam getting that error…
“Macintosh:~ stefandozier$ which psql /opt/local/lib/postgresql83/bin/psql Macintosh:~ stefandozier$ createuser Enter name of role to add: stefandozier Shall the new role be a superuser? (y/n) y createuser: could not connect to database postgres: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket ”/tmp/.s.PGSQL.5432”? Macintosh:~ stefandozier$” anyone have any ideas on how to solve this? everything has worked fine up until i try to createuser
Hi, First off very nice post, much needed. As you can imagine i still have problems this to get working. I’m using 10.4.11 on mac book pro. here’s the output from the mkmf.log. when i tried the last step gem install—include-dependencies postgres
table of contents for archive: /usr/local/lib/libruby-static.a is out of date; rerun ranlib(1)
looking at the time stamp of this file libruby-static.a, it look old sometime in 2007(maybe came with macos), so surely wasn’t installed by following these upgrade instructions.
so i did ranlib libruby-static.a and reattempted and it worked.
shouldn’t this file be updated with installing new ruby, any ideas ?
oops my mistake, posted several time. didn’t get any response when i hit it. sorry !
—include-dependencies is no longer needed with gem 1.0 since it’s now a default argument.
Thank you for your very clear instructions. I ran into the following checksum error when I tried to install PosgreSQL with MacPorts. I tried reinstalling by repeating the initial command, but always got the same checksum error. Can you tell me what to do?
paul-denlingers-macbook-pro:~ pdenlinger$ sudo port install postgresql83 postgresql83-server -> Fetching bison -> Attempting to fetch bison-2.3.tar.bz2 from http://ftp.gnu.org/gnu/bison -> Verifying checksum(s) for bison -> Extracting bison -> Configuring bison -> Building bison with target all -> Staging bison into destroot -> Installing bison 2.3_2 -> Activating bison 2.3_2 -> Cleaning bison -> Fetching libxml2 -> Attempting to fetch libxml2-2.6.30.tar.gz from http://xmlsoft.org/sources/ -> Verifying checksum(s) for libxml2 Error: Checksum (md5) mismatch for libxml2-2.6.30.tar.gz Error: Checksum (sha1) mismatch for libxml2-2.6.30.tar.gz Error: Checksum (rmd160) mismatch for libxml2-2.6.30.tar.gz Error: Target org.macports.checksum returned: Unable to verify file checksums Error: The following dependencies failed to build: libxml2 libxslt Error: Status 1 encountered during processing.
wonderful post. worked wonderfully well for me. keep up the good work!
I had to start Postgres after installing it before I could do any of the rest of the install, I used this command, lifted from the .plist file mentioned in the tutorial.
sudo /opt/local/etc/LaunchDaemons/org.macports.postgresql83-server/postgresql83-server.wrapper start
RobbyonRails, I followed your instructions on installing Ruby on Rails (2.0.2) on Leopard up to the point where instead of installing PosgreSQL I wanted to use MySQL which I had already installed.
I had an initial error on installing the MySQL drivers – $ sudo gem install mysql; then used – $ sudo gem install mysql—with-mysql-config=/usr/local/mysql/bin/mysql-config
This appeared to install mysql-2.7; however although I can see the MySQL database in the console, I am unable to view the records in the browser. Instead I get error messages like NoMethodError & You have a nil object when you didn’t expect it!
Please advise.
Quicksilver
Thanks Robby for putting together these guides. I’ve used them in the past but I’ve got hung up on a problem, I think with the PostgreSQL libraries.
When I try $ rake db:migrate I get the error no such file to load—postgres
I’ve also noticed that when I create and drop databases, nothing is printed to stdout. The databases get created but nothing is printed. This happens whether I am logged in as the postgres user or my own.
Thanks again for the guide.
How about some instructions on undoing all this? I really hate these ports installing a whole bunch of things wich I don’t want on my Mac. Feels like FreeBSD when after a long, long time the whole process just stops with a damn error message such as: Error: The following dependencies failed to build: readline Error: Status 1 encountered during processing. Ports really suck.
First off, macports doesn’t “suck.” It was designed with just that sort of concern in mind. If you want to be rid of all the ports you’ve installed, just delete the /opt folder.
Everything MacPorts installs gets installed there (including dependencies). It’s like its own little sandbox.
For the copy paste inclined:
sudo rm -rf /opt
make sure there’s NO space between / and opt, or you’ll be sorry!
First off, macports doesn’t “suck.” It was designed with just that sort of concern in mind. If you want to be rid of all the ports you’ve installed, just delete the /opt folder.
Everything MacPorts installs gets installed there (including dependencies). It’s like its own little sandbox.
For the copy paste inclined:
sudo rm -rf /opt
make sure there’s NO space between / and opt, or you’ll be sorry!
@Ken Robertson, or anyone
I’m concerned about Textmate’s Ruby env issue.
Will moving the macports version of Ruby into the default Leopard location cause any issues?
@Anyone Any good Postgres client’s for Mac? I love myself some good CLI, but I’d rather work with a front-end for database stuff.
for Textmate not finding ruby you have to go to Textmate Preferences, Advanced, Shell Variables and set this variable:
variable: TM_RUBY value: /opt/local/bin/ruby
And, Macports is Definitely is the way to go.
Check out my post at http://freegnu.blogspot.com/2008/03/rails_requires_rubygems_094_error_on.html if you want the way back from reinventing the wheel with MacPorts. I was in the process of uninstalling MacPorts packages that are already available in Leopard when I read the Apple Developer Connection article about ruby on rails already being installed on Leopard. I resolve some issues with having multiple versions of Ruby and RubyGems installed by uninstalling the MacPorts version. I find the easiest and quickest way to remove packages installed under /usr/local is to ls an rm by hand. It’s quicker than you might expect especially with ruby and rubygems as all the files have the word ruby or rb or gem in their name. You could just “find /use/local -name ‘ruby’ -or -name ‘rb’ -or -name ‘gem’” and delete anything in that list the looks like a file installed by ruby or gem.
Just curious—did you stop using iTerm? If so, why? I’m getting frustrated with iTerm keybindings….
Just curious—did you stop using iTerm? If so, why? I’m getting frustrated with iTerm keybindings….
@andy:
yeah, I switched to Terminal.app that ships with Leopard. It’s working great now that it has tabs. :-)
in case initdb claims about not being able to find a suitable encoding for the locale “UTF-8”, just extend the initdb command like here:
sudo su postgres -c ’/opt/local/lib/postgresql82/bin/initdb -D /opt/local/var/db/postgresql82/defaultdb -E UTF8’
Hi Robby, thanks a lot for this tutorial,
What if I don’t want PostgreSQL automatically startup after a system restart? how can I turn it on manually?
I finished following tutorial until that sentence while I’m stuck at googling and waiting for your answer/solutions.
Okay, so I liked this post a lot. But I’m looking for a nice recipe to publish a rails app under apache2 on leopard. Preferably without using damned fastcgi, whose /tmp/fcgi_ipc just will not hold its preferences between reboots for some reason, no matter what I tell it …
Robby? Anyone?
thanks greg! “sudo env ARCHFLAGS=”-arch i386” gem install postgres” saved my day!!
I just installed ruby via macports a moment ago, and it appears as if its missing the socket library altogether. This creates problems with rubygems, since it requires a socket to actually pull down gems. Anyone else seeing this problem?
I just installed ruby via macports a moment ago, and it appears as if its missing the socket library altogether. This creates problems with rubygems, since it requires a socket to actually pull down gems. Anyone else seeing this problem?
I just installed ruby via macports a moment ago, and it appears as if its missing the socket library altogether. This creates problems with rubygems, since it requires a socket to actually pull down gems. Anyone else seeing this problem?
Great tutorial – smooth like butter!