Friday 3 February 2012

Installing Ruby 1.9 and Rails 3 on Mac OS X

 
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, we're beginning to nudge folks to try Ruby 1.9.3.
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

Snow Leopard and Lion ship with Ruby 1.8.7, which works with Rails 3. But we're going to also install Ruby 1.9.3. We won't be updating the system-installed Ruby. That way, you'll be able to switch back and forth between these Ruby versions if you like.
The most flexible way we've found to install different versions of Ruby on Mac OS X is using Ruby Version Manager (RVM). It's a command-line tool that makes it easy to install and manage multiple Ruby environments. We'll be installing Ruby 1.9.3, the latest Ruby version that's supported for use with Rails 3.
  1. First, you'll need to make sure you have Xcode 3 or later installed. Xcode includes the Mac developer tools such as gcc that RVM needs to compile versions of Ruby for you. You can check your Xcode version by opening a Terminal session and typing
    xcodebuild -version
    
    If you're not running Xcode 3 or higher, you have a few choices. If you're running Mac OS X 10.7 (Lion), you can download Xcode 4.2 as a free app from the Mac App Store and it'll be automatically installed on your Mac. If you're running Mac OS X 10.6 (Snow Leopard), you'll need to install Xcode from the Snow Leopard DVD that came with your Mac. You'll find Xcode in the Optional Installs directory. Alternatively, you can download just the GCC compiler and related tools as pre-built binary packages for Lion or Snow Leopard from the osx-gcc-installer downloads page.
  2. Next, to install RVM from its GitHub repository (the recommended way) you'll need the Git version control system. If you installed Xcode 4.2, it installed Git for you. Otherwise, you'll need to download and install Git. You can check that you have Git installed by opening a Terminal session and typing
    git --version
    
  3. With that out of the way, install RVM by opening a Terminal session and typing
    bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
    
    Note that bash isn't the prompt here; you actually have to type it. And be careful: there are two less-than signs in this command, with a space between them.
  4. To automatically load RVM into any new Terminal shell sessions, append the RVM function setup to the end of your shell's initialization file by typing:
    echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" ' >> ~/.bash_profile
    
  5. Speaking of which, close your Terminal session and open a new session. Then confirm that RVM is being loaded properly by typing
    type rvm | head -1
    
    Note that type is a command that you actually have to type.
    You should see the following:
    rvm is a function
    
  6. Now install Ruby 1.9.3 by typing
    rvm install 1.9.3
    
    If you get an error and you have Xcode 4.2 installed, you may have to use
    rvm install 1.9.3 --with-gcc=clang
    
    This will download, compile, and install Ruby 1.9.3 into a directory managed by RVM.
  7. When it's done, set Ruby 1.9.3 as the current Ruby version in your Terminal session by typing
    rvm use 1.9.3
    
  8. Then verify that Ruby 1.9.3 is the current Ruby version by typing
    ruby -v
    
    You should see
    ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]
    
  9. Now set Ruby 1.9.3 as the default interpreter to be used whenever you open any new Terminal sessions by typing
    rvm --default 1.9.3
    
    If you ever want to go back to the system-installed version of Ruby (1.8.7), simply type
    rvm system
    
    And to switch back to Ruby 1.9.3, type
    rvm 1.9.3
    
    Alternatively, you can switch back to the default version (1.9.3) by typing
    rvm default
    
    Isn't that handy!
  10. Next, although not required, you'll likely want to generate the Ruby core documentation by typing
    rvm docs generate
    
  11. Finally, while you've got a Terminal open, verify that RubyGems 1.8.10 or higher is installed by typing
    gem -v
    

Install SQLite3

Rails uses SQLite3 as a default development database because it's lightweight, easy to use, and included with Mac OS X. We'll just need to install the Ruby bindings to SQLite3.
  1. Verify that you have SQLite 3 installed by typing
    sqlite3 -version
    
    It should respond with 3.7.9 or higher.
  2. Then install the Ruby bindings to SQLite3 by typing
    gem install sqlite3
    

Install Rails 3

Rails is distributed via RubyGems: the standard Ruby package manager. When you installed Ruby, the RubyGems system came along for the ride. With RubyGems already installed, it's easy to install Rails and its dependencies.
  1. Install Rails by typing
    gem install 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 couple dozen gems installed.
  2. When it's done installing, verify that the correct version of Rails was installed by typing
    rails -v
    
    Rails should answer with 3.2.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.
  1. From a command prompt, navigate to a directory where you want the application code to live (~/work, for example).
  2. Start by creating an empty Rails application called todos:
    rails new todos
    
  3. Change into the todos directory that was created in the previous step:
    cd todos
    
  4. 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).
  5. Run the database migration by typing
    rake db:migrate
    
  6. Then start the Rails app by typing
    rails s 
    
  7. Finally, point your web browser at http://localhost:3000 and you should see a page welcoming you to Rails. To start managing your todos, go to http://localhost:3000/todos.
  8. When you're done, you can stop the Rails app by typing CTRL-C in the command prompt where you started the app.

Next Steps

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 attending an upcoming public Rails Studio, or scheduling a private course on-site at your location. We'll show you how we build Rails apps, and through 15 hands-on exercises and guided instruction you'll learn how to create a Rails app from start to finish. You'll come away with the confidence to knock out your first Rails app, or improve your existing app. It's a lot of fun!


 Mike Clark 

http://pragmaticstudio.com/blog/2010/9/23/install-rails-ruby-mac


No comments:

Post a Comment