Updated: February 12, 2013
A few weeks before every Pragmatic Studio course, we send out homework to help attendees prepare for the course. The homework includes instructions for getting any required software installed and tested on their laptop, and some recommended reading. Our goal is to make sure that everyone turns up on the first day ready to program. So we try to catch installation problems early and help folks through them. Sending out prep homework ahead of time also means that attendees can begin to experiment on their own. As a result, they often jot down questions to ask during our time together. In a way, our courses start the moment you register.
We're continually updating our homework to take into account new software versions and new ways of doing things. With the topics we teach, there's never a dull moment. For the Rails Studio in particular, we've been teaching Rails 3 for a while now. And although you can use Ruby 1.8.7 with Rails 3, it's better to use Ruby 1.9.
It seems a lot of folks are interested in making this transition. So we thought it might be helpful to share our recommended installation steps for Ruby and Rails.
Install Ruby 1.9 and Rails 3
The easiest way we've found to install Ruby, Rails, and other supporting software on a Mac is using the RailsInstaller. It's a self-contained Mac installer (an .app file) that includes a Ruby language execution environment, a baseline version of Rails, and other useful goodies such as Git, SQLite, and the Ruby Version Manager.
Note that Mac OS X ships with an older version of Ruby (1.8.7) that Apple uses for internal tools but it isn't recommended for use with Rails. The RailsInstaller installs Ruby 1.9 separately rather than updating the system-installed Ruby.
On the RailsInstaller page, click the appropriate green button under "Mac Downloads" to download the installer.
Once the installer has downloaded (it usually ends up as a .tar file in your Downloads folder), double-click it to uncompress the file. You should end up an .app file. Double-click that file to start the installation process.
If you see a warning dialog stating that the RailsInstaller can't be opened because it's from an unidentified developer, you'll need to update your security settings. Open the System Preferences app and then the Security & Privacy group. Click the lock icon in the lower-left, and then change the "Allow applications downloaded from:" setting to Anywhere. You may want to change this back after opening the RailsInstaller.
After stepping through a couple standard installer screens, the RailsInstaller will start chugging through the process of installing everything you need. (If you're curious, everything gets installed into your
/optdirectory.) Installation takes a while, and you're welcome to step away for a refreshing beverage or a tasty sandwich...
When the installation is complete, find the Terminal application (it's under the /Applications/Utilities directory) and drag it onto your dock. You'll end up using Terminal a lot as a Rails developer, so it's good to have it handy. Then open a new Terminal session. You should see a new window with a cursor and a prompt that looks something like this:
The default prompt includes the computer name (enoch in my case), the current working directory (tilde represents your home directory), the current user name (mike), and a trailing $ which is the prompt for input.
If this is the first time you've seen this side of a Mac, it may seem rather intimidating. Don't let it throw you. It's simply a way to interact with your computer by entering commands. In fact, here comes our first command...
Verify that Ruby 1.9 was successfully installed by typing
Ruby should reply with
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.2.0]
Next, update Rails to the latest version by typing
gem update rails
Then sit back and relax as RubyGems downloads all the Rails-related gems and assembles the documentation. After a few minutes, you should end up with a dozen or so gems installed.
Finally, verify that the latest version of Rails was successfully installed by typing
Rails should answer with 3.2.13 or higher.
Create An Example Rails App
Now that we have all the required software installed, let's create your first Rails app to make sure everything is working in harmony. We'll create a simple application for managing a list of todos.
From a command prompt, navigate to a directory where you want the application code to live (~/work, for example).
Start by creating an empty Rails application called
rails new todos
Change into the
todosdirectory that was created in the previous step:
The application doesn't know about todos yet, so we'll use scaffolding to quickly generate all the code for managing a list of todos. Run the scaffold generator by typing
rails g scaffold todo name:string due_on:date completed:boolean
You'll see Rails create a bunch of files, including a migration file for creating a database schema to store todo items in a database (SQLite3 in this case).
Run the database migration by typing
Then start the Rails app by typing
When you're done, you can stop the Rails app by typing
CTRL-Cin the command prompt where you started the app.
That's all there is to it! Now you have everything you need to start building your own Rails app. And that's how we recommend you start learning Rails, by building something, whether it be for fun or profit. Rails is all about helping you get from idea to deployment, fast!
You might also consider taking our online Rails course or scheduling a private course on-site at your location. You'll learn how to build a complete Rails app step-by-step, from idea to deployment. You'll come away with a solid understanding of the fundamentals of Rails, and how to put all the pieces together, so you can confidently create your first Rails app or jump right into an existing app!