Installing Ruby 1.9 and Rails 3 on Windows

Install Ruby 1.9

The best and easiest way we've found to install Ruby on Windows is using the RubyInstaller. It's a self-contained Windows installer (an .exe file) that includes a Ruby language execution environment, a baseline set of RubyGems, and documentation. Installers are available for a number of popular Ruby versions. We'll be installing Ruby 1.9.2, the latest Ruby version that's supported for use with Rails 3.
  1. On the RubyInstaller downloads page, download the file named Ruby 1.9.2-p290. You can either download the executable installer (recommended) or the 7-zip archive. The rest of the instructions assume you downloaded the executable installer.
  2. Once the executable installer has downloaded, use Windows Explorer to navigate to where you saved the .exe file and double-click it to start the installation process. Off you go, but pay attention to the prompts...
  3. After stepping through a couple standard installer screens, you'll be prompted for the installation destination. By default, Ruby will be installed in your C:\Ruby192 directory. We'll assume you go with the default destination, but you're not done! Make sure to check the box marked Add Ruby executables to your PATH. This will automatically update your PATH environment variable to include the C:\Ruby192\bin directory so you can use Ruby from any command prompt.
  4. Speaking of which, go ahead and open a new command prompt (Start -> Run..., and type cmd) and verify that Ruby 1.9.2 was successfully installed by typing
    ruby -v
    
    Ruby should reply with
    ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
    
    If instead you see "Command not found", then you either need to open a new command prompt and try again, or check that your PATH environment variable includes the C:\Ruby192\bin directory (see step #3).
  5. Finally, while you've got a command prompt open, verify that RubyGems 1.7.2 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 some operating systems. Unfortunately, Windows doesn't ship with SQLite3. So we'll need to download and install the SQLite3 DLL and command-line program. We'll also need to install the Ruby bindings to SQLite3. Thankfully, it's easier than it sounds.
  1. On the SQLite download page under the Precompiled Binaries For Windows section, download the sqlite-dll-win32-x86-3070701.zip file. This zip file contains two files: sqlite3.dll and sqlite3.def.
    While you're there, also download the sqlite-shell-win32-x86-3070701.zip file. This zip file contains the sqlite3.exe command-line program. It's not required for Rails development, but it's a handy way to poke around in your SQLite3 databases.
  2. Unzip both downloaded files, and you'll end up with the following three files:
    sqlite3.def
    sqlite3.dll
    sqlite3.exe
    
  3. Copy those files into your C:\Ruby192\bin directory.
  4. Next, go back to your command prompt and verify that the SQLite3 command-line program is installed by typing
    sqlite3 -version
    
    It should respond with 3.7.7.1.
  5. Finally, install the Ruby bindings to SQLite3 by typing
    gem install sqlite3
    
    You may see a warning about sqlite3 being built with an earlier version of SQLite. Pay it no attention.

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 (your C:\work directory, 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 taking our online Ruby Studio, 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!

Comments

Popular posts from this blog

Zeller's Congruence

Property Event-Delegation

Method with variable arguments