Ruby MySQL2 Gem Installation on Windows 7

Ruby MYSQL2 gem installation on windows 7

EDIT 30/09/2014

When this answer was posted the 64 bit rails installer wasn't the recommended version - it now seems people are starting to use it more. It should be noted when you download the MySQL Connector you need to download either 64 or 32bit to correspond to the version of rails you installed.

Amazingly I lucked upon an answer very early this morning as I happened to be looking for something else of a similar nature. I'm not quite sure why there isn't a single simple guide for this as it appears to be very straight forward!

For some reason just specifying the mysql-dir when you install the gem doesn't pick up with other sub directories so you need to set the parameters manually.

For anyone else experiencing the same problem, I did the following:

1) Download the MySql C Connector from: http://dev.mysql.com/downloads/connector/c/

NOTE Don't download the installer, download the ARCHIVE for your OS

Download either the 32bit or 64 bit ARCHIVE to correspond with the version of rails you installed.

2) Extract the file to C:\mysql-connector

3) Then ran:

 gem install mysql2 --platform=ruby -- '--with-mysql-lib="C:\mysql-connector\lib" --with-mysql-include="C:\mysql-connector\include" --with-mysql-dir="C:\mysql-connector"'

Voila everything is working fine.

EDIT 30/01/2014

I just had to do a fresh install on a bricked machine and the command in step 3 didn't work, what did work was:

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

I'm not quite sure what the difference is but this time it seems to be picking up the directories ok, so if the first one doesn't work try this one!

I think this has to do with how you go about installing rails, this time round I used the railsinstaller which seems to set the paths up correctly.

A lot of the outcome here seems to depend on the shell your using, a lot of people are having problems with powershell so I wouldn't advise using it. I did this in an elevated command prompt.

Oh and lastly if you get an error regarding the mysql2 gem when you do RAILS S you need to copy the libmysql.dll from the LIB directory of the mysql connector to the bin directory where rails has been installed.

Unable to install MySQL2 gem on Windows 7

The specific version of mysql2 gem you're trying to install (0.2.4) not only lacks binaries for Windows, but have issues on Windows.

Please install mysql2 gem without indicating the version:

gem install mysql2

Which will install latest version (0.2.6 at the time of my posting this) and also provides binaries for Windows which skip the compilation step.

If you still want to force the compilation (because your version of MySQL differs from the one used to generate the binary gem, you will need to install RubyInstaller's DevKit from RubyInstaller website:

http://rubyinstaller.org/downloads

And follow the DevKit installation instructions from our wiki (that is linked from the download page)

You will need to provide the path to both headers and libraries during the gem installation process, and adjust the MySQL installation location from the following instructions:

subst X: "C:\Program Files (x86)\MySQL\MySQL Server 5.1" 
gem install mysql2 --platform=ruby -- --with-mysql-dir=X: --with-mysql-lib=X:\lib\opt
subst X: /D

The above command uses subst to avoid issues with path with spaces, which you should avoid always.

Hope this helps.

installing ruby with mysql2 on windows 7 64 bit returns error on rails s

Basically following the instructions in here
I just completed an installation from zero to fully functional on Windows 7 64bit.

In your case the problem may lie in the version of the gem mysql2 or in the manner in which it was installed; you could start with uninstall and re-install the 0.2.6 version @see Gediminas answer and check the GemFile if you have a different version.
This save me from troubles with specific:

--with-mysql-dir=.... --with-mysql-lib=.... --with-mysql-include=....


Anyway I will detail each steps of my checklist so you can see if there are differences to your installation

  • Installed mysql-installer-community-5.6.24.0.msi from http://dev.mysql.com/downloads/mysql/
    In my personal checklist
    these are my selected component:
    MySQL-Install-OptionSelected
    .. my choice for connectivity options:
    enter image description here
    .. and account/security option:
    enter image description here

  • Installed Ruby rubyinstaller-2.1.6.exefrom http://rubyinstaller.org/downloads/

Notice: according to http://rubyinstaller.org/downloads/ I select the 2.1.* ( not the last one) and I downloaded the standard 32-bit version because the 64-bit versions of Ruby are relatively new on the Windows area and not all the packages have been updated to be compatible with it. To use this version you will require some knowledge about compilers and solving dependency issues, which might be too complicated if you just want to play with the language. Also reading on the same page
: Ruby 2.0.0 and above -->: mingw64-32-4.7.2

  • Installed Ruby Devkit DevKit-mingw64-32-4.7.2-20130224-1151-sfx.exe from http://rubyinstaller.org/downloads/ (extracted to C:\DevKit)
  • Don't forget to setup the DevKit
cd C:\DevKit 
ruby dk.rb init
ruby dk.rb install
devkitvars.bat
  • Don't forget to copy libmysql.dll in my case, after installing mysql community 5.6 i found three different dll ( in dimension) with the same name:
cd \
find . -name "libmysql.dll"
./Program Files/MySQL/MySQL Connector.C 6.1/lib/libmysql.dll
./Program Files/MySQL/MySQL Server 5.6/lib/libmysql.dll
./Program Files/MySQL/MySQL Workbench 6.2 CE/libmysql.dll``

Definitively i choose this option:

C:>cd C:\Ruby21\bin>
C:\Ruby21\bin> copy "\Program Files\MySQL\MySQL Server 5.6\lib\libmysql.dll" .
1 file(s) copied.
  • I had some issue installing gems therefore i decided to update rubigems, this helps me a lot.
C:\Ruby21\bin>gem update --system
...
[snip]
...
C:\Ruby21\bin>gem env
RubyGems Environment:
- RUBYGEMS VERSION: 2.4.6
- RUBY VERSION: 2.1.6 (2015-04-13 patchlevel 336) [i386-mingw32]
- INSTALLATION DIRECTORY: C:/Ruby21/lib/ruby/gems/2.1.0
- RUBY EXECUTABLE: C:/Ruby21/bin/ruby.exe
- EXECUTABLE DIRECTORY: C:/Ruby21/bin
- SPEC CACHE DIRECTORY: C:/Users/rondadev/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: C:/ProgramData
- RUBYGEMS PLATFORMS:
- ruby
- x86-mingw32
- GEM PATHS:
- C:/Ruby21/lib/ruby/gems/2.1.0
- C:/Users/rondadev/.gem/ruby/2.1.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- C:\DevKit\bin
- C:\DevKit\mingw\bin
- C:\Ruby21\bin
- C:\Windows\system32
- C:\Windows
- C:\Windows\System32\Wbem
- C:\Windows\System32\WindowsPowerShell\v1.0\
- C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\
- C:\Program Files\Microsoft SQL Server\110\Tools\Binn\
- C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\
- C:\Program Files (x86)\MySQL\MySQL Fabric 1.5.4

ready to install rails and create the first application:

gem install rails

gem install mysql2 -v 0.2.6

rails new test_app -d mysql

At the end the last steps:

  1. connected MySQL Workbench and created a new schema named
    test_app_development (to create a new schema you can press the 4th icon in the tool bar with a cylinder and a plus sign ..see next screenshot)

  2. fixed username and password in config/database.yml as in sansarp
    anwser
    enter image description here

  3. Migrated the database

MySQL Workbench and database migration
after migrate your schema should look like this:
enter image description here


  1. executed rails -s

  2. successfully opened in the browser http://localhost:3000

```

C:\Projects\test_app>rake db:migrate --trace
DL is deprecated, please use Fiddle
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:schema:dump (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:schema:dump

C:\Projects\test_app>rails s
DL is deprecated, please use Fiddle
=> Booting WEBrick
=> Rails 4.2.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-04-29 00:20:21] INFO WEBrick 1.3.1
[2015-04-29 00:20:21] INFO ruby 2.1.6 (2015-04-13) [i386-mingw32]
[2015-04-29 00:20:21] INFO WEBrick::HTTPServer#start: pid=4144 port=3000


Started GET "/" for ::1 at 2015-04-29 00:20:34 +0200
Processing by Rails::WelcomeController#index as HTML
Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/railties-4.2.1/lib/rails/templates
/rails/welcome/index.html.erb (0.0ms)
Completed 200 OK in 31ms (Views: 15.6ms | ActiveRecord: 0.0ms)

mysql ruby gem not installing on Windows 7

The 'mysql' and 'mysql2' packages/extensions/gems don't work with the
x64 bit version of Ruby on Windows 7 (or any 64 bit version of
Windows). You need to install the x86 (32) bit version of Ruby for
them to work.

So I repeated step 3 in my question, and installed version 1.9 instead taking the advice from my comment in my question (on issue 191) by luislavena on August 30, 2013. This is the x86 (32 bit) version. Got it from here.

http://rubyinstaller.org/downloads/

Looks like the file's name is "rubyinstaller-1.9.3-p551.exe".

Then completed step 4. The Ruby developers must have fixed their scripts to modify the config.yml file automatically for the x86 version. I didn't have to edit this time.

Then for step 5, I installed this devkit exe, because the newer version of Ruby didn't have a dev kit that was x86.

For step 6, I placed into it's own folder. It still extracts files in the exe root, not a folder of the exe root. Then ran all the commands except for the last one. It still threw the error.

"DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe"

Then for step 7, I ran this.

gem install mysql

It installed the x86 version of mysql gem, because I can see the gem, by running this. It's very shiny and looks expensive!

gem list

5/26/2015 update:

Latest version of Ruby when installed on Windows that allows you to
start the Rails server is 2.1.6.

Starting the rails server didn't work by running "rails s". I had to go back and uninstall old versions of Ruby. See my answer here. Ignore the other answers if you are on Windows.

Remove older versions of Ruby (on Windows)

The only stable version of Ruby on Windows that allows you to start the rails server is "rubyinstaller-2.1.6.exe". "rubyinstaller-2.2.2.exe" throws the following error:

C:/row/Ruby22/lib/ruby/gems/2.2.0/gems/nokogiri-1.6.6.2-x86-mingw32/lib/nokogiri
.rb:29:in `require': cannot load such file -- nokogiri/nokogiri (LoadError)
from C:/row/Ruby22/lib/ruby/gems/2.2.0/gems/nokogiri-1.6.6.2-x86-mingw32
/lib/nokogiri.rb:29:in `rescue in <top (required)>'
...
... continued

Ruby mysql2 gem, cannot build on Windows 7

To install and compile extensions sometimes you need libraries and headers of the dependencies. In this case, you need MySQL headers and libraries to properly compile.

Assuming you downloaded Ruby from RubyInstaller and also installed the Development Kit and followed it's installation instructions, to successfuly install mysql2 gem I recommend you read this answer in our Troubleshooting page:

  1. Download MySQL Connector/C
  2. Extract into a path without spaces
  3. When installing mysql2 gem, provide the additional options (gem install mysql2 -v 0.2.10 -- --with-mysql-include=... --with-mysql-lib...)

Provide for --with-mysql-include option the path to the include directory of MySQL Connector/C. The same for --with-mysql-lib but this time point to lib directory.

Don't forget to copy libmysql.dll to your Ruby bin directory.

Hope this helps.



Related Topics



Leave a reply



Submit