We build Web & Mobile Applications.

< All Articles

ImageScience and RubyInline on Windows

UPDATE! Thanks to the work of Luis Lavena you can now download a Win32 gem of ImageScience: I have posted an updated set of instructions here. If you do decide to try following this guide to compile it yourself be sure to read Luis’ warning about mixing C runtimes.

According to the website ImageScience is a clean and happy Ruby library that generates thumbnails – and kicks the living crap out of RMagick. What it fails to mention is that by the time you’ve jumped through all the hoops necessary to get it running on Windows you’ll be qualified to start working as a professional acrobat.

RMagick is reasonably straightforward to install on Windows: download a special Win32 gem, run an installer and away you go! The problem with ImageScience is that it uses RubyInline to wrap the FreeImage library and in order for this to work you need a C compiler: Windows doesn’t have one installed by default.

As RMagick is easier to install you might question why I want to use ImageScience. The answer is consistency: this site uses ImageScience on the server and so I want to use it when developing locally on my Windows system. It took me a few attempts and lots of fruitless searches but I was eventually able to piece everything together and get it all working.

So here’s my step-by-step guide to installing ImageScience and RubyInline on Windows.

I’m using Windows XP SP2 but these instructions could also work for other versions of Windows. More importantly I’m using Ruby 1.8.6 which was built using Microsoft Visual C++. In order for RubyInline to work you must install the same compiler on your system that was used to build Ruby, so if you’ve built Ruby yourself using a different compiler then these instructions won’t work for you.

For simplicity I’ve used $RUBY_HOME to refer to the directory where you have installed Ruby (on my system it is installed in C:\Program Files\ruby).

1. Download and install Visual C++

The good news here is that you don’t need to spend any money: Visual C++ 2008 Express Edition is available for free. All you need to do is download and run the installer which will then download the necessary files (about 88 megabytes for a basic installation – you don’t need to install any of the optional components unless you really want to). You might want to make your first cup of coffee while you wait.

2. Update Ruby compiler settings

There are two changes that need to be made to the Ruby source: a compiler version check needs to be updated so that Visual C++ 2008 will work; and there are some deprecated compiler flags that need to be removed.

3. Configure environment variables

In order for the command line C compiler to find all of the files it needs it uses environment variables. You have two options here: you can always remember to use the Visual Studio 2008 Command Prompt icon that was added to the start menu by the installer in step 1 (look under Visual C++ 9.0 Express Edition → Visual Studio Tools) before you use Ruby; or you can update your system environment variables manually. If you choose the latter option, here are the variables you’ll need to update:

Note that if you installed Visual C++ to a directory other than C:\Program Files\Microsoft Visual Studio 9.0 you will need to change the above paths as appropriate.

4. Install RubyInline

Open a command prompt window and run gem install RubyInline. At the time of writing this will install RubyInline version 3.6.5. While it’s installing you might want to go and make your second cup of coffee.

5. Patch RubyInline

The RubyInline gem has a few Windows-specific problems that need to be fixed - this information comes courtesy of Hao Lian’s blog.

6. Test RubyInline

Go to the $RUBY_HOME\lib\ruby\gems\1.8\gems\RubyInline-3.6.5\demo directory and open a command prompt window. Type ruby hello.rb. If you don’t see hello world output then go back and check you’ve followed all of the above instructions because something has clearly gone wrong!

The good news is that we’re now almost there. Grab your third cup of coffee and prepare yourself for the final few steps.

7. Install the ImageScience gem

Open a command prompt window and run gem install image_science. At the time of writing this will install ImageScience version 1.1.3.

8. Patch ImageScience

Earlier this year a patch was posted by Luis Lavena to make ImageScience compatible with Windows and Visual C++. Download the patch and apply it to image_science.rb in $RUBY_HOME\lib\ruby\gems\1.8\gems\image_science-1.1.3\lib.

9. Download and install FreeImage

Download the FreeImage binary (at the time of writing version 3.10.0) and unzip to your desktop.

10. Test ImageScience

Open a command prompt window, type irb to start a Ruby console and type require ‘image_science'. You should see => true as the result: if you get a compilation error then check that you copied the FreeImage files to the correct locations in step 9.

That’s it: three cups of coffee and you’re finally done! You should now be able to use ImageScience and RubyInline in your Ruby code.

My next task is to figure out how to use RubyInline’s inlinepackage tool to build a pre-compiled version of ImageScience so that deployment on Windows becomes as simple as `gem install imagescience-win32`.


Here’s Luis Lavena’s warning about the danger of mixing C runtimes:

The problem with compiling ruby extension with other compilers besides VC6 is that the extensions will link to a different runtime than the one ruby is linked to (MSVCR80.dll for the extension, MSVCRT.dll for ruby). Also, FreeImage.dll with another runtime (afaik, cannot check it right now).

This poses a huge problem: memory allocated by one runtime cannot be freed by another version of the runtime, and that can lead to huge and unknown crashes in your application.

To avoid this problem you should use Win32 gem of ImageScience described here.

Updated on 07 February 2019
First published by Rob Anderton on 02 December 2007
© Rob Anderton 2019
"ImageScience and RubyInline on Windows" by Rob Anderton at TheWebFellas is licensed under a Creative Commons Attribution 4.0 International License.