⌚️Installing The MYSQL2 Gem ⌚️#RubyOnRails On Windows 7/8/10

Step-by-Step guide on installing the MYSQL2 gem on Windows 7/8/10…

Installing MYSQL2 Ruby on Rails gem on a Windows environment can be difficult, typically showing the infamous “Failed To Build Native Extension”:

Failed to build gem native extension error

If you’re seeing this error, the good news is it’s actually quite simple to resolve.

It’s caused because you don’t have the MYSQL C-connector library installed. This allows your system to communicate with MYSQL, providing the required hooks & API calls to the MYSQL2 gem…

📜 Overview 📜

The solution depends on how you installed Ruby on Windows

💻 C-Connector 💻

If you used another method (such as Devkit), you’ll need to obtain the MySQL C-Connector library from MySQL…

🖥️ MSYS2 🖥️

If you used MSYS2 (via RubyInstaller), you can use the
use-system-libraries switch (if you have the correct package)…


Both methods covered below…

💻 C-Connector 💻

If you are *NOT* using “MSYS2” with Ruby (RubyInstaller2),
you’ll need to manually download C-Connector from MySQL

1️⃣ Download the MYSQL C-Connector Library

There are now two ways to obtain this library (MySQL changed it)…

  1. Use the “archive” provided by MySQL to download the ZIP version of the C-Connector library.
  2. Download the “installer” for MySQL 8.0 (which includes the EXACT SAME library as the ZIP archive).

*BOTH* versions put the SAME library on your system
you need C-Connector (*NOT* C++) (which is at 6.1.11)…

MySQL want you to use an installer — but it’s basically the same as the ZIP, so just use the ZIP :)

Even if you use the latest installer, the C-Connector suite is sill at 6.1.11 (literally NO difference between the two). Thus, it depends entirely on whether you wish to use a ZIP archive or installer to manage the package.

We STRONGLY recommend using the older “zip” version (no bloatware) → although you can obviously use the installer if you wish…

📦 Archived (ZIP) version (recommended)

As mentioned, this is the cleanest way to get the library onto your system.

Not only does it do away with any of the bloatware typically added by installers, but it also gives you the ability to keep all your libs / dependencies in the same folder…

Download the 32-bit or 64-bit ZIP version of the MYSQL C-Connector library…

Download the zip version according to your version of Windows.

We’re running Windows x64, so we get the 64-bit archive. However, if you’re running x86 (32-bit), then you will need to download that version.

If in doubt, just download the 32-bit edition

♻️ Installer version (NOT recommended)

If you’re looking at the standard C-Connector recommendations by MySQL (they’ve changed the process slightly), you’ll be prompted to download an installer for the entire MySQL package (below)…

Only download the “web” installer — other one isn’t worth the time.

If you want to use this method to obtain the C-Connector client, you have to download the above file onto your hard drive.

This puts an installer application on the drive, which you’re able to load up to install the ENTIRE MySQL package:

After clicking onto this, it brings you into the installation system — from which you’re able to select the packages you wish to install. You ONLY need the
C-Connector library:

Hopefully this should show you that you don’t actually *need* the installer.

There are no benefits over using the installer to the naked ZIP file.

2️⃣ Unzip / Install

Next, we need to put the files onto your system…

  • If you used the ZIP, simply copy the files onto your hard drive, and then move them into a permanent folder…
  • If you used the installer, you need to select a permanent installation directory into which you’re able to place the files as required (this is done automatically by the installer)…
Pick the above options for the installer — it will install into your “Program Files” directory…

⚠️ REMEMBER ⚠️ → the directory should have NO SPACES in its address…

3️⃣ Install Gem With Arguments

Once installed, you need to try and install the gem again with arguments pointing to the correct mysql-dir path. This allows Ruby to identify the include and lib paths, which is what many other tutorials reference:

gem install mysql2 --platform=ruby -- --with-mysql-dir="c:/mysql-connector-path"

Now, there’s actually a problem which prevents 0.5.x versions of the gem working on Ruby 2.4/2.5 on Windows (with C-Connector).

Frankly, I don’t know (and am not overly concerned about) what the cause of the issue is. I believe it’s a problem with the way in which Ruby 2.4/2.5 is able to manage binary compilation, but I’ve not looked into it enough to be sure…

Apparently, you can’t compile mysql2 on Windows using Ruby 2.4/2.5

In this case, there are several work-arounds:

gem install mysql2:0.4.10 --platform=ruby -- --with-mysql-dir="c:/mysql-connector-path"
Using the 0.4.10 version of the gem seems to work fine

You may also wish to use the “precompiled” binaries (libmysql.dll):

gem install mysql2 -- --with-mysql-dir="c:/mysql-connector-path"

These will allow you to install & run the gem with the various versions of C-Connector available from MySQL.

The process should not be as complicated as this — but, as with most things, Oracle’s involvement with MySQL seems to have changed how its distribution is managed.

4️⃣ Place libmysql.dll Into Ruby “bin” Dir

Place libmysql.dll from your MYSQL-Connector files into your Ruby/bin dir

You’ll find libmysql.dll in your mysql_files/bin folder

You should copy this file & place into your ruby/bin directory (allows Ruby to use MYSQL hooks & functions)

This will complete the installation.

🖥️ MSYS2

If you installed Ruby/Rails using the new RubyInstaller2, you’ll be using MSYS2 to manage the packages for your system…

This works very similarly to yum/apt/brewEXTREMELY effective in giving you access to many of the dependencies missing from Windows.

There’s actually a dependency for MySQL (libmariadbclient) which was created with MSYS2 → allows you to install the gem without further dependencies…

Doing this is EXTREMELY simple:

1️⃣ Install the ‘mingw-w64-x86_64-libmariadbclient’ package

If you installed the libmariadbclient package, you have the equivalent of the MySQL C-Connector library on your system…

First search for the package…

The first step is to search for the package:

ridk exec pacman -Ss mariadb

This will show which versions are available. You want one of them to be installed (as pictured above). If neither are, it means you have to add it to your system:

We already have it installed, so no reason to install/update…
ridk exec pacman -S mingw-w64-i696-libariadbclient #-> 32-bit
ridk exec pacman -S mingw-w64-x86_64-libmariadbclient #-> 64-bit

Pick the version for your system, type the above into your CMD and hit Enter.

You can see more information about MSYS2 package installation here.

This will install the “MariaDB” package onto your MSYS2 installation — from here, you can use it to install the mysql2 gem…

2️⃣ Install Gem With System Libs

With MSYS2, you can use the “system libraries” to install various packages…

This is generally recommended by the maintainers of RubyInstaller2

If you have the correct packages installed, you’ll be able to use them in the gem installation / compilation process. This is how we can install mysql2:

ridk exec gem install --platform=ruby -- --use-system-libraries

After this, the gem should install (as shown above).

☎️ Further Support ☎️

If you need further support, please feel free to contact us → we’re in the UK so please consider the ⌚ difference!


We have a 👾PCFixes.com👾 live chat support channel at https://www.pcfixes.com/ → available 24/7

👾PCFixes.com👾 Is Currently Voted The ⭐️Best Live Tech Support⭐️Service

Live support is recommend IF you use your system for business or work.

If you need the help right now, getting an expert on screen gives you the ability to at least get a second opinion (and perhaps someone to help guide you through the fix). Live support is the only way to do this.

⚠️ Do NOT use live services that charge up front. ONLY use companies who provide live support without ANY up-front commitments…⚠️

✔️ PCFixes.com is the only recognized online system repair service
✔️ PCFixes.com is operated from the UK by veteran PC repair technicians
✔️ PCFixes.com gives 24/7 support to anyone needing system repairs

📴 PCFixes.com also resolves issues with Android & iPhone

⭐️⭐⭐️⭐️⭐️

PCFixes.com is FREE to talk to anyone and you’re welcome to stay on the line for as long as you require to get things fixed.

The real benefit of PCFixes.com lies in its contributor network → if you cannot get your system fixed directly, you can use their local network (call an out an expert to come and solve the issue for you)…

👾 PCFixes.com 👾 provides 24/7 LIVE support which gives you the ability to connect with real people to get specific solutions for your system.

If you need any help identifying or solving the problem you’re experiencing, it’s worth using the service to gain a second opinion. If you want to keep them on the line whilst you try and fix it, you’re very welcome to do that…

📥 Thanks For Reading! 📥

If you need further help, please feel free to ask below…

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store