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.
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.
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.
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.
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:
You should see:
git version 22.214.171.124
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.
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.
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
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.
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
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!
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!