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 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.
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.
- 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
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.
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/
$ 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
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
- 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