Installing Rails On A Mac

9

Installing Rails can be one of the trickiest things to overcome for a newbie to Ruby and Ruby on Rails. Being an open source project with lots of dependencies is a great strength for Rails and allows it to innovate quickly, but it has the unfortunate side effect of making a prospective developer jump through a lot of hoops, particularly for someone new to programming or new to the Unix/Linux world.

Windows

Windows users are in luck as there is a great, no hassle installer available at RubbyInstaller.org. This is about as easy as it gets for installing Rails; unfortunately, my experience on a Windows laptop (even one with an SSD) is that Rails development can be painfully slow due the large amount of file I/O that Rails development entails.

Mac

This is bound to change, and I had to rewrite some of these instructions over the course of installing Ruby on Rails on a couple of different Macs with different versions of OS X and XCode. But here is what worked for me on Mountain Lion (OS X 10.8) and XCode 4.5.2. There are a couple of terms and concepts to be familiar with.

  • XCode is Apple’s development environment, primarily used for creating iOS and Mac apps, but it includes some system libraries that you need for our Rails development.
  • Homebrew is a package environment for OS X that can install most other missing system packages you’ll need for Rails
  • RVM (Ruby Version Manager) is highly recommended as it lets you manage different versions of Ruby, Rails and Gems so you can try out new versions or mimic different production environments without having to install/reinstall everything.

Installing Rails

Install XCode:

  • Click and install XCode from the Mac Store
  • Once installed, go to Preferences -> Downloads -> Install Command Line Tools

Please note that the next several installation commands should be run in the Terminal application.

Install Homebrew and apple-gcc42
This installs the Homebrew package manager and a GCC compiler

$ ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"
 $ brew tap homebrew/dupes
 $ brew install apple-gcc42
 $ sudo ln -s /usr/local/bin/gcc-4.2 /usr/bin/gcc-4.2

Install RVM:
In the OS X Terminal, issue the following commands:

$ \curl -L https://get.rvm.io | bash -s stable --ruby

Note: run the source command outputted from the above command

$ rvm install 1.9.3
$ rvm use 1.9.3
$ rvm --default 1.9.3

The above commands install RVM, install ruby version 1.9.3, tell RVM to use the 1.9.3 version for the rest of the stuff you’ll install and set 1.9.3 to be default going forward.

Install XQuartz
Apple no longer ships an X Windowing system starting with Mountain Lion, so you’ll probably need to install the open sourced version of this available at  http://xquartz.macosforge.org/landing/

Install Rails

$ gem install rails

Rails should now be installed. To create a test app, run

$ rails new my_app_name

If you get an error about OpenSSL support, you may need to update RVM. See the instructions at https://rvm.io/packages/openssl

Optional: MySQL
MySQL is a popular open source database that you may want to install locally to make sure that it behaves the same as your production instance of MySQL (if that’s what you’re running). Rails abstracts a lot of the different database differences to your app, but you still may want to actually run MySQL just to have the added confidence that there aren’t any subtle quirks/differences.

$ brew install mysql

 $ unset TMPDIR

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

 $ mysql.server start

 $ /usr/local/opt/mysql/bin/mysqladmin -u root password '[[[YOUR_ROOT_PASSWORD_HERE]]'

If you want to have MySQL start automatically when your Mac logs in, you can set it up like this:

$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/Cellar/mysql/5.5.27/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

Optional: ImageMagick

  • I had to do some basic image manipulation in my app (resizing photos, etc) and I had to install the ImageMagick package through homebrew to get it working.
$ brew uninstall imagemagick
 $ brew install --fresh imagemagick

Done! I had to try a lot of combinations to get it working on my laptop and my app; hopefully this helps as most of the online resources I found were out of date for Mountain Lion or the newest version of XCode.

There are also more detailed instructions for the Mac, as well as Linux installation instructions available at http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec-rubygems

Happy coding!

About the Author

This is a post written and contributed by Bret McGowen.

Bret McGowen is a software developer at Rackspace, designing and building RackConnect, which lets customers have the best of both traditional and cloud hosting. Bret has spent most of his professional life writing software using Microsoft’s .NET framework but is beginning to see the light and has started to dabble in open source technologies. When he’s not on the jogging trail or volleyball court he enjoys working on and reading about tech startups.

Follow Bret on Twitter at @bretmcg.

9 Comments

Once I have installed Command Line Tools, how do I install homebrew?
Where do I put in those commands?
Thanks

avatar Scott on May 4, 2013 | Reply

You should write those commands in Mac OS Terminal. Applications > Utilities > Terminal. be sure that you are in root!

avatar Kasra on June 6, 2013

OMG…what a life saver! Thank you for taking the time to write this info up.

avatar Nicole Betea on May 5, 2013 | Reply

This is awesome! Just got a new computer and didn’t have to demolish the internet to find every little detail.

I did have one issue…

When installing RVM: ‘\curl -L https://get.rvm.io | bash -s stable –ruby’ I had to add and extra dash to ruby: ‘ –ruby’

avatar Jason on June 5, 2013 | Reply

Thanks Jason! I have updated the post to indicate –ruby instead of -ruby

avatar Bret McGowen on June 18, 2013

Thank you Very Very much for these Info ! ;)

avatar Kasra on June 6, 2013 | Reply

I kept having problems getting rails to install. Turning off Avast anti virus solved the issue.

Good tutorial the steps were easy to follow once I figured out I needed to disable anti virus

avatar Joe on June 9, 2013 | Reply

Note, I had to change the “Install RVM” code to …
user$ \curl -L https://get.rvm.io | bash -s stable

avatar kevin on June 13, 2013 | Reply

Thanks for letting me know, I updated the post with a changed command

avatar Bret McGowen on June 18, 2013

Leave a New Comment

(Required)


©2013 Rackspace, US Inc. About Rackspace | Fanatical Support® | Hosting Solutions | Investors | Careers | Privacy Statement | Website Terms | Trademarks | Sitemap