Cairo with Ruby - Installation on Windows

Getting Cairo Graphics to work with Ruby on Windows is straightforward although it scares you a bit in the installation.

In short, using rcairo works perfectly fine on Windows at least with Ruby 2.6. Make sure that you have a full Ruby installation along with the DevKit (follow instructions on this page for example but grab Ruby 2.6 or Ruby 2.7 instead). I just have not had the chance to try this with Ruby 3.0 yet.

Although the project is called rcairo, the gem is just cairo – so, you can install it as gem install cairo – this will throw up a bunch of text including some errors but don’t worry, it installs fine.

$ gem install cairo
Fetching pkg-config-1.4.6.gem
Fetching cairo-1.17.5.gem
Fetching red-colors-0.1.2.gem
Fetching native-package-installer-1.1.1.gem
Successfully installed red-colors-0.1.2
Installing required msys2 packages: mingw-w64-x86_64-pkg-config
warning: mingw-w64-x86_64-pkg-config-0.29.2-1 is up to date -- skipping
Successfully installed pkg-config-1.4.6
Successfully installed native-package-installer-1.1.1
Temporarily enhancing PATH for MSYS/MINGW...
Installing required msys2 packages: mingw-w64-x86_64-cairo
warning: dependency cycle detected:
warning: mingw-w64-x86_64-harfbuzz will be installed before its mingw-w64-x86_64-freetype dependency
error: failed retrieving file 'mingw-w64-x86_64-bzip2-1.0.8-1-any.pkg.tar.xz' from repo.msys2.org : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-bzip2-1.0.8-1-any.pkg.tar.xz' from sourceforge.net : Maximum file size exceeded
error: failed retrieving file 'mingw-w64-x86_64-mpdecimal-2.4.2-1-any.pkg.tar.xz' from repo.msys2.org : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-mpdecimal-2.4.2-1-any.pkg.tar.xz' from sourceforge.net : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-tk-8.6.10-1-any.pkg.tar.xz' from repo.msys2.org : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-tk-8.6.10-1-any.pkg.tar.xz' from sourceforge.net : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-graphite2-1.3.13-2-any.pkg.tar.xz' from repo.msys2.org : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-graphite2-1.3.13-2-any.pkg.tar.xz' from sourceforge.net : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-harfbuzz-2.6.4-3-any.pkg.tar.xz' from repo.msys2.org : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-harfbuzz-2.6.4-3-any.pkg.tar.xz' from sourceforge.net : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-freetype-2.10.1-1-any.pkg.tar.xz' from repo.msys2.org : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-freetype-2.10.1-1-any.pkg.tar.xz' from sourceforge.net : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-fontconfig-2.13.1-1-any.pkg.tar.xz' from repo.msys2.org : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-fontconfig-2.13.1-1-any.pkg.tar.xz' from sourceforge.net : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-pixman-0.38.4-1-any.pkg.tar.xz' from repo.msys2.org : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-pixman-0.38.4-1-any.pkg.tar.xz' from sourceforge.net : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-cairo-1.16.0-1-any.pkg.tar.xz' from repo.msys2.org : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-cairo-1.16.0-1-any.pkg.tar.xz' from sourceforge.net : The requested URL returned error: 404
Building native extensions. This could take a while...
Successfully installed cairo-1.17.5
Parsing documentation for red-colors-0.1.2
Installing ri documentation for red-colors-0.1.2
Parsing documentation for pkg-config-1.4.6
Installing ri documentation for pkg-config-1.4.6
Parsing documentation for native-package-installer-1.1.1
Installing ri documentation for native-package-installer-1.1.1
Parsing documentation for cairo-1.17.5
Installing ri documentation for cairo-1.17.5
Done installing documentation for red-colors, pkg-config, native-package-installer, cairo after 4 seconds
4 gems installed

I have not yet found if there are any problems created due to the errors about packages that it could not find, but I was able to run a simple sample program and it worked fine. You can see that it builds native extensions successfully and that probably handles it. Running the sample generated this.

A related gem is pango that supports more complex text layout and you can install it with gem install pango – again, this installs fine (while building native extensions along the way). This builds 4 native extensions and it takes some time (a few minutes on my PC), so have some patience.

gem install pango
Fetching pango-3.4.4.gem
Fetching cairo-gobject-3.4.4.gem
Fetching glib2-3.4.4.gem
Fetching gobject-introspection-3.4.4.gem
Temporarily enhancing PATH for MSYS/MINGW...
Installing required msys2 packages: mingw-w64-x86_64-glib2
warning: mingw-w64-x86_64-glib2-2.64.1-1 is up to date -- skipping
Building native extensions. This could take a while...
Successfully installed glib2-3.4.4
Installing required msys2 packages: mingw-w64-x86_64-gobject-introspection
Building native extensions. This could take a while...
Successfully installed gobject-introspection-3.4.4
Building native extensions. This could take a while...
Successfully installed cairo-gobject-3.4.4
Installing required msys2 packages: mingw-w64-x86_64-pango
error: failed retrieving file 'mingw-w64-x86_64-pango-1.43.0-3-any.pkg.tar.xz' from repo.msys2.org : The requested URL returned error: 404
error: failed retrieving file 'mingw-w64-x86_64-pango-1.43.0-3-any.pkg.tar.xz' from sourceforge.net : The requested URL returned error: 404
Building native extensions. This could take a while...
Successfully installed pango-3.4.4
Parsing documentation for glib2-3.4.4
Installing ri documentation for glib2-3.4.4
Parsing documentation for gobject-introspection-3.4.4
Installing ri documentation for gobject-introspection-3.4.4
Parsing documentation for cairo-gobject-3.4.4
Installing ri documentation for cairo-gobject-3.4.4
Parsing documentation for pango-3.4.4
Installing ri documentation for pango-3.4.4
Done installing documentation for glib2, gobject-introspection, cairo-gobject, pango after 4 seconds
4 gems installed

The rcairo repository has a sample that uses pango which also runs normally after the installation is completed. The image below was generated from running that sample which uses both pango and cairo. It’s been a while since I worked with Cairo, so I’m excited!

There is always a bit of a worry about whether native extensions work fine on Windows, so I wrote this up just to note that these gems (cairo and pango) work perfectly fine and build native extensions as required successfully. Also, this helps me remember what had happened in case I need it on a different computer. Of course, if it helps someone, that’s great! If you have some comments, please add below so that I can reflect changes here.

If you are setting up Ruby on Windows, take a look at Installing JRuby on Windows and Installing Ruby 3.0 on Windows both on this site itself.

Update on the warning

I raised an issue on rcairo GitHub and got the possible solution that the RubyInstaller DevKit MSYS2 repository probably needs to be updated. You can achieve this by doing this before doing the gem install cairo command.

ridk exec pacman --refresh --sync --noconfirm

Hope this helps!

comments powered by Disqus