Step by step tutorial on installing Ruby on Rails 4 on Windows 8

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

It is instructive to learn to install all the pieces of Ruby on Rails step-by-step manually.  This tutorial will walk you through installing this on a development environment on Windows 8.

1. Install Ruby

Ruby on Rails is a web framework written in the Ruby programming language.  So Ruby is the base.  We will install that first.  Ruby Installer at RubyInstaller.org is a great way to install Ruby for Windows.   RubyInstaller is not to be confused with RailsInstaller.

For this example, we downloaded the Ruby Installer 1.93.  For later versions of installers, you might have to download the right one based on the version of your Windows operating system whether it is 32-bit or 64-bit.

Since Ruby Installer is a self-contained Windows installer, just double-click on the executable and run it.

2. Installation Options

In our case, we are installing Ruby 1.9.3 and have chosen the installation options as…

  • Install tcl/tk support
  • Add Ruby executable to PATH
  • Associate .rb and .rbw files with Ruby

3. Confirm that Ruby is installed

After installation, see that it saved ruby in c:\ruby193 and added Ruby to the Windows PATH environment variable.  Also see that a new Windows tile have been created on your start page that says “Start Command Prompt With Ruby”

ruby tile on Windows Start page

Click on that to get to the command prompt.

Now create a new folder in Windows at c:\Sites

That will be the location of where we will place our Ruby apps.   At the Command Prompt, it is best to navigate to this directory before typing Ruby commands.

4.  Ruby Command Prompt

At the command prompt in the C:\Sites directory, type …

ruby -v

That will give you the version of your Ruby.  Our is 1.9.3 patch 429.

Ruby command line and irb

You can run a Ruby statement at the command prompt by using -e option by typing …

ruby -e "puts 'This is ruby'"

This runs the Ruby statement …

puts 'This is ruby'

which displays a string to the screen.  Typing “ruby -h” will give you help on the various ruby prompt switch options.

5.  Interactive Ruby

You can run much more extensive Ruby code using Interactive Ruby (irb).  Start that by typing “irb” at the prompt.  See image above.

Type some Ruby code.  Here we typed “7+8”.  Note that Ruby does not need to terminate statements with semicolons.

See that it outputs 15.  Type “exit” to exit irb.

6.  Ruby Gems

Another thing that is installed is RubyGems (from RubyGems.org).  At the command prompt, run “gem -v” to see what version of RubyGems you have.

ruby gem

Type “gem list” to see what gems are currently installed.  A gem is a Ruby package.  And RubyGems is like a package manager/installer.

To make sure that you have the latest version of RubyGems you can update it by …

gem update --system

Note that there is two hyphens.

Typing “gem -h” will give help.

7.  Rails is a Gem

Rails is actually a Ruby gem.  Let’s find this gem on RubyGems.org …

rails on rubygems

This confirms that rails is a gem that is spelled “rails” (as opposed to “ror” or “rubyrails” or etc).  And the current version is 4.0.

8.  Installing Rails

So we can install it using RubyGems by typing …

gem install rails

(spelled as it was found on rubygems.org).   But if you run this command now, it will fail with an error …

need devkit

because we forgot to install Ruby DevKit first (see below steps).

9.  Dependency Error installing Rails

Sometimes gems have certain dependencies.  In our case, we ran into this error saying “The ‘atomic’ native gem requires installed build tools” and something about needing DevKit.

We will now install the Ruby DevKit.  We can get it from RubyInstaller.org …

ruby devkit

10.  Installing DevKit

Extract the DevKit zip file into c:\Ruby193\devkit.

Navigate to c:\Ruby193\devkit and run “ruby dk.rb init” and then “ruby dk.rb install“…

installing devkit

This makes Ruby aware of DevKit.

11.  gem install rails

At the command prompt, navigate back to c:\Sites and run the “gem install rails” command again.  And this time it should work.  The command takes a while before it shows its progress.  And when progress does start, it will take a long install.  That is because it got a lot to do.

If you get error saying …

Could not find a valid gem ‘rails’.  Unable to download data“.

Then check your spelling.  We did saw on RubyGems.org that “rails” is a valid gem.  So this could be a connection/server error and you might want to just try again.  If you get messages saying …

Unable to convert … from ASCII-8BIT to UTF-8

don’t worry too much about it.  It is just building the documentation.  Things will still work.

When our installation completed, we had 25 new gems installed.   Run “gem list” again and see a whole bunch of new gems were installed.  It includes the Rails gem as well as gems like actionpack and activerecord and others that are part of Rails.

 12.  ri docs

It also installed ri documentation for rails 4.0.

ri is the Rails help.  Type “ri” at the command prompt. It asks to enter a method.  Let’s enter “to_a” to see what that method does …

ri console

Type a blank line to exit.   You can also type ri along with the method from the command line like …

ri join

This will tell you more about the join method.

ri from command line

13.  Database for rails

Rails application typically do require a database.  You can install Sqlite or install MySQL database on your local Windows 8 development machine by following this tutorial.  For simplicity, we are going to use Sqlite.  We have installed that at c:\sqlite and added “c:\sqlite” to our PATH environment variable.

14. Creating a new Rails application

We are now going to generate a new Rails application. At the command prompt, navigate to the “c:\Sites” folder.  It is best to generate your Rails app here.

We’ll call our Rails app “firstruby“, so we type …

rails new firstruby

By default this will create the firstruby Rails application set to use the sqlite database.  If you wanted it to use MySQL, then you type …

rails new firstruby -d mysql

The “-d mysql” tells Rails that we intend to use MySQL database for this app.  If we had left it out, it would have defaulted to using SQLite for the database.

See that it had created the “firstruby” folder in “c:\Sites”.  This is your rails application.  If you ever want to delete this Rails application, delete the “firstruby” folder.

Inside the rails application is a “public” folder.  This is the Rails public folder that will be exposed to the public via the webserver.  This is where you put your web assests and code such as css, javascript, and images.

15.  Starting the Webrick server

In order for viewers to see our web app, we have to start up a webserver.  Let’s launch the small WEBrick server that comes bundled with Rails (although other webservers can also be configured to work).

Navigate inside your “firstruby” folder and then type at the command line …

rails server

Typing “rails s” does the same thing.

starting webrick

You might get a Windows Security Alert, we choose to select the options as shown above and allow access.

From the WEBrick console, we see that WEBrick webserver started on port 3000.  It also tells us to use Ctrl-C to stop server.

16. View Webapp in browser

That means that we can point our browser to http://localhost:3000 to see our first Rails web application.  localhost is same as 127.0.0.1 which is the IP address to refer to own local development machine.  You should see the Rails welcome page…

rails welcome page

17. View Application’s Environment

Click on the link “About your application’s environment” and see the specs of your app…

applications details

18.  Stop server

Whenever you are not working on your app, you should stop the Web server on your development environment.  We stop WEBrick with Ctrl-C …

stop web server

When asked to terminate job, type “y”

That’s it.  Your Rails environment is set up.  Your next step involves configuring and putting in Ruby code into your application.