20
Aug

Installing Ruby on Rails with MySQL2 gem on Windows

Being a technical member of staff I’m generally expected to come up with at least one clever idea every now and then, apparently a coffee robot that wheels across the office delivering fresh Latte on demand doesn’t have a business benefit, and using facial detection on my laptop camera to electrify people that try and touch things on my desk breaks some kind of health and safety regulations.

So with the loose brief of “business benefit” and “nobody gets hurt” I settled on developing a collaboration tool that helps the company share documents more efficiently.

Obviously the first step was picking the right tools for the job, I settled on C# for the client application and it was then a decision between PHP and Ruby on Rails for the required web services.

Now I like PHP, my work laptop is Windows 8.1 and setting up a development environment is really extremely easy, it’s really just a case of download, install and get on with it.  The problem is I’d of gotten bored doing yet another PHP project so I decided on ruby on rails since I hadn’t touched it in a while, surely RoR under windows would be as good if not better?

A quick google search revealed that I could use railsinstaller.org, one thing to note is that it installs Ruby 1.9.3 which is the current recommended version, if you’re looking for 2.x then you’ll need to follow the guide on the ruby on rails site.

Note: This version of the installer is 32 bit, version 2.x(64 bit) still has issues with Windows so for the time being I’m leaving it alone.

Step 1.
Download the installer from here .  (ww.railsinstaller.org)

Run through the installer and leave everything on default….

As you can see the installer gives you Ruby, Rails, git, Sqllite and the Devkit once it’s finished we can try creating a project just to make sure it works:

Rails new testproject

Outputs the following (note this is the tail end of a lot of operations):

Great it worked! It probably installed a load of gems I don’t want but I can always go through and sort it out later.

Now I prefer to have my database off my development laptop simply because the memory usage can slow me down when I’m not developing it also means if I want to run some CPU intensive operations on the data I can set if off and carry on working(or play Doom).  On top of that if I’m working with a distributed team ensuring we all have the same data has its merits.

I have a VPS which I use for this sort of thing running CentOS and MySQL.

Cd testproject
gem install mysql2

C:Sitestestproject>gem install mysql2
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...

ERROR:  Error installing mysql2:
        ERROR: Failed to build gem native extension.
        C:/RailsInstaller/Ruby1.9.3/bin/ruby.exe extconf.rb
checking for ruby/thread.h... no
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=C:/RailsInstaller/Ruby1.9.3/bin/ruby
        --with-mysql-dir
        --without-mysql-dir
        --with-mysql-include
        --without-mysql-include=${mysql-dir}/include
        --with-mysql-lib
        --without-mysql-lib=${mysql-dir}/lib
        --with-mysql-config
        --without-mysql-config
        --with-mysql-dir
        --without-mysql-dir
        --with-mysql-include
        --without-mysql-include=${mysql-dir}/include
        --with-mysql-lib
        --without-mysql-lib=${mysql-dir}/lib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-mlib
        --without-mlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-zlib
        --without-zlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-socketlib
        --without-socketlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-nsllib
        --without-nsllib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-mygcclib
        --without-mygcclib
        --with-mysqlclientlib
        --without-mysqlclientlib

Gem files will remain installed in C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mysql2-0.3.16 for inspection.
Results logged to C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mysql2-0.
3.16/ext/mysql2/gem_make.out

C:Sitestestproject>

Well that’s the mother of all error messages isn’t it? Because it was at the bottom of the screen I checked the gem_make.out to see if it could give me some more information, helpfully it literally just repeated what I’d just seen. So then when we scroll back up a bit we come to:

“Could not create Makefile due to some reason(Wow thanks), probably lack of
necessary libraries and/or headers(Well that cleared that up..).  Check the mkmf.log file for more
details.  You may need configuration options.(Or a remotely helpful error message?)

So here’s where I’m going to save you the pain that I had to go through, what the error actually relates to is that in order to install this gem you need to MySQL libraries that come with either a MySQL server installation OR the MySQL connector, as per the documentation from the gems’ Github here (https://github.com/brianmario/mysql2), which incidentally isn’t anywhere near the top of google searches when you’re looking for this problem.

To keep things really simple we’re going to download the MySQL connector, for those of you that know what you’re doing you can adapt the following to point at a server install directory or copy the required files to somewhere accessible.

Grab the MySQL connector, do not download the installer, download the archive for your OS

Now the version of the RoR installer we’re using is 32 bit so we want the 32 bit archive of the MySQL connector. If you’ve downloaded the bleeding edge(2.x) version you’ll probably need the 64 bit archive.

You can get the archive (again, not he msi installer) here (http://dev.mysql.com/downloads/connector/c/)

Once it’s finished downloading open the archive, in windows 8.1 you’ll probably get the inbuilt zip file handler:


All you need to do now is extract these folders to the root of the drive that RoR is installed on so for me that’s C:.

A little tip here once its extracted rename the folder that’s rather inconveniently called “mysql-connector-c-6.1.5-win32” to “mysqlconnector” to make life easier.

Note I have been doing all of this in a command prompt, I’ve heard varying reports of command prompts on different versions of Windows needing slightly different syntax. If you’re experiencing problems with this then it’s most likely down to your PATH environment variable. The idea of using the rails installer is that it should have set all this for you, a restart may cure any PATH issues.

What we need to do now is run the gem install command again but this time tell it where the MySQL libraries are so….

gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:mysqlconnector"'

I made this a bit bigger so you can see the single and double quotes, enter this command and we should see the following:

C:Sitestestproject>gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:mysqlconnector"'
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
Successfully installed mysql2-0.3.16
1 gem installed
Installing ri documentation for mysql2-0.3.16...
Installing RDoc documentation for mysql2-0.3.16...

C:Sitestestproject>

All installed fine, if you still see the same error message as before try using the following command, this goes back to what I was saying about command prompts path.

gem install mysql2 --platform=ruby -- '--with-mysql-lib="C:mysqlconnectorlib" --with-mysql-include="C:mysqlconnectorinclude" --with-mysql-dir="C:mysqlconnector"'

If neither work then you’ve probably missed something! If you are 100% sure you haven’t leave a comment below. (Really, please double check before asking.)

The last thing is you need to copy libmysql.dll from C:mysqlconnectorlib TO C:RailsInstallerruby1.9.3bin.

Once we’ve got all this working don’t forget to add “gem ‘mysql2’” to your gemfile.

Now obviously we haven’t actually connected to a database as yet, that’s simply a case of pointing your configdatabase.yml at the server and supplying the correct details. So here’s mine:

development:

  adapter: mysql2

  encoding: utf8

  database: YOUR_DB_NAME

  username: YOUR_USER_NAME

  password: YOUR_PASSWORD

  host: YOUR_SERVER_IP

  port: 3306

That’s it you should be good to go, if you want to test your database connection you can do the following from your project directory, if you get an error when doing ‘rails console’ then run bundle install first and try again:

C:Sitestestproject> Rails console development
irb(main):006:0> ActiveRecord::Base::connection

This will print out your connection info OR an error message

irb(main):007:0>ActiveRecord::Base::connected?
=> true

Note here that if you do ActiveRecord::Base::connection you’ll either get an error or your connection information.

A point I’d like to raise here is that I find this to be a very serious problem a number of open source technologies. Windows development seems to be an afterthought, one day I’ll do a write up on my experiences with NodeJS and OpenCV on Windows at the moment I’m still attending counselling sessions until I’m ready to talk about it.

There’s a lot of hostility toward PHP in some quarters because of function naming, documentation etc, the thing a lot of them miss is that it-just-works and generally that’s all people care about, especially when they’re trying to learn something new. The fact that it’s the most widely used server side language on earth tells a story.

That about wraps up this article, the next one is going to talk you through a project with Unity 3D in C# I’m working on, specifically I’ll show you how to calculate parabolic trajectories for cannon balls and mortars(and anything else), not exactly work related but it’s maths, physics and explosions so I think it deserves some attention!

Written by Marc Fielding 

GROUP COMPANIES