Jul 302011

Bye Bye Macports, Welcome Homebrew

Homebrew: The missing package manager for OS X

Why? well, the reality is, macports is not that good anymore.
Once you have many packages installed and start updating, everything start to break apart, lot’s of failing packages.

Homebrew is very easy to install, it’s fast and simple. That means you can make your own homebrew formula for your package so easily. oh, and homebrew is in ruby! :)

Back to topic, this is you how you get rails with mysql up and running with homebrew and rvm.

Clean up

To make sure to have a clean install, I recommend removing any previous .rvm installation and previous Xcode.

$ rm -rf ~/.rvm/
$ sudo rm -rf /Developer

1. Xcode

Install Xcode from AppStore. it’s 1+ GB download so it may take a while.
after it’s downloaded it will not install automatically, you need to open Applications and install again from there, the name will be “Install Xcode”.
You also need to install Command Line Tools for Xcode.

Better way is to go to https://developer.apple.com/downloads/index.action and download from there, you will have to login with a free apple developer account.
Download the 2 minimum require files

– Xcode 4.3.1 for Lion (1.85 GB)
– Command Line Tools for Xcode (171.70 MB)

Update: If you don’t want to download and install huge XCODE (3.0GB) :
It allows you to install the essential compilers, GCC, LLVM, etc.
PS: I have not tested it
Thanks JP for the tip.

2. Install HomeBrew

UPDATE: in the comments some people recommended to do create the folder “/usr/local/Cellar” before hand, due to some bug on homebrew.

mkdir -p /usr/local/Cellar
$ /usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"

Installation instructions: https://github.com/mxcl/homebrew/wiki/installation

3. install RVM

$  bash -s master < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

Above im using the master branch, so that it works with xcode 4.3.1

Then after RVM is installed run these two 'one-line' commands, the second command will reload your bash with RVM.

$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
$ source ~/.bash_profile

Details instructions: http://beginrescueend.com/rvm/install/

Note: you may have to add "--with-gcc=clang" to rvm for installing ruby 1.9.2 if you have Xcode 4.3+
Read this: http://www.arroyodelaluz.es/

4. Install ruby 1.9.3-p125

OS X Lion comes with Ruby-1.8.7-p249, but we all want ruby 1.9.2/1.9.3 right?
RVM head and Ruby 1.9.3-p125 supports XCODE 4.3.1 read more

$ rvm install 1.9.3-p125
$ rvm use ruby-1.9.3-p125
$ gem install rails bundler unicorn pg 
... and so on ...

I tested both ruby-1.9.3-head and ruby-1.9.3-p0, and 1.9.3-p125, and it works well with all my apps. Ruby 1.9.3 is faster than 1.9.2 booting rails, and way way faster than 1.8.7. So let's use the lastest Stable Ruby (1.9.3-p0)

See: http://ndwilson.com/.

NOTE: For Heroku I recommend you to use ruby-1.9.2-p290, if you use taps ("heroku db:pull/push")

Optionally you might want to install GIT, wget, Read Full Report, imagemagick and any other mighty software tools for daily use.

Example apps I'm usually required to install:

# brew install git ack wget curl redis memcached libmemcached colordiff imagemagick nginx sqlite libxml2 libxslt readline v8 rsync sphinx lzma geoip lzo 

5. Install Mysql

$ brew install mysql

one-line command:

$ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

Once mysql is installed you might want it to load automatically each time you start your mac.

$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/Cellar/mysql/5.5.14/com.mysql.mysqld.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

*check that the version I use here is 5.5.14

6. Troubleshooting:

if you have problems with mysql "cannot connect to /tmp/mysql.sock"
then create a file /usr/local/etc/my.cnf and add this:

port = 3306 
socket = /tmp/mysql.sock 
bind-address =
port = 3306 
socket = /tmp/mysql.sock 

if encounter errors with homebrew run this command and follow recommendations:

$ brew doctor

update: If you end up with Segmentation fault or cannot install Ruby-1.8.7, you might want to try sovereignty essay:

$ export CC=/usr/bin/gcc-4.2
$ rvm install ruby-1.8.7 

Important, also read this if you have Xcode 4.3.1+

by the way this is my /usr/local/etc/my.cnf optimized file, when using this file you may have to recreate your db

$ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
port = 3306 
socket = /tmp/mysql.sock 

event_scheduler = ON 
collation_server = utf8_unicode_ci 
character_set_server = utf8 

bind-address =
port = 3306 
socket = /tmp/mysql.sock 
max_connections = 20

table_open_cache = 256
max_allowed_packet = 32M 
binlog_cache_size = 1M 
max_heap_table_size = 64M 

read_buffer_size = 2M
read_rnd_buffer_size = 2M
sort_buffer_size = 4M
join_buffer_size = 512k
thread_cache_size = 2 
thread_concurrency = 2
query_cache_size = 16M 
query_cache_limit = 2M 

default-storage-engine = INNODB
thread_stack = 192K 
transaction_isolation = REPEATABLE-READ 
tmp_table_size = 64M 

# MyISAM Options 

key_buffer_size = 32M
bulk_insert_buffer_size = 32M
myisam_sort_buffer_size = 32M
myisam_max_sort_file_size = 256M
myisam_repair_threads = 1 

# INNODB Options
innodb_additional_mem_pool_size = 8M
innodb_buffer_pool_size = 64M
innodb_thread_concurrency = 2
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 8M
innodb_log_file_size = 8M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_flush_method = O_DIRECT
innodb_lock_wait_timeout = 120

max_allowed_packet = 16M 


key_buffer_size = 64M
sort_buffer_size = 64M
read_buffer = 16M
write_buffer = 16M


UPDATED (Mar 14, 2012):
* Fixed homebrew install URL
* changed from "#" to "$" to avoid confusion of running commands as root

EDITED (Feb 10, 2012):
* updated for new RVM
* source .bash_profile after editing it.
* decreased memory settings for mysql
* using ruby-1.9.3-p0
* fixed minor bugs

  80 Responses to “Installing Rails on OS X Lion with HomeBrew, RVM and Mysql”

  1. I just did a Lion install from scratch and things look good using the Xcode alternative. I got a warning while install ree-1.8.7-head, but it still appeared to work. The warning was:

    Error running 'patch -F 25 -p1 -N -f <"/Users/tom/.rvm/patches/ree/1.8.7/tcmalloc.patch"', please read /Users/tom/.rvm/log/ree-1.8.7-head/patch.apply.tcmalloc.log

  2. I’m receiving an error on Step #4 rvm install 1.9.3-p0

    The error I’m receiving is below.

    Error running ‘ ./configure –prefix=/Users/*****/.rvm/rubies/ruby-1.9.3-p0 –enable-shared –disable-install-doc –with-libyaml –with-opt-dir=/Users/*****/.rvm/usr ‘, please read /Users/*****/.rvm/log/ruby-1.9.3-p0/configure.log
    There has been an error while running configure. Halting the installation.

    Help is appreciated.


  3. Hey All,

    With Xcode 4.3 apple makes the command-line tools an optional install when you get xcode from the app-store (thats why the size is only 1.35 gb).

    To install the command tools open xcode => preferences => downloads => and press install command-line tools.

    This should fix all of the errors you are getting.

  4. I have installed RVM, Xcode, and homebrew on Lion OSX without major error but I can’t figure out why the message: “-bash: /etc/profile.d/rvm.sh: No such file or directory” appears above my prompt whenever I start any terminal window. Any suggestions on how I can fix that?

  5. seems like you installed multiuser RVM, I dont know how you got that. :D
    check out this page for solutions: http://beginrescueend.com/support/troubleshooting/

  6. Yeah, I must not have read the install documents carefully… but the problem it seems was that in the bottom of my /etc/profile it was sourcing /etc/profile.d/rvm.sh

    I’m not sure how it got there…but I removed it and all is well now. :-)

  7. The gist URL above to install homebrew is no longer valid. The code was moved, and thing one worked for me: ruby -e “$(curl https://raw.github.com/mxcl/homebrew/2577536a8a88c9807d771254c627a5d84d16194d/install_homebrew.rb)”

  8. 5.Install mysql version 5.5.20 not working
    $ mkdir -p ~/Library/LaunchAgents
    $ cp /usr/local/Cellar/mysql/5.5.14/com.mysql.mysqld.plist ~/Library/LaunchAgents/
    $ launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

    edit to
    mkdir -p ~/Library/LaunchAgents
    cp /usr/local/Cellar/mysql/5.5.20/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

  9. Example apps I’m usually required to install:
    brew install git ack wget curl redis memcached libmemcached colordiff imagemagick nginx sqlite libxml2 libxslt readline v8 rsync sphinx lzma geoip lzo

    rsync, lzma not working

  10. Problem:
    $ brew install wget
    Error: Cannot write to /usr/local/Cellar

    sudo chown -R $USER /usr/local

  11. Downloading and Installing Homebrew Installation successful!

    ruby -e “$(curl https://raw.github.com/mxcl/homebrew/2577536a8a88c9807d771254c627a5d84d16194d/install_homebrew.rb)”

  12. thank you, will updated post.

  13. Hi,

    Firstly, thank you for the great article.

    I’m new to Ruby and for the first time installing Ruby on my Mac.

    I am getting following error at step 4: $ gem install rails bundler unicorn pg

    ERROR: While executing gem … (Gem::FilePermissionError)
    You don’t have write permissions into the /Library/Ruby/Gems/1.8 directory.

    Your help will be greatly appreciated.

    Thank you.


  14. When I got to this line to load mysql automatically:

    $ cp /usr/local/Cellar/mysql/5.5.14/com.mysql.mysqld.plist ~/Library/LaunchAgents/

    I got this error:

    …mysql.mysqld.plist: No such file or directory

  15. Hello,

    I am having issues during MySQL installation using brew: Getting following error:
    brew install mysqlError: undefined method `url’ for nil:NilClass
    Please report this bug:
    /usr/local/Library/Homebrew/bottles.rb:25:in `bottle_current?’
    /usr/local/Library/Homebrew/bottles.rb:17:in `install_bottle?’
    /usr/local/Library/Homebrew/formula_installer.rb:21:in `initialize’
    /usr/local/Library/Homebrew/cmd/install.rb:74:in `new’
    /usr/local/Library/Homebrew/cmd/install.rb:74:in `install_formulae’
    /usr/local/Library/Homebrew/cmd/install.rb:72:in `each’
    /usr/local/Library/Homebrew/cmd/install.rb:72:in `install_formulae’
    /usr/local/Library/Homebrew/cmd/install.rb:20:in `install’
    /usr/local/bin/brew:83:in `send’

    Can someone help. I am using OS X Lion

  16. For anyone having issues with the last three lines of installing mysql ( Line 2: file not found ). The install location is different per version, you can see the version in the output after running ‘mysql_install_db’. Also the plist filename has changed to ‘homebrew.mxcl.mysql.plist’. At least for me it was, didn’t test all of this, but it got me through the installation process..

  17. Hi FRED
    I clicked on your contact link but it doesn’t work, so I am posting here.
    I installed Homebrew – but can not get FontForge to execute.
    If you have a few free moments, perhaps you can take a look at my github issue https://github.com/mxcl/homebrew/issues/14206

  18. I don’t know how this is easier than Macports – or better – you mention at the beginning of your post that Macports falls apart over time – well – based on your own example, and other folks responses, Homebrew falls apart from the start. yeah – create this directory because of a bug in Homebrew.

    i think not.

Sorry, the comment form is closed at this time.

Switch to our mobile site