DaveSouth.org

Email RSS Twitter Delicious

Install Ruby on Rails using MacPorts

There are several ways to set up a system for Ruby on Rails development. Leopard comes pre-installed with Ruby and Rails and is perfectly fine for most users. For me, however, I kept running into troubles. Trying to do a free standing install of graphic libraries (FreeImage or ImageMagick) proved too painful. I switched back to using MacPorts.

Sandbox

Much of what I’ve done is based on the work of Dan Benjamin at Hivelogic. Whether you compile manually (as Dan suggests) or use MacPorts, the goal is the same — to create a sandbox for your Ruby on Rails environment.

These instructions are meant to be quick and to the point. I won’t be going into great detail as to why I’ve done what I’ve done. There are many articles that explain why much better.

XCode

Be sure to install XCode tools from the Leopard installation DVD.

MacPorts

Download the latest version of MacPorts and install.

Path

Add these lines to your ~/.profile or ~/.bashrc document:

  export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
  export MANPATH="/opt/local/share/man:$MANPATH"

Log out of the terminal session and begin a new one so the changes take hold.

Ports

I just install the necessary ports in one command. This will take some time as it compiles all packages from source. So go take a break.

  sudo port install mysql5-server git-core tiff -macosx imagemagick +q8 +gs +wmf ruby rb-rubygems rb-termios rb-mysql

MySQL

Initialize MySQL database and install MySQL 5 startup. I also create a mysql link to the mysql5 binary.

  # Initialize MySQL database
  sudo mysql_install_db5 --user=mysql
  # Set MySQL to start with computer
  sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
  
  # Set mysql link to mysql5 binary for convenience
  cd /opt/local/bin
  sudo ln -s ../lib/mysql5/bin/mysql mysql

Secure MySQL

  sudo mysql_secure_installation5

  # Hit enter for current root password (because one hasn't been set, yet)
  # When it asks if you want a root password, it's up to you. A development machine probably doesn't need one.
  # Yes, you do want to remove anonymous users
  # Yes, you want to disallow root login remotely
  # Yes, remove test database
  # Yes, reload privilege tables now

GEMs

I never use the RDocs and RI files generated when installing gems. So I edit my ~/.gemrc file and add gem: --no-ri --no-rdoc at the end of the file. It will then add those options every time I run gem. You don’t have to do it, but it does speed up installation immensely if you don’t need local documentation.

As of this writing, rubygems is at version 1.3.5. MacPorts installs version 1.3.1. So it needs to be upgraded. Also add github as a valid gem repository. Then run a single entry to load all rubygems at once.

  # Upgrade rubygems to latest version
  sudo gem update --system

  # Install rails and other useful gems
  sudo gem install rake rails capistrano \
    capistrano-ext mongrel redgreen pdf-writer \
    ZenTest autotest-rails ruby-debug haml rcov \
    rr RedCloth chronic will_paginate aws-s3 net-dns \
    paperclip shoulda subdomain-fu vpim hoptoad_notifier

MySQL Gem

The ruby based MySQL connecter is gone in Rails 2.2.2. I did have instructions on how to compile the mysql gem, but it was so slow compared to the ruby based connector. It should be the other way around.

In the past, I couldn’t get Macports to install the rb-mysql port, but that’s changed. It works great. I added it to the ports list above. It is the fastest connector I’ve used.

Shoulda

I’ve tried RSpec, test-spec-on-rails and Shoulda. Of the three, Shoulda is the best. Yet I’ve had problems with it and there are others having second thoughts on these testing additions. Dan Croak wrote an excellent article about the problems: A critical look at the current state of Ruby testing

I do like the change in Rails 2.2.2 so you no longer have to write “test_should_pass_because_i_am_typing_snake_case”. Instead, you type a nice block.

  test 'should test something in my program' do
     assert this
     assert that
  end

There is one very useful aspect to Shoulda that I don’t want to lose — macros. I love them. Very nice bits of code to test common problems in Rails. So I still install Shoulda and use the macros.

Check out Shoulda’s documentation.

SSH Warning

Git is taking over source control management. After using it for several months, I understand why. It’s a huge jump in managing code and worth learning. However, installing git-core caused an unexpected problem.

Git-core depends on SSH, so MacPorts dutifully installs the SSH port. Because we changed our execution path to use /opt/local/bin before /usr/bin we will be using MacPorts version of ssh before we use Leopard’s version. This isn’t a problem except when you want to add your ssh key to the Leopard ssh agent.

Leopard loads a copy of ssh agent and can manage your ssh-keys on the secure Leopard keychain. This happens the first time a particular ssh-key is used to authenticate a connection. BUT only if you use Leopard’s version of ssh to make that connection.

What happened to me was that I had to create a new ssh-key. I did, loaded it on the servers, and tried connecting. But Leopard never asked to save the passphrase to the new key. To get around this I used the absolute path to Leopard’s ssh (/usr/bin/ssh) when making the connection. A Leopard dialog box appeared and asked for my key and then asked to save the passphrase in my login keychain.

After that, the MacPorts ssh binary works with Leopard’s ssh-agent. So it’s only a nuisance when you change or add keys.

Changelog

  • Friday, 4 April 2008 – Added ‘rr’ to gem install list.
  • Monday, 14 April 2008 – Updated git-core install.
  • Friday, 4 July 2008 – Added improved RedCloth and will_paginate gems. Converted port install to only one line. Added Shoulda plugin. Added SSH warning.
  • Monday, 1 September 2008 – Updated RedCloth to pull from standard GEM repository now that RedCloth is update to version 4.
  • Monday, 8 September 2008 – Removed rb-mysql from ports list. Added how to install MySQL gem on Leopard.
  • Saturday, 11 October 2008 – Changed shoulda from plugin to gem
  • Sunday, 21 December 2008 – Added rb-mysql to ports install, changed Shoulda recommendations
  • Thursday, 15 January 2009 – Added capistrano-ext to the gem list for multi-stage capistrano deployments
  • Friday, 16 January 2009 – Added github.com as a gem repository. Added thoughtbot-shoulda and thoughtbot-paperclip to gem installation.
  • Thursday, 22 January 2009 – Added subdomain-fu to the gem installation.
  • Tuesday, 7 April 2009 – Removed freeimage macport and added imagemagick macport
  • Tuesday, June 9, 2009 – Update includes mysql5-server port and autotest-rails gem.
  • Friday, October 2, 2009 – Added aws-s3 and removed right-aws gems.
  • Friday, December 18, 2009 – Changed from github to gemcutter.
  • Tuesday, May 25, 2010 – Removed adding gemcutter as gem source