Ruby For Rails Step-by-Step Procedure on Mac

This is a 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 independent Ruby environments on the same machine. It takes a little extra effort up front, but it’s well worth it in the end. We’ll be installing Ruby 1.9.3, the latest Ruby version that’s supported for use with Rails 3.

  1. RVM compiles Ruby versions from source code, and to do that it uses the GCC compiler and other build tools. GCC isn’t installed by default on Mac OS X; GCC is only available after you’ve installed the Mac developer tools.

    Check to see if you have GCC installed by opening a Terminal session and typing

    gcc --version
    

    If you see a version number, then you’re all set. If the gcc command isn’t found, then you have a couple options:

    • If you’re running Lion and you have an Apple ID, download Apple’s officialCommand Line Tools for Xcode. Once it has finished downloading, simply double-click the .dmg file and then the enclosed .pkg file to start the installation process.

    • If you’re running Snow Leopard or don’t want to create an Apple ID, download the unofficial GCC compiler and related tools from the osx-gcc-installer downloads page. Pre-built binary packages are available for both Snow Leopard (GCC-10.6.pkg) and Lion (GCC-10.7-v2.pkg). Once it has finished downloading, simply double-click the .pkg file to start the installation process.

  2. Next, to install RVM from its GitHub repository (the recommended way), you need a working version of the git version control system. It may seem like overkill at this point to install a version control system. However, as you become more familiar with the Rails community you’ll find that having git installed will make things a lot easier. And it’s easy enough to install, so we may as well get it out of the way now.

    Check to see if you already have git installed by typing the following:

    git --version
    

    If you see a version number, then you’re good to go. If the git command isn’t found, then download the latest version of the graphical Git installer from thegit-osx-installer downloads page. Once it has finished downloading, simply double-click the .dmg file to start the installation process.

  3. With that out of the way, install RVM by going back to your Terminal prompt and typing (or copying and pasting) the following:

    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. When the RVM installation is complete (it’s fairly quick), reload your Terminal shell environment by typing

    source ~/.bash_profile
    

    This knits RVM into your environment and causes RVM to be loaded into any new Terminal sessions.

  5. Speaking of which, go ahead and 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 that you have RVM installed, you’re ready to install Ruby 1.9.3. To do that, type

    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 (it’s under the ~/.rvm directory). This may take a while, so feel free to grab a refreshing beverage or a tasty snack.

  7. When the installation is done and you’re back at a command prompt, 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.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0]
    
  9. Now set Ruby 1.9.3 as the default version to be used whenever you open any new Terminal sessions by typing

    rvm --default 1.9.3
    

     

  10. Finally, although not required, you’ll likely want to generate the Ruby core documentation by typing

    rvm docs generate
    

    This takes a little while to complete, but in the end you’ll have all the Ruby documentation at your fingertips.

  11. Setting up RVM may seem like a lot of work, but here’s the payoff: you can now easily switch between Ruby versions. For example, 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
    

    And when a new version of Ruby comes along, you can easily install it alongside your existing versions.

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.7 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.1 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 tohttp://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.

     

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: