Using MacPorts(DarwinPorts) to install Ruby on Rails, MySQL, Subversion, Capistrano and Mongrel on Mac OS X
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
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
32 comments made
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
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?
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.
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
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.
Set Bonjour in your agent’s .plist; man launchd.plist for more:
Sockets
Bonjour
...
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.
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 :)
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.
when attempting to install mysql5 +server i get Error: Unable to execute port: invalid command name “configure.universal_args”
what does this mean?
newbie
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.
Thanks for the write-up. It saved me a lot of time :) Thanks!
Pete says:
The SVNX path is not quite right. It needs to be /opt/local/bin
Cheers for the heads up! I’ve updated the article.
Does this all work OK with OS X Leopard?
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.
I’ve just installed using these instructions on Leopard and everything works fine.
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
Great! I’m eagerly awaiting Leopard to arrive in the post!
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!
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!
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!
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!
(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 :)
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=?
It’s mentioned in some of the comments but, to be clear, launchctl needs sudo:
> Fetching db44
> Attempting to fetch patch.4.4.20.1 from http://www.oracle.com/technology/products/berkeley-db/db/update/4.4.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.
wow, thanks for the write-up!
Does? macports automatiacally install org.macports.mysql5.plist?
here? /Library/LaunchDaemons/org.macports.mysql5.plist
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.
Got something to say?