⌚️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”:
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 ↴
1️⃣ Download the MYSQL C-Connector Library
There are now two ways to obtain this library (MySQL changed it)…
- Use the “archive” provided by MySQL to download the ZIP version of the C-Connector library.
- 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
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 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)…
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
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)…
⚠️ 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
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…
In this case, there are several work-arounds:
gem install mysql2:0.4.10 --platform=ruby -- --with-mysql-dir="c:/mysql-connector-path"
You may also wish to use the “precompiled” binaries (
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
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.
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
brew→ EXTREMELY 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…
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:
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
2️⃣ Install Gem With System Libs
With MSYS2, you can use the “system libraries” to install various packages…
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
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!
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)…
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…