RMagick on Windows

March 18, 2013 at 8:51 AM PDT

RMagick on Windows is tricky. Recently, I wrote up an answer on Stack Overflow on ways to get it to work under Windows with Rails. Unfortunately, my answer hasn’t received much attention.

I have managed to find two solutions to install RMagick and getting it to work with Rails back when I was on Windows. These solutions aren’t specific to Rails.

The Simple Solution

The easiest solution is to install the ancient rmagick-2.12.0-x86-mswin32 gem. To get this specific gem version to work with Bundler, you will need to add the following to your Gemfile.

if RUBY_PLATFORM =~ /(win|w)32$/
  gem 'rmagick', '2.12.0', :path => 'vendor/gems/rmagick-2.12.0-x86-mswin32', :require => 'RMagick'
  gem 'rmagick', :require => 'RMagick'

Note the path argument there. You will need to place the gem in a location where Bundler can find it. This example uses vendor/gems/ for its location. You will need to unpack the .gem file to this location.

gem unpack rmagick-2.12.0-x86-mswin32 vendor/gems/

A Better Solution

The provided Windows gem is heavily outdated and it is intended for Ruby 1.8.6, meaning there’s no guarantee that it will work with future Ruby versions. It is possible to compile a newer version of the RMagick gem on Windows using DevKit. You will need a 32-bit version of ImageMagick installed with development headers.

I have created a batch file that maps the directory of ImageMagick to X:\ and it gives the parameters to RubyGems on where to find the required files to build RMagick. This sort of mapping is necessary as the configuration options don’t know how to handle spaces in the paths. Alternatively, you can install ImageMagick to a location that has no spaces in its path and avoid binding it to a drive letter altogether.

The following commands map the directory of ImageMagick to X:\ and have RubyGems compile and install RMagick.

subst X: "C:\Program Files (x86)\ImageMagick-6.7.6-Q16"
gem install rmagick --platform=ruby -- --with-opt-lib="X:\lib" --with-opt-include="X:\include"
subst X: /D

The path in this example will need to be modified if you have a version other than 6.7.6-Q16 installed or if you are not on 64-bit Windows.

If you are using Bundler, a much nicer one liner in Gemfile is all that is needed with this solution.

gem 'rmagick', :require => 'RMagick'