Installing Ruby, Rails, and MySQL on OS X Lion

Hi, folks! What’s new? Not much here, either. Just hanging out on Treehouse Island. How have you been? How are things? Things are good here, too. What’s that? You want to install the latest versions of Ruby, Rails, and MySQL on Lion? Ok, that’s cool, we can do that instead of watching the latest Twilight movie. No problem.

We’re going to be using homebrew and rvm to get this all up and running. This is a bit of an opinionated method of getting everything installed. There are several ways of installing a modern Ruby and Rails environment and in the end, they all get you to the same place. Another popular option for getting ruby up and running is ruby-build and rbenv.

For the purposes of getting everything up and running quickly, I find rvm to be a bit easier to use and explain so I’m going to use that. Another quick note: while these instructions are written for Lion, they should also work on Snow Leopard, particularly the osx-gcc-installer instructions.

Install Developer Tools

Before we install either Homebrew or rvm, we need to have a compiler installed. Xcode happens to come with one. Open up the App Store and type “Xcode” in to the search box. Click install. This is a large download so go ahead and get some coffee or catch up on the latest meme on Reddit.

Xcode in the Mac App Store

Another option is to use the osx-gcc-installer. This doesn’t install the Developer Tools from Apple, only the compiler. The instructions that follow should work with only that installed but I haven’t tested it. Besides, you’ll probably want to have the full developer tools installed so you can unlock the iOS foundations badge on Treehouse.

Install Homebrew

Homebrew is “The missing package manager for OS X.” The other big package manager available on OS X is called MacPorts. It’s also very good but we’re going to use Homebrew for the purposes of this article.

A Word of Warning

The instructions that follow run a shell script directly from a web site that you are not in control of. This is generally considered a Bad Thing™ and in general, you should at least review what the script is doing before you run it. We’re going to have to trust each other and the homebrew team that the script does what it’s supposed to do. Always think twice when you see something like this, though.

Nevertheless, we’re going to trust the homebrew team and run this script. I’ve run this several times before and it’s not like it’s a comment thread on YouTube.

Moving On

Open up the Terminal application that comes with OS X. This should be inside the “Utilities” folder in your main “Applications” folder. Type the following:

/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

You should see a bunch of text going by at this point and having it seem like something is installing. If you see any error messages, double check to make sure that you’ve installed the Developer Tools correctly.

Install Git

This part should go by pretty quickly. Open up a terminal and type the following:

brew install git

You should now have git installed. You can verify by typing the following:

git --version

You should see:

git version 1.7.7.2

That’s the latest version of git as of this writing. If you don’t see something similar, double check that you’ve correctly installed the Developer Tools and Homebrew above.

Install MySQL

Now that you’ve got Homebrew installed, installing MySQL is pretty easy. In the same terminal, type the following:

brew install mysql

Again, you should see a bunch of text going by saying things like “Downloading,” “Patching,” “make” and the like. After it finishes, you should see a bunch of instructions. Let’s follow them, still in terminal:

mysql_install_db --verbose --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/mysql/5.5.15/com.mysql.mysqld.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

That should install mysql to run on startup of your system. Test it with the following at the command prompt:

Note: The dollar sign in front of the mysql command denotes that we are at a shell inside of the terminal. It is not necessary to type it.

$ mysql -uroot

You should see the following:

Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 75
Server version: 5.5.15 Source distribution
 
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
 
mysql>

If you don’t see that, something went wrong in the install. Double check the instructions above and try again. Type “quit;” to exit the prompt.

Congratulations! You’ve installed msyql.

Install RVM

RVM is the Ruby Version Manager. It lets you easily switch between different versions of Ruby on the command line. Remember what I told you a few paragraphs above about not running untrusted shell scripts? We’re going to do it again. Open up terminal again and type the following to install rvm:

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

That install install rvm. We’re not done yet, though! Create a new file in your home folder called “.bash_profile” and open it using a text editor:

$ touch ~/.bash_profile
$ open -e ~/.bash_profile

This will open the file in TextEdit. Place the following inside:

export PATH="./bin:$PATH"
[[ -s "/Users/jason/.rvm/scripts/rvm" ]] && source "/Users/jason/.rvm/scripts/rvm"

Make sure to change jason above with the “Account name” on your mac. You can find that by typing the following in to your terminal:

$ echo $USER

In my case it returns jason.

If you already have a .bash_profile, you can add those lines to it at the bottom.

With those things done, you can now close terminal.

Install Ruby

In terminal, we need to tell RVM to load the environment we just set up. With terminal open, type the following:

$ source ~/.bash_profile

Now we can install a ruby interpreter:

$ rvm install 1.9.3-p0

This will install the latest Ruby. At the time of this writing, that is version 1.9.3, patch level 0. This should take a few minutes. Feel free to get back to Reddit as you have already loaded up on coffee by this point.

Once it’s done, you have installed Ruby! Congratulations!

Now, let’s tell rvm to use the Ruby we’ve just installed:

$ rvm use 1.9.3-p0

And we’ll make sure we’re using the right version:

$ ruby -v

You should now see:

ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]

Update: As Ryan Townsend points out in the comments, if you’d like to use this version of Ruby every time you’re in a shell, type the following:

$ rvm use 1.9.3-p0 --default

Thanks, Ryan!

Install Rails

Now that we have Ruby installed, let’s install some gems. First up, we’re going to want to install bundler which Rails depends on. Let’s install some gems:

$ gem install bundler
$ gem install mysql2
$ gem install rails

Now that we have rails and bundler installed, we can create our own rails app. Type the following:

$ rails new testapp

This will create a rails application called “testapp” and then run “bundle install” inside of it. Congratulations, you are now on the rails!

Summary

We’ve gone and installed the latest versions of the Developer Tools, a package manager, git, mysql, ruby, and rails. At this point, we’re all set to create our own Rails applications. If you’d like to learn Ruby, keep an eye on Treehouse. We’ll have the Ruby Foundations badge ready to be unlocked real soon now. If you have any questions, let me know in the comments. Cheers!

Free Workshops

Watch one of our expert, full-length teaching videos. Choose from HTML, CSS or WordPress.

Start Learning

Treehouse

Our mission is to bring affordable Technology education to people everywhere, in order to help them achieve their dreams and change the world.

Comments

29 comments on “Installing Ruby, Rails, and MySQL on OS X Lion

  1. Just a quick tip, if you want to always be running the Ruby 1.9.3, without having to enter ‘rvm use 1.9.3-p0′ into your terminal every time, you can set it as the system-wide default: ‘rvm use 1.9.3-p0 –default’

  2. I’d recommend creating RVM gemsets and .rvmrc files for each of your projects

    e.g.

      cd myproject
      rvm –rvmrc –create ruby-1.9@myproject

    That creates a gemset specific for that project, all gems used by that project will be held in that local gemset.  The .rvmrc file will automatically switch your terminal session to the correct gemset each time you enter the project directory.

    You can add additional things to your .rvmrc file, such as checking your gemfile and running bundler if it’s changed. Combined with Guard that works pretty well to make sure your gems are in line with your Gemfile at all times.

  3. Another tip: if you are planning on deploying to Heroku then you’ll want to dump mysql and install Postgres, since that’s what they use (unless you pay extra for the MySQL addon).

    Having said that, you should only ever need something like that on your dev box if you are doing something that requires DBMS specific features.

  4. Anyone running Xcode 4.2 running into an issue installing via rvm, try

    rvm install 1.9.3 –with-gcc=clang

    credit to http://stackoverflow.com/questions/8032824/cant-install-ruby-under-lion-with-rvm-gcc-issues for the tip

  5. Thank you Jason! Always nice to have a step-by-step breakdown of my main toolset :) I had just re-intalled Lion for a fresh feel… this helped me get everything back on track!

  6. I’m usually wary of doing this sort of thing on the machine itself because OS updates can kill it. I use MAMP (free), which sits in /Applications, to run an Apache/MySQL/PHP stack: it’s invulnerable from OS updates, and much easier to port between machines, back up, and tweak the version of PHP/whatever. 

    Could these instructions be redone for MAMP installation? 

  7. RVM defaults to the user’s ~/.rvm directory, and brew uses /usr/local/, so it doesn’t conflict with the system installs of ruby and its gems, and it protected form OS specific upgrades.

  8. Though the computers in the stores currently may not have Lion preinstalled, Apple is giving a voucher to all customers who purchase(d) a Mac on or past June 6 to download Lion for free.

  9. Though the computers in the stores currently may not have Lion preinstalled, Apple is giving a voucher to all customers who purchase(d) a Mac on or past June 6 to download Lion for free.

  10. Rather than referencing “/Users/jason/.rvm/…” in your .bash_profile it might be better to use “$HOME/.rvm/…”. This eliminates the need to determine your username ahead of time, and makes it easier to share your shell configurations across multiple platforms.

  11. Rather than referencing “/Users/jason/.rvm/…” in your .bash_profile it might be better to use “$HOME/.rvm/…”. This eliminates the need to determine your username ahead of time, and makes it easier to share your shell configurations across multiple platforms.

  12. I’m getting this error when running rake db:migrate

    rake aborted!Please install the mysql adapter: `gem install activerecord-mysql-adapter` (mysql is not part of the bundle. Add it to Gemfile.)Tasks: TOP => db:migrate => environment

    Any ideas about it?

      • This gem does not exists…
        ERROR:  Could not find a valid gem ‘activerecord-mysql-adapter’ (>= 0) in any re
        pository
        ERROR:  Possible alternatives: activerecord-jdbcmysql-adapter, activerecord-jdbc
        mssql-adapter, activerecord-odbc-adapter, activerecord-jdbc-adapter, activerecor
        d-jdbch2-adapter

  13. This is the problem i am having 

    new-host:~ myname$ rvm install 1.9.3-p0
    Installing Ruby from source to: /Users/myname/.rvm/rubies/ruby-1.9.3-p0, this may take a while depending on your cpu(s)…

    ruby-1.9.3-p0 – #fetching 
    ruby-1.9.3-p0 – #extracted to /Users/myname/.rvm/src/ruby-1.9.3-p0 (already extracted)
    Fetching yaml-0.1.4.tar.gz to /Users/myname/.rvm/archives
    Extracting yaml-0.1.4.tar.gz to /Users/myname/.rvm/src
    Configuring yaml in /Users/myname/.rvm/src/yaml-0.1.4.
    Compiling yaml in /Users/myname/.rvm/src/yaml-0.1.4.
    Installing yaml to /Users/myname/.rvm/usr
    ruby-1.9.3-p0 – #configuring 
    ERROR: Error running ‘ ./configure –prefix=/Users/myname/.rvm/rubies/ruby-1.9.3-p0 –enable-shared –disable-install-doc –with-libyaml-dir=/Users/myname/.rvm/usr ‘, please read /Users/myname/.rvm/log/ruby-1.9.3-p0/configure.log
    ERROR: There has been an error while running configure. Halting the installation

  14. Thanks for the write up Jason, unfortunately I’m receiving the same error as another user here. The installation stops when installing Ruby 1.9.3. Error below:

    ruby-1.9.3-p0 – #fetching ruby-1.9.3-p0 – #extracted to /Users/myname/.rvm/src/ruby-1.9.3-p0 (already extracted)Fetching yaml-0.1.4.tar.gz to /Users/myname/.rvm/archivesExtracting yaml-0.1.4.tar.gz to /Users/myname/.rvm/srcConfiguring yaml in /Users/myname/.rvm/src/yaml-0.1.4.Compiling yaml in /Users/myname/.rvm/src/yaml-0.1.4.Installing yaml to /Users/myname/.rvm/usrruby-1.9.3-p0 – #configuring ERROR: Error running ‘ ./configure –prefix=/Users/myname/.rvm/rubies/ruby-1.9.3-p0 –enable-shared –disable-install-doc –with-libyaml-dir=/Users/myname/.rvm/usr ‘, please read /Users/myname/.rvm/log/ruby-1.9.3-p0/configure.logERROR: There has been an error while running configure. Halting the installation