How to Manage Multiple Versions of Ruby on Windows

I had recently started work on a Rails project and was setting up my local dev environment which I wanted to make it as similar as possible to the environment on the server I will deploy to.

The remote server currently runs Ruby 1.9.2 and Rails 3.x, so this is what I installed on my local machine.

Whilst ensuring that everything worked as expected, I noticed was that WEBrick was very slow to boot. However, as this only needs to be done once (or occasionally twice) per session, I didn’t think much more of it.

I then started coding, wrote some simple tests then ran rake test from the console. And waited, and waited, and waited…

My minimal set of tests took over one and a half minutes to run. WTF?!? This was something that would seriously slow me down.

I did some Googling and found out that I wasn’t the only one who was having issues with Rails 3 and Ruby 1.9.2. I read through a couple of questions on StackOverflow and found that one proposed solution was to upgrade to 1.9.3.

Hmmm… I wanted to keep Ruby 1.9.2 installed, but needed a way to speed things up, so I started looking for ways to manage multiple Ruby versions on Windows (yes, I know, Windoof, but just remember that you cannot play Skyrim on Linux without performing a backwards somersault through your own sphincter).

That’s when I discovered pik.

Installing pik

pik bills itself as a tool to manage multiple versions of Ruby on Windows which can be used from the Windows command line (cmd.exe), Windows PowerShell, or Git Bash. It sounded ideal for the job.

In order to install pik you need a working version of Ruby. No problemo, I had already installed 1.9.2 using the mighty RubyInstaller.

After that, as you can install pik via rubygems, it’s a matter of opening a command prompt and typing:

$ gem install pik

If the gem installs successfully, you’ll see a message telling you to use the pik_install script to install the pik executable. You need to install it somewhere that’s located in your PATH, but not your ruby/bin directory.

I did this (taking care with the backslash):

$ pik_install C:\pik

As C:/pik isn’t in my PATH, I had to add it manually and restart the PC. Here’s a brief tutorial on how to do that (alter your PATH variable, not restart the PC).

So following the reboot, it was time to install Ruby 1.9.3. I opened a command prompt and typed:

$ pik

And pik obligingly added my current Ruby version:

** Adding:  192: ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
Located at:  C:\Ruby192\bin


$ pik install ruby 1.9.3
** Adding:  193: ruby 1.9.3p429 (2013-05-15) [i386-mingw32]
 Located at:  C:\Users\Me\.pik\rubies\Ruby-193-p429\bin

And that was all there was to it.

Configuring pik

To list the ruby versions recognized by pik I typed:

$ pik list
* 192: ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
193: ruby 1.9.3p429 (2013-05-15) [i386-mingw32]

And could switch between them thus:

$ pik 193
$ ruby -v
ruby 1.9.3p429 (2013-05-15) [i386-mingw32]

Feeling pretty happy I navigated to my Rails project directory, switched to Ruby 1.9.3 and tried to start the server with rails s, yet to my dismay I saw:

The command "rails" is either spelt incorrectly or could not be found

It seems that Ruby 1.9.3 had failed to recognize the gems I had installed using Ruby 1.9.2. Drat!!

I did some Googling and found that by setting gem_home in pik’s configuration, you could share gem sets.

pik use 192
gem env
=>  - GEM PATHS: C:/ruby192/lib/ruby/gems/1.9.1

pik config gem_home=C:/ruby192/lib/ruby/gems/1.9.1

pik use 193
pik config gem_home=C:/ruby192/lib/ruby/gems/1.9.1

Following this, my Ruby 1.9.3 version recognized all of the gems installed under 1.9.2 and although I could run scripts which required other gems (FXRuby for example), annoyingly I still got the same error message when I tried to start the rails server.

So, I reverted gem_home to its original value for both Ruby versions, switched to Ruby 1.9.3 and installed Rails a second time.

This was a bit of a clunky solution, but it worked and for that I was grateful. I posted a question on the pik mailing list asking how I could solve this issue, but as of yet, I have received no reply.

Back in my Rails project directory I ran my tests again (using Ruby 1.9.3) and they completed within 20 seconds.

What a difference!

Further pik commands

$ pik help commands – Lists all available commands

$ pik list -r – Lists available remote Ruby versions

$ pik 192 – Short cut for $ pik use 192

Useful references

This post currently has 2 responses

  1. Jeff Nyman says:

    Something to keep in mind: on Windows you need to make sure the DevKit for each Ruby version is established. This gets tricky when you want Ruby 2.0 on your machine along with prior Ruby versions because there are different DevKits for the 2.x branch versus previous branches.

    • says:

      Thank you for pointing that out, Jeff. This is a detail I forgot to mention.

Comments are closed!