Updated: Sept. 5, 2013
All of our online courses start with comprehensive instructions for getting the required software installed and set up on your own computer. For the online Rails course, that means installing Ruby and Rails. During the course, you'll then write, refactor, and test your code directly on your own computer. That way, after the course, you'll already be familiar with the environment where you can then start writing your own Rails apps!
Setting up a stable Ruby and Rails environment on your Mac has never been easier. Here's our recommended approach…
Install Ruby 1.9 and Rails 4
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 RVM, Git, and SQLite.
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. It does this using the Ruby Version Manager (RVM) which is a command-line tool that makes it easy to install and manage multiple independent Ruby environments on the same machine. That way, you'll be able to easily switch back and forth between Ruby versions if you like.
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 with a .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 4.0.0 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 apps. And that's exactly how we recommend you start learning Rails, by actually building something, whether it be for fun or profit.
If you're not sure how to get started building Rails apps, consider taking our online Rails course. You'll learn how to build a complete Rails app step-by-step, from idea to deployment. Along the way you'll learn the tips, tricks, and best practices used by experienced Rails developers to build high-quality Rails apps quickly. 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!