In this article I detail how I installed my ruby/rails/postgresql development environment on a fresh install of Snow Leopard.
After installing Snow Leopard, be sure to install XCode.
For my development environment, I'm a big Macports fan. Leading up to the Snow Leopard release there were still a few ports that didn't compile. Most of the ones I use for Ruby and Rails work fine now though. The Macports team keep an updated list of broken ports here.
Before continuing, download the latest release here and install it. To keep your Macports up to date, run the following command line:
sudo port -v selfupdate
Macports will create a new file ~/.profile which includes the following line:
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
This ensures that all the executables installed using Macports work from the command line.
Type the following into the command line and go make some coffee, it'll take a while. I've chosen to add subversion support as well.
sudo port install git-core +svn
You'll want to create an SSH key so that you can pull from remote repositories.
You'll need to create a ~/.ssh directory, generate a rsa key, and copy the contents of the public certificate to add to GitHub/CodebaseHQ or other service.
mkdir ~/.ssh ssh-keygen -t rsa cat ~/id_rsa.pub | pbcopy
Depending on your needs, you can just use the default ruby that comes with Snow Leopard. It ships with ruby 1.8.7 patchlevel 72. I prefer to use Macports since they usually are more up to date. At the time of this article, it uses 1.8.7 patchlevel 174.
sudo port install ruby sudo port install rb-rubygems
Type the following into the command line:
sudo port install postgresql84 postgresql84-server
Next we setup the default database.
sudo mkdir -p /opt/local/var/db/postgresql84/defaultdb sudo chown postgres:postgres /opt/local/var/db/postgresql84/defaultdb sudo su postgres -c '/opt/local/lib/postgresql84/bin/initdb -D /opt/local/var/db/postgresql84/defaultdb'
To get Postgres to startup automatically, type in the following command:
sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql84-server.plist
If you want to start Postgres right now, type in the following command:
sudo su postgres -c '/opt/local/lib/postgresql84/bin/postgres -D /opt/local/var/db/postgresql84/defaultdb'
We probably want to be able to run commands such as psql without typing in /opt/local/lib/postgresql84/bin/psql all the time, to do so we can just add the directory to your ~/.bash_profile.
export PATH=/opt/local/bin:/opt/local/sbin:/opt/local/lib/postgresql84/bin:$PATH
Enter the above first into the terminal, if you want to start running some other Postgres commands.
When Postgres is installed, a new user called postgres will be created. If you don't want to be typing in that username all the time when you are trying to access your databases, just add yourself as a postgres user:
createuser --superuser macusername -U postgres
Next we install the ruby Postgres Gem.
sudo env ARCHFLAGS="-arch x86_64" gem install pg
I tend to find that I need multiple domain names when I'm developing. For example, I might have multiple web applications that need to talk to each other. You can either add extra entries in your /etc/hosts file like so:
127.0.0.1 mylocaldomain1.com 127.0.0.1 mylocaldomain2.com
which requires you to still add the port number in your browser mylocaldomain1.com:3000 or you can add entries into your apache config file. I'm pretty lazy, and I prefer to use GUIs to add/remove domains. Passenger with the Passenger Preference pane is a good way to go. Type in the following in the terminal and follow the instructions.
sudo gem install passenger sudo passenger-install-apache2-module
You'll need to add the following lines your apache configuration file to use Passenger.
LoadModule passenger_module /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.5/ext/apache2/mod_passenger.so PassengerRoot /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.5 PassengerRuby /opt/local/bin/ruby
You can make this system wide or just for yourself.
/etc/apache2/httpd.conf # system wide
/etc/apache/users/{username}.conf # just your user
Next we install the passenger preference pane. This provides a nice GUI to add domains and point them to the right application without create vhost files. You can find the latest version of the preference pane rvm written by Wayne Seguin which does this in a nice way.
sudo gem install rvm rvm-install
It currently supports MRI 1.8.6, MRI 1.8.7, MRI/YARV 1.9.1, MRI/YARV 1.9.2, REE 1.8.6, jRuby and Rubinius. To switch to a new ruby just type in the following command (it will install it, if you don't already have it installed).
rvm use ruby -v 1.9.1
If you want to do any type of image manipulation with Rails, you probably want to install RMagick. Macports has a neat port that installs both ImageMagick, support image libraries and RMagick all in one go.
sudo port install rb-rmagick