As you may have heard, Rails 3.0 final and Ruby 1.9.2 have been released. These have got a large number of features, with both representing the next stage of Ruby development.
While Rails 3.0 won’t cause too many problems, it does require Ruby 1.8.7 or higher. As Ruby has grown, multiple versions have been released which can live happily side-by-side. Sadly, command lines don’t make it easy to specify which version you want to execute.
For example, I have two versions of MSBuild on my machine with my command prompt being aware of both:
>where msbuild
C:WindowsMicrosoft.NETFrameworkv4.0.30319MSBuild.exe
C:WindowsMicrosoft.NETFrameworkv3.5MSBuild.exe
However, by default, it will execute the one it finds first when scanning the directories set in the PATH:
>msbuild
Microsoft (R) Build Engine Version 4.0.30319.1
The same is true with Ruby. Yet, this is complicated by the fact that there are multiple different executables (gem, rake, cucumber, spec, rails etc) together with different gems depending on the core Ruby version – 1.8 and 1.9 will have different gems installed even on the same machine.
To help manage this, the Ruby Version Manager (RVM) was created. “RVM is a command line tool which allows us to easily install, manage and work with multiple ruby environments from interpreters to sets of gems”
Sadly, RVM doesn’t work on Windows. Thankfully, Pik does – “Pik is a tool to manage multiple versions of ruby on Windows”.
This means we can do the following:
>ruby -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
>pik switch 192
>ruby -v
ruby 1.9.2p0 (2010-08-18) [i386-mingw32]
Below is how I installed Pik, along with Ruby 1.9.2 and Rails 3.0 to start the next part of my Ruby development journey.
Installing Pik
My machine already has Ruby 1.8.6 (ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]) installed which means I can install pik via RubyGems. If you don’t already have Ruby, the README file on GitHub has a section called “Install pik using the installer“ which I recommend you follow.
gem install pik
After downloading the gem, you need to install pik into a directory on your machine. This can be anywhere, apart from where you already have Ruby installed (C:rubybin).
>mkdir C:pik
You need to include the directory used above in your %PATH% environment variable before the location of any existing Ruby installation. I put it at the start.
When installing, simply specify the directory you picked.
>pik_install C:pik
Thank you for using pik.
After which, you will have three files in the folder. This is everything required for pik.
29/08/2010 17:08 119 pik.bat
29/08/2010 17:08 145 pik.ps1
29/08/2010 17:08 694,272 pik_runner.exe
Using Pik
I can now start working with Pik. Executing the pik command will locate all existing Ruby installations and configure itself.
>pik
** Adding: 186: ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
Located at: C:Rubybin
Usage: pik command [options]
Executing pik list outputs all the ruby installations it knows about.
>pik list
* 186: ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
As Rails 3.0 requires 1.8.7 or higher, let’s install the latest version of Ruby.
>pik install ruby
** Downloading: http://rubyforge.org/frs/download.php/71175/ruby-1.9.2dev-preview3-i386-mingw32-1.7z
to: C:UsersBen Hall.pikdownloadsruby-1.9.2dev-preview3-i386-mingw32-1.7z
Sadly, this still tried to install a dev preview, even with the latest version released. Luckily, it can be installed manually.
Manually installing Ruby 1.9.2
By default, installing Ruby on Windows can be somewhat difficult. Thankfully, a 1.9.2 one click installer has been released which you can download here – rubyinstaller-1.9.2-p0.exe
After clicking next a few times, I installed Ruby into C:Ruby192.
I then need to tell pik about my installation by pointing it at the bin directory. That’s it.
>pik add C:Ruby192bin
** Adding: 192: ruby 1.9.2p0 (2010-08-18) [i386-mingw32]
Located at: C:Ruby192bin
If you list pik, then you can see all the different versions installed:
>pik list
* 186: ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
192: ruby 1.9.2p0 (2010-08-18) [i386-mingw32]
You can then switch to the particular version you want, for example originally I was running 1.8.6.
>ruby -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
But I can then switch to 1.9.2 with a simple command.
>pik switch 192
>ruby -v
ruby 1.9.2p0 (2010-08-18) [i386-mingw32]
Installing Rails 3.0
That’s the hard bit done. I didn’t have install pik but during the transition between 1.8.6 and 1.9.2 I *think* this will be invaluable. With 1.9.2 now configured and set as my current pik environment I can just install rails.
>gem install rails
Yep, that’s it.
Rails 3.0 Hello World!!
To create a new rails app, let’s just execute the command:
>rails new HelloWorld3
Followed by:
>rails server
=> Booting WEBrick
=> Rails 3.0.0 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-08-29 18:01:27] INFO WEBrick 1.3.1
[2010-08-29 18:01:27] INFO ruby 1.9.2 (2010-08-18) [i386-mingw32]
[2010-08-29 18:01:27] INFO WEBrick::HTTPServer#start: pid=7472 port=3000
I now have our Rails 3.0 application running on top of Ruby 1.9.2 on Windows.
If I ever need to go back to Rails 2.3.5, I just type the following:
>pik default
>rails -v
Rails 2.3.5