Support: 1-800-961-4454
Sales Chat
1-800-961-2888

Installing Ruby On Rails On A Mac

25

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

NOTE: This step is optional and is only necessary if you plan to do image manipulation using ImageMagick.

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.


More
25 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

help please!!!
i have a problem with step : install RMV
there is error:
ruby-2.0.0-p247 – #configuring……..
Error running ‘./configure –prefix=/Users/angelina/.rvm/rubies/ruby-2.0.0-p247 –disable-install-doc –without-tcl –without-tk –enable-shared’,

avatar angelina on June 28, 2013 | Reply

I’ve also hit the asme problem. Looking up change.log, I found this –

checking whether the C compiler works… no
configure: error: in `/Users/joy/.rvm/src/ruby-2.0.0-p247′:
configure: error: C compiler cannot create executables

Now, I had installed the apple gcc using brew And it installed without any issue. So, to cross check, I wrote a Hello World C program and tried compiling it.

#include

int main()
{
printf(“Hello World\n”);
return 0;
}

test.c:1:19: error: stdio.h: No such file or directory
test.c: In function ‘main’:
test.c:5: warning: incompatible implicit declaration of built-in function ‘printf’

My assumption is something went wrong with the header file locations with the new gcc installation.

Also, I had previously installed gcc via XCode Developer tools. Does this have to do something with this?

avatar Joy on August 19, 2013

Thanks for the tutorial. Very helpful. Even to someone who has only owned a Mac for 4 hours :)

If you use SU at any point during your install, make sure that you are not root or SUDO when installing gems. You will run into a lot of permission issues if you install gems at root.

avatar Daniel on July 15, 2013 | Reply

Excellent writeup thanks. Had to reinstall OSX twice before i came across this and now i have my rails environment perfectly set up.

avatar Taimoor Qureshi on August 2, 2013 | Reply

Thank you very much for your article. I’m a Rube-Newb, but wanted to note for Mac users that homebrew IS ideal for configuring Ruby, but you can still use macports alongside homebrew if you trunk macports in /opt/. Declaring $ rvm autolibs homebrew should default Ruby dependencies to be checked against homebrew instead of macports (which is the default). So my request, since you pushed me over into Ruby zone today, is for you to write an article about trunking and having macports and homebrew on one machine :D

avatar Nathan on August 5, 2013 | Reply

Thank you so much I have followed so many tutorials to get rails going and yours….. finally worked for me! you are a lifesaver!!!

avatar Ryan on August 7, 2013 | Reply

Worked like a charm on my new Mac. Thanks for the write-up.

avatar Chris McNelis on August 8, 2013 | Reply

Hi, I’m very new to this, and so far you’ve helped me a lot!

I’m not sure at all, but should I change the rvm commands into Ruby version 2.0.0-p247? (..new version since the the article got published?)
Or is it on purpose that the version used in the article is a previous version?

I’ve run the rvm commands with the version from this article, but if I “should” use Ruby 2.0.0-p247, how do I change that? Or is it actually no matter really?

And again, this helped me a lot – thanks! :-)

avatar Nick on August 25, 2013 | Reply

Installation of ruby on rails on Mac is quite different as compare to windows. You will require a proper assistant to get it install. Ruby on Rails is an open source full-stack web application framework written in Ruby. It follows the popular MVC framework model and is known for its “convention over configuration” approach to application development. Ruby on Rails development is a full-stack web framework optimized for programmer happiness and sustainable productivity.

avatar Mack S on September 24, 2013 | Reply

You can always use vagrant, that’s what I do, in windows or mac I share the same virtual box between them and all my team.

avatar Bmac on September 26, 2013 | Reply

Is Installing Xcode a must? Even if I have standalone `Command Line Tools` which is released independent of Xcode too?

avatar AK on October 5, 2013 | Reply

What is “rails” and why would I want to install it?

(Yeah, I know it is a dumb question, but it is a dumb question that SHOULD have been answered by the article…)

Don’t bother with a reply, I am off to google it…

avatar Testicules on October 10, 2013 | Reply

May I suggest the article What is Ruby on Rails?.

avatar Daniel Kehoe on October 28, 2013

The article is a bit out of date since Mac OS X 10.9 Mavericks has been released. For instructions for Mac OS X 10.9 Mavericks, see the article:

Install Ruby on Rails on Mac OS X Mavericks

avatar Daniel Kehoe on October 28, 2013 | Reply

ruby -e “$(curl -fsSkL raw.github.com/mxcl/homebrew/go/install)”

avatar Conal Da Costa on November 21, 2013 | Reply

This was awesome! Thank you so much!

avatar Jeff on December 10, 2013 | Reply

Leave a New Comment

(Required)


Racker Powered
©2014 Rackspace, US Inc.