Setting Up Rails and PostgreSQL on Snow Leopard
Hello again. This post will go over how to set up the Ruby language, the Ruby on Rails web framework, and the PostgreSQL relational database server on an Intel Mac running Mac OS X 10.6 (Snow Leopard).
Note: As the Perl folks say, there’s more than one way to do it. This is only one of many possible configurations and ways to set things up. I’m writing this post (and the next two or three) for my future self and anyone else who might want to get up and running with Rails this way.
In the next few posts, we will: create a simple database-driven CRUD web application in Rails; set up a source repository on GitHub and push our code there; create a server on the Rackspace Cloud, set it up for production use, and finally deploy our app from GitHub to that server.
This post is really an adaptation of a nice guide written by Jared McFarland. His setup includes MySQL rather than Postgres, and so my post will only modify and elaborate on his where appropriate.
Let’s begin.
MacPorts
The first thing we want to get is MacPorts, a system by which we can easily get the latest, most widely-used versions of Ruby, RubyGems (a Ruby software packaging system), Ruby on Rails, and PostgreSQL. (Snow Leopard does come with versions of Ruby, Gems and Rails, but things move fast, and they were already outdated when Snow Leopard was released.)
Go here and get the latest DMG disk image for Snow Leopard (1.8.1 at this writing). Install the PKG. (Note: MacPorts will put itself and everything else you install via MacPorts in the /opt folder on your primary hard disk, so you’ll know where everything is in case you want to get rid of it later.)
Ruby
[Update, 12/21/2009, 4:30 p.m. MST: Marc Chung tells me that a better way to install Ruby than that described in this section is via rvm (Ruby Version Manager), which allows you to have multiple Rubies installed and switch between them. More details forthcoming.]
In Terminal, do:
sudo port install ruby
and enter your user account password if/when prompted. This will install the latest Ruby 1.8, which at this writing is 1.8.7, patchlevel 174, dated June 12, 2009.
When it’s done, do:
which ruby
and you should see:
/opt/local/bin/ruby
Now, do:
ruby -v
and you should see:
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10]
RubyGems
sudo port install rb-rubygems
Latest RubyGems on MacPorts is 1.3.4. (1.3.5 is the actual latest and is coming to MacPorts Real Soon Now).
SVN and Git
sudo port install subversion
sudo port install git-core +svn
If you don’t have any existing Subversion repositories, you can ignore the subversion line and omit “+svn” from the git-core line. If you’re starting from scratch, you only need Git. Latest Git on MacPorts is 1.6.5.3 (actual latest 1.6.5.6).
PostgreSQL
sudo port install postgresql84-server
Latest PostgreSQL is 8.4.2, and it’s on MacPorts.
[Update, 12/24/2009, 12:30 am MST: Add the following line to the end of your ~/.profile file:
export PATH="/opt/local/lib/postgresql84/bin:$PATH"
Then issue the following command from the prompt:
source ~/.profile
The PostgreSQL binaries need to be in your PATH; if they aren't, the next step will fail.]
That’s all for the MacPorts stuff. Now for some Ruby Gems.
PostgreSQL Ruby Adapter
sudo gem install pg
Latest PostgreSQL Ruby Adapter gem (pg, a.k.a. ruby-pg) is 0.8.0.
Rails
sudo gem install rails
Latest Rails is 2.3.5. This includes eight gems — actionmailer, actionpack, activerecord, activeresource, activesupport, and rails (all 2.3.5); rack (1.0.1); and rake (0.8.7).
You now have all the software you need to start developing web applications using Ruby on Rails on your Snow Leopard Mac!
Be sure to tune in next time, for another exciting episode of Jeremy’s Wild and Crazy Adventures on (off?) the Rails, when we’ll discover how to create a basic CRUD app in Rails without writing a single line of Ruby code! (Before long, we’ll want to develop apps by actually writing some Ruby code, but more on that later.) We’ll also set up Git repositories in which to store and track our code revisions, both locally and on GitHub. See you then!