[Ruby on Rails] – mysql2 with Ruby 2.4 on Windows

Developing Ruby on Rails on a Windows system is such a pain. But with some nice tips and tricks, it would be enjoyable.

In this tutorial, I will instruct you on how to install and use mysql2 gem version 0.4.10 (the last version that support rails 4.2) on a Windows system

1. Required software

  1. Ruby 2.4: Ruby with DevKit

The last step when install Ruby 2.4 with RubyInstaller is install MSYS2 and MINGW toolchain. Choose option 3
Choose option 3 here

  1. MySQL C-Connector Library: Choose the correct version of your windows here

Download the zip version then extract it to some directory that have no space character in name (and it should be short too). For example, extract it to C:\Ruby\MySQL
Download the zip archive

  1. [Optional] Visual Studio Code: for easy command typing and a good “IDE” to develop with Ruby

If you decided to go with the VSCode, then you need to reconfig so PowerShell can run ruby scripts

2. Install mysql2 gem

Say you are developing redmine (latest version when writing this tutorial is 3.4.5)

They are using Rails version 4.2.8 combine with mysql2 0.4.6 and above in gemfile.

The first time you run bundle update, bundler fetch the gem mysql2 0.4.10 then install.

If you are using Ruby 2.3, then it’s will install smoothly

If you are using Ruby 2.4, the installation process are likely to failed if you are running on a Windows system

2.1. Cause

mysql2 gem version 0.4.x does not have precompiled lib for rails 4 with Ruby 2.4

2.2. Resolution

To compile the lib yourself

  1. Open Visual Studio Code terminal and (optional) cd to your redmine source code folder
  2. type in the following code
gem install mysql2:0.4.10 --platform=ruby -- '--with-mysql-lib="C:\Ruby\MySQL\mysql-connector-c-6.1.11-win32\lib" --with-mysql-include="C:\Ruby\MySQL\mysql-connector-c-6.1.11-win32\include"'

Important
* Do not use folder with space character
* The platform architecture may vary (x32/x64)
* If you are using a Ruby version manager like uru, check the current version of ruby first to make sure using ruby 2.4
* If build native code failed, run ridk install and choose option 3

  1. Copy compiled lib to your application vendor cached folder

Navigate to your gems installed folder, copy the mysql2.so file to your vendor cache under folder 2.4

The folder is usually at

your-installed-ruby-folder\lib\ruby\gems\2.4.0\gems\mysql2-0.4.10\lib\mysql2

Copy it to folder

your-redmine-project-folder\vendor\cache\ruby\2.4.0\gems\mysql2-0.4.10-x86-mingw32\lib\mysql2
  1. Run the code and profit
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.