Skip to content

Using MacPorts(DarwinPorts) to install Ruby on Rails, MySQL, Subversion, Capistrano and Mongrel on Mac OS X

Posted on 07 January 2007

Update: Leopard comes with Rails installed. Check out my quick guide.

Originally I installed Ruby on Rails on my MacBook by compiling from source, closely following the Hive Logic guide. However, I found that I couldn't deploy using Capistrano because of Open SSL errors.

While I was unable to pinpoint the exact cause of the error I did discover, during my search for a solution, that my Ruby on Rails installation wasn't particularly flexible or easy to debug.

I also discovered that probably the easiest way to to fix this error would be to re-install OS X. As it goes, this was no bad thing because during the process I was able to customise my installation and opt out of installing a lot of software the comes pre-installed on OS X out of the box. In fact so much so that I was able to free up about 6-7gb of space that I wouldn't have otherwise had.

Why MacPorts

My inspiration for using MacPorts comes largely from James Duncan Davidson who actually helped write the Hive Logic article. He talks about the advantages of the MacPorts installation method in his post Sandboxing Rails With MacPorts.

MacPorts (formerly known as DarwinPorts) is a package manager that will download, compile and install software, while installing any required dependencies, automatically. You can easily upgrade or deactivate packages you download, it just makes managing everything that much easier.

Packages are installed to /opt/ instead of /usr/local/ which, as Evan Weaver points out, means even if the port uninstall command fails, you can just drag the opt folder into the trash if things go really wrong and start from scratch.

Installing Xcode tools and MacPorts

Firstly I installed the Xcode tools from the OS X installation cd. I then went and installed the MacPorts package.

To finish the MacPorts installation process you need to setup your path environment variable to tell the system where to look for executable files that aren't run with an explicit path. I made my changes in /etc/profile to make them system wide, but you could make the changes in ~/.bashrc to make them user specific.

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

Remember to restart your terminal session to see the path changes.

I then ran the following to update the portfile definitions and update MacPorts if needed.

sudo port selfupdate

Install MySQL, Ruby and Subversion

Now MacPorts comes into its' own as I only needed to run the following commands to install MySQL 5, Ruby and Subversion.

sudo port install mysql5 +server
sudo port install ruby
sudo port install rb-rubygems
sudo port install rb-termios
sudo port install rb-mysql
sudo port install subversion +tools

You can run port installed to see the packages and the versions that are installed.

I found I had to add a symbolic link to the mysql5 mysqld.sock file because MacPorts puts it in /opt/local and therefore it couldn't be found at /tmp/mysql.sock.

sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock

To make sure Mysql5 starts up whenever I reboot my machine I ran the following:

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

It's also recommended to setup MySQL Native Bindings for a performance boost on the MySQL adapter:

sudo mysql_install_db5 --user=mysql 

When using svn via svnX GUI I found that I had to change the path in the preferences from /usr/local to /opt/local/bin To find out where svn is installed type which svn on the command line.

Install Rails, Capistrano and Mongrel

Now that Ruby is installed I installed Rails and friends as gems:

sudo gem install -y rake
sudo gem install -y rails
sudo gem install -y capistrano
sudo gem install -y mongrel
sudo gem install -y mongrel_cluster

Now my Ruby on Rails installation is easily managed and flexible. I can upgrade, deactivate or uninstall my software by doing:

sudo port upgrade packagename
sudo port deactivate packagename
sudo port uninstall packagename

View the snippets archive ››

34 comments made

Ruben commented on 15 Feb 07 at 02:38

Very useful, thanks for the easy to digest step by step guide. I agree that while the HL guide is useful, I find Mac/Darwin/Cheese Ports is much easier to use (as well as far easier upgrade). I’ve read some people have had issues installing some of the components with MacPorts but I haven’t had any issues. Being a FreeBSD-x86 user I just feel more comfortable using a ports system, and if worse comes to worse out with the /opt folder and just start again. Cheers – Ruben

Keith commented on 17 Mar 07 at 16:38

I have a question for you. How do you decide between a port install or a gem install when both options are available?

Did you have a specific reason for installing termios and the mysql bindings as ports instead of gems?

Paul commented on 20 Apr 07 at 06:29

I went down the MacPorts route as, from what i’d read, it appeared to be the most successful and flexible method for installing Rails.

It’s certainly worked out well for me – no issues at all thus far.

Doug commented on 02 May 07 at 13:25

Thanks for this tutorial, I’m almost done with my installing everything.

However, when I ran this command:
launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist

I get this returned:
No such process
ERROR!
Workaround Bonjour: Unknown error: 0

Any helpful hints?

Thanks,
Doug

Paul commented on 02 May 07 at 13:38

I haven’t personally come across that error but it appears you are not alone:
http://blog.nanorails.com/articles/2006/07/11/installing-rails-on-mac-os-x-tiger
If you search that page for ‘bonjour’ you will see some other people debugging it.

On the whole though it appears the error is relatively harmless.

John commented on 06 May 07 at 00:41

Set Bonjour in your agent’s .plist; man launchd.plist for more:

Sockets

Bonjour
...

John commented on 06 May 07 at 00:46

It looks like my snippet was stripped of useful tags:-)

In English: set the Bonjour key to false in the Sockets portion of the .plist.

kj commented on 09 Jun 07 at 14:10

hi,

i followed these instructions while doing a newbie install on mac osx…:)

BUT i keep getting..

“kjs-macbookPRO:~ kj$ mysql5
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ’/opt/local/var/run/mysql5/mysqld.sock’ (2)

when i try to run mysql5…

can you please help? totally confused and lost…

KJ :)

Paul commented on 09 Jun 07 at 16:06

Hi KJ,

This link may help: http://forums.macosxhints.com/archive/index.php/t-63628.html

A guy on there explains the error because your user doesn’t have the permissions to use the various mysql files.

I’m not sure how that might have happened for you but it’s certainly worth looking into.

Nathan Nash commented on 13 Jun 07 at 00:02

when attempting to install mysql5 +server i get Error: Unable to execute port: invalid command name “configure.universal_args”

what does this mean?

newbie

Paul commented on 13 Jun 07 at 03:25

Hi Nathan,

If command line doesn’t understand the port command then i’d say that sounds like your mac ports isn’t installed/setup correctly.

I would check that your path was setup correctly in /etc/profile as this ensures your system knows where to look for executable files when using the port command.

Ariejan commented on 11 Aug 07 at 11:25

Thanks for the write-up. It saved me a lot of time :) Thanks!

Steven commented on 16 Oct 07 at 11:09

Pete says:

The SVNX path is not quite right. It needs to be /opt/local/bin

Paul commented on 17 Oct 07 at 02:44

Cheers for the heads up! I’ve updated the article.

Rich commented on 31 Oct 07 at 05:27

Does this all work OK with OS X Leopard?

Paul commented on 31 Oct 07 at 06:03

I haven’t got Leopard myself, however, given that Rails is pre-installed, I believe a lot of these steps would be unnecessary.

When I do get Leopard I’ll be sure to post some updated instructions.

Nick Poulden commented on 01 Nov 07 at 06:05

I’ve just installed using these instructions on Leopard and everything works fine.

Paul commented on 01 Nov 07 at 07:04

That’s great to hear, I guess if you still want everything managed by macports then the installation process doesn’t change.

I have also read that you can upgrade to Leopard and retain your mac ports rails install by simply making the change in your path environment variable again. i.e. to make it look in /opt/local/bin

Rich commented on 01 Nov 07 at 09:53

Great! I’m eagerly awaiting Leopard to arrive in the post!

Arik Jones commented on 10 Nov 07 at 17:31

You have so helpful to my rails learning curve. Seriously, you have some of the most useful “noob” friendly material out there. Keep it up man!

JTigger commented on 02 Dec 07 at 23:05

First off: thank you, thank you, thank you!!! Very concise and easy to follow.

One bit of feedback, I ran into problems at the MySQL. After some thrashing, I realized that if I ran them in reverse order, it all flowed; and I added a sudo to the launchctl:

1. sudo mysql_install_db5—user=mysql 2. sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist 3. sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock

Again, super helpful! Thanks for putting this together!

Quint commented on 13 Dec 07 at 17:22

Thank you for the instructions and your other contributions.

I hope you could provide a bit of direction on completing the installation process. It would appear that while installing the subversion +tools, something has halted the process (at least, nothing has happened in the terminal window in the last 2 hours or so). I have pasted below all text generated after executing “sudo port install subversion +tools”.

P.S. I did navigate to the oracle URL (in Safari) noted in the last line below which resulted in a 404 error. Maybe the patch that’s being fetched isn’t located there anymore?

QMacBook17:~ Quint$ sudo port install subversion +tools
> Fetching apr
> Attempting to fetch apr-1.2.12.tar.bz2 from http://www.apache.org/dist/apr
> Verifying checksum(s) for apr
> Extracting apr
> Configuring apr
> Building apr with target all
> Staging apr into destroot
> Installing apr 1.2.12_0+darwin_9
> Activating apr 1.2.12_0+darwin_9
> Cleaning apr
> Fetching db44
> Attempting to fetch patch.4.4.20.1 from http://www.oracle.com/technology/products/berkeley-db/db/update/4.4.20/
> Attempting to fetch patch.4.4.20.2 from http://www.oracle.com/technology/products/berkeley-db/db/update/4.4.20/

Thank you!

Adam Conroy commented on 17 Dec 07 at 23:13

After 3 hours of struggling with compiling readline and sudo issues with my ruby binaries I decided to cave and do it through MacPorts.

OMG if I only did this when I first set out…I could have a Rails app completed by now or something with how long I dealt with this! Thank you!

Simon Starr commented on 25 Jan 08 at 04:33

(Posted in case anyone else has the same problem!)

I ran into trouble when installing subversion + tools – the sqlite3 install fell over with the following error:

Error: Target org.macports.build returned: shell command ” cd ”/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_databases_sqlite3/work/sqlite-3.5.4” && gnumake all ” returned error 2

A quick Google lead me to this thread, where running “sudo port clean—all sqlite3” was recommended.

Once I’d done that, I tried installing subversion again and everything was fine.

Thanks for a great article :)

john Plumridge commented on 27 Jan 08 at 03:17

b: Keith commented on 17 Mar 07 at 16:38 I have a question for you. How do you decide between a port install or a gem install when both options are available?

That’s a good question: Answer=?

john commented on 27 Jan 08 at 16:39
hi, i’ve been trying to work rails but gone through many install/uninstall, and now Iam stuck….. thats wat my terminal looks like as of now…...

s01060011243685b0:~ alihan$ sudo gem uninstall rails
Password:
ERROR:  While executing gem ... (Gem::InstallError)
    Unknown gem rails->= 0
I was trying to uninstall and get macports to do a new installation. please help me. Thats on my mac os x tiger g4 power pc.
MattyT commented on 29 Jan 08 at 21:15

It’s mentioned in some of the comments but, to be clear, launchctl needs sudo:

sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
Abhimanyu Chirimar commented on 05 Feb 08 at 00:09
Hello Paul, Thank for this great guide – Like Quint, I am stuck at this line as well sudo port install subversion +tools Its been doing this for about 9 hours and I don’t quite know what to do here. Any help is appreciated – All I see is :
> Fetching db44
> Attempting to fetch patch.4.4.20.1 from http://www.oracle.com/technology/products/berkeley-db/db/update/4.4.20/
Paul commented on 05 Feb 08 at 03:20

I have had a similar problem with the subversion just not installing on various machines. I can’t remember exactly where it’s stopped before though.

Usually if it’s taken more than an hour without showing any signs of completing I just stop it run the line again.

I know it’s not very scientific but eventually it has always managed to install.

andy commented on 03 Mar 08 at 19:23

wow, thanks for the write-up!

louie commented on 25 Mar 08 at 02:01

Does? macports automatiacally install org.macports.mysql5.plist?

here? /Library/LaunchDaemons/org.macports.mysql5.plist

Tyler commented on 25 Mar 08 at 12:39

I’m hurting really bad here, I’ve been at it for days now and I can’t get the /private/tmp/mysql.sock to stick around. I’ve tried some chmod 777 and 1777 commands on just about everyone of the files and folders involved and I can’t figure this out:

After I start the MySQL server, I create the symbolic link with the ‘sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock’ command.

It creates the proper link and everything is hunky-dory, but once I restart my machine, the /tmp file no longer exists in the /tmp directory. This happens if the LaunchDaemon launches MySQL or I do explicitly. The file just disappears when I log off and I have to re-create it each time I want to access MySQL from an app that uses the default install.

Any help would be extremely appreciated.

Bill Heaton commented on 17 May 08 at 05:10

I found this site by following a tutorial at :

http://www.buildingwebapps.com/articles/17-setting-up-rails-on-leopard-mac

I noticed that some comments were asking about leopard related updates on this post. Anyways I thought it may help as an addition reference for mac 10.5 users.

Simon Starr commented on 25 Sep 08 at 06:44

@Tyler, it may be a few months too late for you but I’ve been having the same issue. Today I finally got fed up enough to do something about it and wrote some code to create a shortcut to mysql.sock in /tmp/ at every boot using launchd.

I’ve written some brief instructions here

Got something to say?

About

Paul is a web developer for Kyanmedia web agency. He's lucky enough to write in Ruby on Rails full-time and uses this site to post snippets of code.

Contact

my name at gmail.com

More snippets

Take a look in the archive

Need a website?

Contact my employer. Make sure to check out our portfolio of work.

Hosting

I recommend hostingrails.com