Wednesday 6 July 2011

Install Ruby with TK on Windows XP

Developping a small graphic cross-platform application with Ruby? Seems TCL/TK is the way to go as Ruby support for TK is part of the core development, and TCL/TK is available for most platforms (Windows/MAC/Linux). Even if everything for Ruby/TK is packaged by default on MAC OS X (and I guess Linux), it's not the same for Windows. The Windows RubyInstaller does not provide TK extentions anymore, and despite the numberous Howtos on the net, none really precises which version of each component you need. And the different versions require different installations. So here is a quick how to install Ruby with TK support on Windows XP.

Basically you have two options:
  • Using Ruby 1.8. In this case you need to install some existing ruby tk binaries.
  • Using Ruby 1.9. In this case you need the ruby tk gem to build the binding yourself. I didn't managed to get this option working, but I still explain here how the installation works.

Install TCL/TK

This step is common to both installations. Use ActiveTcl convenient installer you can find here: http://www.activestate.com/activetcl
For Ruby 1.8 you need version 8.4 instead of 8.5 (the latest at this time).

Install Ruby

Use the convenient RubyInstaller for Windows you can find here: http://www.ruby-lang.org/en/downloads/

Install TK binding for Ruby 1.8

Get the archive at https://github.com/rdp/ruby_windows_tk and decompress it on your drive. You just need to cd in this folder and run:

ruby install.rb
This binary installation is meant for Ruby 1.8.5, but it seems to work pretty well for Ruby 1.8.7 (latest at this moment).

Install TK binding for Ruby 1.9

In this case you will need to build the binding yourself, so first we have to install the RubyInstaller Development Kit. It contains the compiler dans files to comile gems and you can download it there:http://rubyinstaller.org/downloads/

Note to install the devkit you have to follow these few steps (taken from: https://github.com/oneclick/rubyinstaller/wiki/Development-Kit)
  • Run the bin and uncompress data in <DEVKIT_INSTALL_DIR>
  • Start Command Prompt With Ruby
  • cd <DEVKIT_INSTALL_DIR>
  • ruby dk.rb init
  • ruby dk.rb install
Now let's install the tk gem. As explained here https://github.com/rdp/tk_as_gem, Ruby TK gem is a little hack with the regular Ruby 1.9 sources for TK embedded in a gem. To build it use the following command line:
gem install tk_as_gem -- --with-tcl-dir=c:\Tcl --with-tk-dir=c:\Tcl
Testing Ruby TK

Now everything is in place, run the follogin code in the ruby console:

require 'tk'
root = TkRoot.new { title "Ex1" }
TkLabel.new(root) {   text  'Hello, World!'   pack  { padx 15 ; pady 15; side 'left' } }
Tk.mainloop

This should display a simple window with a label.

No comments:

Post a Comment