Sunday, December 11, 2011

Getting Started with Ruby Development on Windows

This post is about getting a usable Ruby environment setup and configured on your Windows computer.  I assume that you are familiar with some form of software development but that you are completely new to Ruby.  Download the Ruby installer and the Ruby Development Kit from the RubyInstaller site.  I am using Ruby 1.9.2 (rubyinstaller-1.9.2-p290.exe) and the current DevKit (DevKit-tdm-32-4.5.2-20110712-1620-sfx.exe) I chose to use Ruby 1.9.2 even though Ruby 1.9.3 is out because there is an issue using the Ruby-debug-base19x gem with Ruby 1.9.3.  Currently, you would have to build your own Ruby from source to be able to use the ruby-debug-base19x gem with Ruby 1.9.3 and that is beyond the scope of this blog post. If you are going to use an IDE like RubyMine or eclipse for your Ruby development, then you will want to be able to interactively debug your Ruby code using the ruby-debug gem.

Run the Ruby installer that you just downloaded. If you are planning on using multiple Ruby installs, don’t check any of the boxes on the Installation Destination and Optional Tasks installer page.  These options include:

  • Install Tcl/Tk support
  • Add Ruby executables to your Path
  • Associate .rb and .rbw files with this Ruby installation. 

I always choose to install my development tools into the C:\Dev folder.  So for this install I chose C:\Dev\Ruby192. Later we will be creating batch files that set up the proper environment for each Ruby installation.  Next we need to setup the development kit.  This kit will provide you with all of the C language tools that you may need to compile and build ruby gems.  Many gems come with native extensions written in the C programming language.  The executable, DevKit-tdm-32-4.5.2-20110712-1620-sfx.exe, is a self-extracting executable.  Just run the executable and it will ask you for the location to which you should extract the files.  I chose to use the folder C:\Dev\RubyDevKit. 

Launch a command window with the Ruby interpreter on the PATH. In the Start Menu, choose All Programs –> Ruby1.9.2-p290 –> Start a Command Prompt with Ruby. Navigate to the Ruby DevKit installation and execute theses commands:

ruby dk.rb init
ruby dk.rb install

Further documentation for the Ruby DevKit can be found here.  I use my own batch files to set up the Ruby environment.  I do this because I may have different command windows open with different Ruby environments. I find it easy to start up a command window (cmd.exe) and just run the batch file for the Ruby environment that I wish to use.  In the C:\Dev folder, create a text document name ruby192env.bat.  The contents of this file will look like this:

@echo off

@echo         Setting environment for Ruby 1.9.2 tools

set Path=C:\Dev\Ruby192\bin;%Path%
call C:\Dev\RubyDevKit\devkitvars.bat

title Ruby 1.9.2

This file will insert the Ruby interpreter and DevKit tools at the beginning of your PATH environment variable when run.  I have similar files for all of the Ruby versions installed on my computer.  Let’s test the ruby installation, fire up a command window and execute the command:


This gets our Ruby environment ready to go. Next run the interactive ruby shell, irb. At the irb prompt type:

puts ‘Hello, World!’

Below is the output on my computer:

irb(main):001:0> puts 'Hello, World'
Hello, World
=> nil

If you get similar output to the above, then everything is installed correctly. Press Ctrl + d to exit irb. Now that we have a fresh Ruby installation and the DevKit for building native extensions, let’s get some gems!  Gem is Ruby’s package management tool.  If you come from the Python world, it’s like pip only much better. Gem will allow you to find and install Ruby gems (libraries).  Gems are just libraries of code that you can download into your Ruby installation so that they are available for your use.  For example, Rails can be installed with the command:

gem install rails

I don’t recommend that you actually install rails at this point.  First we need the debugging packages that will allow us to step through our Ruby code in IDEs like RubyMine and eclipse.  Go back to your command window (or fire up a new one, don’t forget to run the ruby192env.bat file) and execute these commands:

gem install ruby-debug-base19x
gem install ruby-debug-ide

Here is the output of these commands on my computer:

C: >>gem install ruby-debug-base19x
Fetching: columnize-0.3.5.gem (100%)
Fetching: archive-tar-minitar-0.5.2.gem (100%)
Fetching: ruby_core_source-0.1.5.gem (100%)
Fetching: linecache19-0.5.12.gem (100%)
Building native extensions.  This could take a while...
Fetching: ruby-debug-base19x-0.11.29.gem (100%)
Building native extensions.  This could take a while...
Successfully installed columnize-0.3.5
Successfully installed archive-tar-minitar-0.5.2
Successfully installed ruby_core_source-0.1.5
Successfully installed linecache19-0.5.12
Successfully installed ruby-debug-base19x-0.11.29
5 gems installed
Installing ri documentation for columnize-0.3.5...
Installing ri documentation for archive-tar-minitar-0.5.2...
Installing ri documentation for ruby_core_source-0.1.5...
Installing ri documentation for linecache19-0.5.12...
Installing ri documentation for ruby-debug-base19x-0.11.29...
Installing RDoc documentation for columnize-0.3.5...
Installing RDoc documentation for archive-tar-minitar-0.5.2...
Installing RDoc documentation for ruby_core_source-0.1.5...
Installing RDoc documentation for linecache19-0.5.12...
Installing RDoc documentation for ruby-debug-base19x-0.11.29...

C: >>gem install ruby-debug-ide
Fetching: ruby-debug-ide-0.4.16.gem (100%)
Building native extensions.  This could take a while...
Successfully installed ruby-debug-ide-0.4.16
1 gem installed
Installing ri documentation for ruby-debug-ide-0.4.16...
Installing RDoc documentation for ruby-debug-ide-0.4.16...

Now you will be able to breakpoint on, and step through code in your favorite IDE that supports Ruby development.  I am currently using RubyMine by JetBrains. A professional license can be obtained at a very reasonable cost and they offer a free license for open source development. A lot of Ruby programmers use vim as their Ruby development IDE.  If you have never used Linux or any Unix like system and don’t know what vim is, then just stick with RubyMine.

There are many more useful gems you will want to install.  It really depends on your goals.  First and foremost I would recommend installing rspec and cucumber.  These are BDD style testing tools and each deserves its own blog post. 

For your next step into Ruby, do yourself a favor and download and complete the Edgecase Ruby Koans.  These koans are an excellent introduction to the capabilities of the Ruby programming language.  You will not need any additional gems to run the koans.