Web Development

How To Install Ruby on Rails MYSQL2 gem on Windows 10

If you cannot install the MYSQL2 gem in Windows, you need to download the C-Header files from MYSQL. Unlike other gems, MYSQL2 requires direct linking into some of the native build files provided by the library. Unless you have MYSQL installed already, it means you need to download – and reference – this library from the Internet.

how to install ruby on rails mysql2 gem on windows 10
How To Install Ruby on Rails MYSQL2 gem on Windows 10

MYSQL2 gem

The problem is that since the MYSQL2 gem requires certain files to help it connect to the Internet, the gem uses these to at build. The error you’ll receive will mention “Failed to build native extensions”. This means your Windows system did not have the header files required for the gem to be installed correctly.

The way to resolve this is to first log onto the MYSQL website and look for “C-Connector”. This is basically the “driver” for MYSQL, allowing Windows to call a number of pre-compiled functions to help it communicate with local or external MYSQL installations. The purpose of this download is to ensure your computer has the necessary files to install the gem.

Once you have found the MYSQL C-Connector file, you need to download it to your hard drive. From this, unzip the file and place its contents on your hard drive. You must put it on a path which has no spaces, otherwise, the installation reference will break. A path such as C:/downloads/MySQL-c-connector/files will work fine. Please note you do NOT need to install this – only download the zipped archive, we can remove it after the gem has been installed.

If you have downloaded and placed the files on your hard drive, you need to then install the gem again. To do this, you should load up CMD and type the following command “gem install mysql2 –platform=ruby — ‘–with-myself-dir=”C: MySQL-connector-path”‘”.

This command will reference the newly installed MYSQL C-Connector library files which Ruby requires to build the gem. If you’ve downloaded/installed it correctly, this should allow Ruby to install the required files etc.

If successful, the cmd will explain the gem has been installed; if not, it will likely be that you have either downloaded the incorrect files OR you have not got another necessary library installed on your system.

Since Windows comes with no external libraries, it’s necessary to install any required from the Internet. This is one of the reasons most people don’t like developing for Windows – ultimately, if you’re able to download the correct header files, you should be fine.

What is MySQL2?

Mysql2 is a contemporary, straightforward, and lightning-fast Mysql library for Ruby – it is a wrapper over libmysql.

The Mysql2 gem is intended to facilitate the highly frequent task of connecting, querying, and iterating over results. Certain database libraries exist that are straightforward 1:1 translations of the already sophisticated C APIs. This is not the case.

Additionally, it requires the usage of UTF-8 [or binary] for the connection [and all strings in 1.9; if Encoding.default internal is enabled, it converts from UTF-8 to that encoding] and makes encoding-aware MySQL API calls when possible.

MySql2 Documentation

The API is divided into two classes:

Mysql2::Client – your database connection.

Mysql2::Result – returned when a #query on the connection is sent. It contains Enumerable.

How to download MYSQL C-Connector Library?

If you are NOT using “MSYS2” with Ruby (RubyInstaller2), you will need to get C-Connector manually from MySQL…

There are now two methods for obtaining this library (MySQL made the modification)

  • Option 1) Download the ZIP version of the C-Connector library using the “archive” feature offered by MySQL.
  • Option 2) Download the MySQL 8.0 “installer” (which includes the EXACT SAME library as the ZIP archive).

EITHER of the versions install the SAME library on your machine.

You need C-Connector (NOT C++) (version 6.1.11)…

Even with the most recent installation, the C-Connector suite is still at version 6.1.11. (literally NO difference between the two). Thus, it is totally dependent on whether you intend to handle the item through ZIP archive or installer.

We STRONGLY advocate downloading the previous “zip” version (which has no bloatware), while you may use the installer if you like.

Show More

Raj Maurya

Raj Maurya is the founder of Digital Gyan. He is a technical content writer on Fiverr and freelancer.com. He loves writing. When not working he plays Valorant.

Leave a Reply

Back to top button