Wednesday, November 30, 2011

Installing Rails on OS X Lion with HomeBrew, RVM and Mysql


Bye Bye Macports, Welcome Homebrew

Definition
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 3 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”
Update: If you don’t want to download and install huge XCODE (3.0GB) :
https://github.com/kennethreitz/osx-gcc-installer
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 "$(curl -fsSL https://raw.github.com/gist/323731)"
Installation instructions: https://github.com/mxcl/homebrew/wiki/installation
3. install RVM
#  bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
then after RVM is installed run this 'one-line' command.
# echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
Details instructions: http://beginrescueend.com/rvm/install/
4. Install ruby 1.9.2
OS X Lion comes with Ruby-1.8.7-p249, but we all want ruby 1.9.2 right?
# rvm install 1.9.2
# rvm use ruby-1.9.2
# gem install rails thin bundler
....
I tested both ruby-1.9.2-head and ruby-1.9.3, latest sources from git, and it works well with all my apps.
With Ruby-1.9.3-head, Rails booted a lot faster.
If you wish to try 1.9.3:
# rvm install 1.9.3-head
Optionally you might want to install GIT, wget,
ack, imagemagick
and other mighty software tools for daily use.
# brew install git ack wget curl redis memcached libmemcached colordiff imagemagick
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:
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
bind-address = 127.0.0.1
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 this solution:
# export CC=/usr/bin/gcc-4.2
# rvm install ruby-1.8.7
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
[client]
port = 3306
socket = /tmp/mysql.sock 

[mysqld]
event_scheduler = ON
skip-character-set-client-handshake
collation_server = utf8_unicode_ci
character_set_server = utf8 

bind-address = 127.0.0.1
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 = 4M
sort_buffer_size = 8M
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 = 64M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 1G
myisam_repair_threads = 1
myisam_recover 

# INNODB Options
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 128M
innodb_thread_concurrency = 2
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 4M
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
innodb_file_per_table 

[mysqldump]
quick
max_allowed_packet = 16M 

[mysql]
no-auto-rehash 

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 16M
write_buffer = 16M

[mysqlhotcopy]
interactive-timeout 
Rating: 7.8/10 (6 votes cast)
Rating: +1 (from 3 votes)

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

  1. THANKS! It went flawlessly, which is completely different to the last time I tried it :)
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  2. i just installed Lion overtop of Snow Leopard. my machine thinks it no longer has XCode. have you had this problem? did you have to reinstall xcode after the migration?
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  3. Flawless for me as well. Thanks. Mysql has never installed that easily
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  4. yes, you MUST reinstall Xcode.
    I also deleted the /Developer folder before installing new Xcode, to avoid keeping old files.
    sudo rm -rf /Developer
    
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  5. Does a new MBP with Lion have a good enough version of Xcode, or still need to download it?
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  6. Very useful instructions, thanks! I skipped the MySQL installation, though, as I am using MAMP as my MySQL solution.
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  7. I spoke too soon. Getting the MySQL API module for Ruby to build required me to install MySQL in the way you’ve documented it here: MAMP’s MySQL doesn’t include compile libraries.
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  8. Nice guide, however for MySQL I’d recommend using the .DMG rather then homebrew. Normally I prefer homebrew as they are totally optimized for mac’s by leveraging installed xcode binaries, however the .DMG is 5.5.15 vs 5.5.14 in homebrew, and you also get the prefs panel… I could care less for the later, but some users might care.
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  9. hey guys, no more Xcode required with this jewel!
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  10. Works thanks for your help
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  11. cool, thanks for the tip, updating guide.
    cheers.
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  12. Fantastic. Thank you for your help to do this upgrade. Many days of pain to get to this site and you made it so easy.
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  13. [...] Guía de instalación (en inglés) Share this:StumbleUponDiggRedditLike this:LikeBe the first to like this post. Esta [...]
  14. thanks. this helped a lot.
    ran into an issue with vanilla Lion install on a new macbook air. “usr/local/Cellar” was not create with homebrew.
    this could be a possible bug https://github.com/mxcl/homebrew/issues/6810 but i just went and manually made a usr/local/Cellar
    cheers mate
    Rating: 3.0/5 (1 vote cast)
    Rating: 0 (from 0 votes)
  15. Like mrchess, I had to create Cellar folder manually. I am also on Lion.
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  16. I’m new to homebrew and I was following everything here except when I got to brew install git I get the error Cowardly refusing to `sudo brew install’
    I’ve never seen this before and google isn’t helping
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  17. because you are not supposed to use sudo at all.
    just do “brew install git”
    cheers
    Rating: 0.0/5 (0 votes cast)
    Rating: +1 (from 1 vote)
  18. I tried and created /usr/local/etc/my.cnf but still got the following message when running mysql_secure_installation:
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
    how can i solve this issue? thanks a lot in advance.
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  19. problem solved :)
    Rating: 0.0/5 (0 votes cast)
    Rating: -1 (from 1 vote)
  20. This all worked perfectly for me, and I am able to run Ruby shell scripts. I have Rails 1.9 installed and working too. I have two (dumb) questions:
    1. Did this install a Web Server?
    2. How do I find and view all the RDoc documentation it keeps reporting has been installed?
    Thanks
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  21. 1. Did this install a Web Server?
    no, install nginx with this command
    brew install nginx
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  22. Since you are using RVM you could also install rails with:
    rvm gemset install rails
    Just learned that myself today when trying to get around an error related to the rails gem currently not having any lib directory
    Successfully installed rails-3.1.0
    1 gem installed
    Installing ri documentation for rails-3.1.0…
    file ‘lib’ not found
    Installing RDoc documentation for rails-3.1.0…
    file ‘lib’ not found
    —————————————-
    dmathieu commented July 05, 2011
    This is because rdoc loads this directory to generate the documentation. By default, this is “lib”.
    And the rails gem does not have any lib directory.
    This was fixed in e7fc5d1. But it raised other problems so it was reverted.
    A fix should arrive soon.
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  23. Thanks for the great post. Do you need to run unset TMPDIR after ‘brew install mysql’.
    Mysql server will not start after installing following your instructions (ERROR! The server quit without updating PID file (/usr/local/var/mysql/wizbang.local.pid).
    I can’t install the mysql gem (ERROR: Failed to build gem native extension. … No such file or directory – /usr/local/mysql/bin/mysql_config).
    Wondering if you might have any input? Seems that with homebrew this should be as easy as brew install mysql. The relationship between mysql and os x needs some counseling. Its been very painful for years. The dmg installer from mysql has never worked for.
    thanks
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  24. Many thanks for the tutorial Frederico!
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  25. Cheers! The chapter 6 is really useful. ;)
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  26. You rock! I am new to rails (do lot’s of ASP.NET MVC) and I struggled along to get the newest version to run. Your post made it all flawlessly simple. Thank you!
    Rating: 0.0/5 (0 votes cast)
    Rating: +1 (from 1 vote)
  27. This might help if mysql fails to download like it did for me.Fix for fails mysql download basically one of the subdomains on mysql.com no longer exists so you need to download mysql from a new location and move it to the brew source cache.
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  28. Thanks a lot of this walkthrough!
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  29. I typed in these exact commands (using Lion OSX):
    rm -rf ~/.rvm/
    sudo rm -rf /Developer
    and it erased ALL my local documents and files! Are these now completely unrecoverable? Why did it delete everything instead of stuff just under RVM and the developer folder?
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  30. are you sure you typed correctly?
    I will ONLY delete the /Users/username/.rvm/ folder and /Developer/ Folders, nothing else.
    I should have warned caution with those 2 commands. :p
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)
  31. this command gives me permissions errors:
    # gem install rails thin bundler
    People say never to use sudo when installing gems, but if I don’t I get the following errors:
    You don’t have write permissions into the /Library/Ruby/Gems/1.8 directory.
    You don’t have write permissions into the /usr/bin directory.
    The first one I can fix by changing ownership of the whole Ruby directory, but I don’t want to change ownership of /usr/bin.
    I can’t be the only one who is getting these errors can i?
    Rating: 0.0/5 (0 votes cast)
    Rating: 0 (from 0 votes)

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">
StatCounter - Free Web Tracker and Counter