Packaging a ruby script as an Windows exe using OCRA

I recently wrote a watir-webdriver ruby script that I needed to be able distribute to others to run on Windows machines that don’t have ruby installed.

I came across the OCRA gem that allows you to easily generate a windows executable from a ruby script. This packages the ruby interpreter and all dependencies into an executable file.

It was quite straightforward to get it working, you simply install the gem on windows and run the ocra command with the name of your ruby script.

The only (minor) issues I had were:

  • if you wish to access external files from your executable (such as a config file) just add ‘$:.unshift File.dirname($0)‘ at the start of your ruby file
  • if you are using ruby logging, then for some reason you can’t call logger.close as it crashes OCRA, but you can just not close the logger which is fine
  • for some reason on Windows I needed to explicitly require ‘securerandom’ to use SecureRandom.uuid whereas it just worked on Mac OSX

Once I resolved these it quickly generated an executable which was runnable without any version of ruby installed. Neat.

Author: Alister Scott

Alister is an Excellence Wrangler for Automattic.