Aug
26

Compiling Ruby with MinGW

on August 26th, 2009 by admin

Initially I used the binary distributions of Ruby. However, when new Ruby patch levels are released, it is not always easy to find the binaries – if you are like me, and prefer to run the latest versions, then you also need to learn to compile Ruby for yourself.

This article will help people who are on Windows platform. While the Ruby language developers say that you should use Microsoft VC++ 6.0 – and with some gems the binary compatibility in fact does matter -, I will use the open source MinGW GNU compiler collection, simply because I consider it a better match.

Let’s get started.

You go to ftp.ruby-lang.org to download the source code. At time of writing this article, the latest stable version is 1.9.1.p243, that you can find in the ‘pub/ruby/1.9′ folder. Download the zip file and extract it in your local ‘c:\temp\ruby’.

Should you decide to use MS VC++ 6, then look for the README.win32 file in the win32 folder. This file gives step by step, correct and straightforward instructions on what to do.

To setup the MinGW compiler, go to Sourceforge.com and download the MinGW installer. At time of writing this article, it is version 5.1.4.

Run the installer. Select ‘Download and Install’, agree to the license, select ‘Current’, select only ‘MinGW base tools’, and keep the default installation directory ‘c:\mingw‘. If you use a different directory, avoid spaces in the directory name. Complete the installation.

After the installation is complete, go the your Windows System Properties – Advanced – Environment Variables and edit the PATH, include ‘c:\mingw\bin’.

MSYS provides a Unix style shell script interpreter and basic Unix commands on Windows. Next, we install MSYS – at time of writing this the current version is 1.0.11. Accept the default directory ‘c:\msys\1.0′. You will have to answer a few simple questions in the post-installation procedure. Install MSYS DTK. Extract the MSYS Core archive to your MSYS installation directory. Finally set your HOME environment variable to ‘c:\msys\1.0\home’, then include ‘c:\msys\1.0\bin’ in your PATH.

The wiki on mingw.org provides additional information if needed.

Now, if you open a ‘cmd‘ command window and type ‘bash’, you are in the Unix-like environment.

Change directory to the location where you extracted the Ruby source files, ‘cd c:/temp/ruby/ruby-1.9.1-p243′.

We are ready to compile Ruby. Type ‘./configure –prefix=c:/ruby/1.9.1.243′. It will take a few minutes to build a make file for you. The prefix parameter tells where you want the Ruby build to be placed.

Type ‘make’, this will again take several minutes. When the make process completed, run ‘make test’. This will execute all the Ruby unit test scenarios that come with the language. Depending on the Ruby version and patch level that you are building, this test may result in a few error messages, if in doubt refer to discussions on the Ruby forum to find out whether you can ignore those errors. 1.9.1.243 gives a NoMethodError error while testing io, and RuntimeError in proc, but the tests complete. Finally, run ‘make install’ to deploy the build into the target directory.

You need to include the Ruby bin directory in the PATH, so add ‘c:\ruby\1.9.1.243\bin’.

To test the build, type ‘ruby -v’ in the command line.

You will see ‘ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32]‘. Great, we have built Ruby.

Next, you probably want to install the gem tool, so that you can use Ruby gems.

Let’s download the gem installer from Sourceforge. Take the zip file and extract it to ‘c:\temp\ruby’ – at time of writing this the current version is 1.3.5. In the command line, go to the rubygems folder and execute ‘ruby setup.rb‘. Gem will be installed. To verify, enter ‘gem’ in the command line.

Let’s now enter ‘gem list’. Oops… we have an error message.

c:/ruby/1.9.1.243/lib/ruby/site_ruby/1.9.1/rubygems/spec_fetcher.rb:1:in `require': no such file to load -- zlib (LoadError)

It is looking for some 3rd party libraries that were not included in the standard ruby distribution. We will need to do some more downloads. The garbagecollect.jp web site makes references to these 3rd party libraries. We will not download all, only the ones we need, but the process you follow should be the same.

For zlib we need a dll. You find zlib – current version 1.2.3 – on zlib.net. We download the binary dll version. Extract the zip file to ‘c:\temp\ruby\zlib‘. Since the Ruby bin folder is already in the path, let’s copy zlib1.dll into that folder. Next, we need to build the zlib Ruby extension, the zlib.so file. For this, go back to the Ruby source folder ‘c:\temp\ruby\ruby-1.9.1.-p243\ext\zlib‘. Running the extconf.rb program will build us the make file – and as you can see in the source code, the ‘dir_config ‘zlib” line tells us that we need to pass in a parameter called ‘with-zlib-dir’ with the value that points to the zlib root folder, under which there are the ‘include’ and the ‘lib’ sub-folders for the build.

‘ruby extconf.rb –with-zlib-dir=c:/temp/ruby/zlib‘ creates the makefile for us. And since we need to do a make again, make sure you are in the bash shell environment.

Execute ‘make’ and there you are, we have a zlib.so built against our current Ruby environment on MinGW. Copy zlib.so under the deployed Ruby build, namely to the ‘c:\ruby\1.9.1.243\lib\ruby\1.9.1\i386-mingw32′ folder, where you can see other .so files.

‘gem list’ works finally, and you will remember to do similar steps to build other extensions, for example openssl and others.

You probably want to continue your installation with adding ruby-debug and ruby-debug-ide so that you can comfortably develop under Netbeans or other IDE. See my other post about Ruby 1.9 debugging for the details.

updateTo compile the iconv extension on mingw, use the iconv library from this source. You will find this necessary as soon as you install the latest rails framework ( 2.3.4 at time of writing the article ). Copy both the binary files and the newly compiled .so file to their respecive locations based on above instructions.

Related Posts

  • Ruby 1.9.1 character encoding, Regular Expressions and Ruby on Rails
  • A smart Winamp play-list - the implementation
  • wxRuby application with Ruby threads
  • Surprising element in design
  • Ruby 1.9 debugger
Tags: programming, ruby
english - RSS Feed

Leave a Reply

Click here to cancel reply.

CAPTCHA Image
Refresh Image

    Recent Posts

    • GPU with CUDA
    • First impressions of the Mac OS
    • Snow Leopard in VMWare
    • CentOS 5 under Windows Virtual PC
    • Turek Miklós esküvőjére
    • Have I told you lately that I like Windows 7?
    • Királynő a sakktáblán
    • TrueIP under Windows 7
    • Virtualdub capture headache
    • Endless Summer

    Archives

    • September 2010 (1)
    • August 2010 (7)
    • July 2010 (2)
    • June 2010 (1)
    • April 2010 (2)
    • March 2010 (1)
    • January 2010 (2)
    • December 2009 (3)
    • November 2009 (6)
    • October 2009 (6)
    • September 2009 (8)
    • August 2009 (14)

    Categories

    • english (37)
    • magyar (16)

    életmód autó cloud fényképészet food gondolatok hírek humor ideas lesson learnt lifestyle logic logika matematika math mese movie music photography product review programming ruby science social software történelem television thoughts travel tudomány utazás video web zene

    WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.

    Blogroll

    • James Chik
    • Klement Vilmos

    Links

    • Home page
    • Photography

    Meta

    • Log in
Blog software by WordPress
Theme based on blue-fade by Web Considerations, LLC
Entries (RSS) and Comments (RSS)
35 queries. 0.284