2

My OS is Red Hat Enterprise Linux Server 7.4 (Maipo), CPU architecture is x86_64 and byte order is Little Endian.

I was doing bundle install for my rails application, but I get

An error occurred while installing mysql2 (0.4.10), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.4.10' --source 'https://rubygems.org/'` succeeds before bundling.

and doing gem install mysql2 -v '0.4.10' --source 'https://rubygems.org/' gives me:

Building native extensions. This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    current directory: /home/my-user-name/.rvm/gems/ruby-2.4.4/gems/mysql2-0.4.10/ext/mysql2
/home/my-user-name/.rvm/rubies/ruby-2.4.4/bin/ruby -r ./siteconf20180704-8600-mkihlj.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for rb_big_cmp()... yes
-----
Using mysql_config at /usr/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... no
checking for MYSQL_OPT_SSL_ENFORCE in mysql.h... yes
checking for MYSQL.net.vio in mysql.h... no
checking for MYSQL.net.pvio in mysql.h... yes
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
-----
Setting libpath to /usr/lib64/
-----
creating Makefile

current directory: /home/my-user-name/.rvm/gems/ruby-2.4.4/gems/mysql2-0.4.10/ext/mysql2
make "DESTDIR=" clean

current directory: /home/my-user-name/.rvm/gems/ruby-2.4.4/gems/mysql2-0.4.10/ext/mysql2
make "DESTDIR="
compiling client.c
In file included from ./mysql2_ext.h:35:0,
                 from client.c:1:
client.c: In function ‘rb_set_ssl_mode_option’:
./client.h:56:3: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
   mysql_client_wrapper *wrapper; \
   ^
client.c:118:3: note: in expansion of macro ‘GET_CLIENT’
   GET_CLIENT(self);
   ^
client.c:119:3: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
   int val = NUM2INT( setting );
   ^
client.c:145:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
client.c: At top level:
cc1: warning: unrecognized command line option "-Wno-self-assign" [enabled by default]
cc1: warning: unrecognized command line option "-Wno-constant-logical-operand" [enabled by default]
cc1: warning: unrecognized command line option "-Wno-parentheses-equality" [enabled by default]
cc1: warning: unrecognized command line option "-Wno-tautological-compare" [enabled by default]
compiling infile.c
compiling mysql2_ext.c
compiling result.c
result.c: In function ‘rb_mysql_result_alloc_result_buffers’:
result.c:326:40: warning: assignment from incompatible pointer type [enabled by default]
     wrapper->result_buffers[i].is_null = &wrapper->is_null[i];
                                        ^
result.c:328:40: warning: assignment from incompatible pointer type [enabled by default]
     wrapper->result_buffers[i].error   = &wrapper->error[i];
                                        ^
result.c: At top level:
cc1: warning: unrecognized command line option "-Wno-self-assign" [enabled by default]
cc1: warning: unrecognized command line option "-Wno-constant-logical-operand" [enabled by default]
cc1: warning: unrecognized command line option "-Wno-parentheses-equality" [enabled by default]
cc1: warning: unrecognized command line option "-Wno-tautological-compare" [enabled by default]
compiling statement.c
statement.c: In function ‘rb_raise_mysql2_stmt_error’:
statement.c:50:3: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
   VALUE rb_error_msg = rb_str_new2(mysql_stmt_error(stmt_wrapper->stmt));
   ^
statement.c:57:3: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
   rb_encoding *default_internal_enc = rb_default_internal_encoding();
   ^
In file included from ./mysql2_ext.h:35:0,
                 from statement.c:1:
statement.c: In function ‘execute’:
./client.h:56:3: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
   mysql_client_wrapper *wrapper; \
   ^
statement.c:273:3: note: in expansion of macro ‘GET_CLIENT’
   GET_CLIENT(stmt_wrapper->client);
   ^
statement.c:405:13: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
             VALUE rb_val_as_string = rb_funcall(argv[i], intern_to_s, 0);
             ^
In file included from ./mysql2_ext.h:35:0,
                 from statement.c:1:
statement.c: In function ‘fields’:
./client.h:56:3: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
   mysql_client_wrapper *wrapper; \
   ^
statement.c:481:3: note: in expansion of macro ‘GET_CLIENT’
   GET_CLIENT(stmt_wrapper->client);
   ^
statement.c: At top level:
cc1: warning: unrecognized command line option "-Wno-self-assign" [enabled by default]
cc1: warning: unrecognized command line option "-Wno-constant-logical-operand" [enabled by default]
cc1: warning: unrecognized command line option "-Wno-parentheses-equality" [enabled by default]
cc1: warning: unrecognized command line option "-Wno-tautological-compare" [enabled by default]
linking shared-object mysql2/mysql2.so
/usr/bin/ld: cannot find -lmariadb
collect2: error: ld returned 1 exit status
make: *** [mysql2.so] Error 1

make failed, exit code 2

Gem files will remain installed in /home/my-user-name/.rvm/gems/ruby-2.4.4/gems/mysql2-0.4.10 for inspection.
Results logged to /home/my-user-name/.rvm/gems/ruby-2.4.4/extensions/x86_64-linux/2.4.0/mysql2-0.4.10/gem_make.out

I read this post,

but doing sudo yum install mysql-devel gives:

Loaded plugins: search-disabled-repos

Package MariaDB-devel-10.3.8-1.el7.centos.x86_64 already installed and latest version
Nothing to do

even though running sudo yum list installed mysql-devel gives:

Loaded plugins: search-disabled-repos
Error: No matching Packages to list

This is strange.

In before, I installed MariaDB by doing these 2 steps:

Step 1:

sudo vi /etc/yum.repos.d/MariaDB.repo

The content of it is:

# MariaDB 5.5 RedHat repository list - created 2018-07-04 03:54 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/rhel7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

Step 2:

sudo yum install MariaDB-server MariaDB-client

And I did start it: sudo service mysql start

How can I install mysql2 gem successfully?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Henry Yang
  • 2,283
  • 3
  • 21
  • 38
  • you are missing libraries required for mysql gem. install them first and then do bundle – Bijendra Jul 04 '18 at 04:14
  • @Bijendra How do I know which library is missing and how to install them? i thought gem install will handle that automatically for me, and for the part it can't get it will tell me. But apparently I don't know where to look for it or such feature does nto exist – Henry Yang Jul 04 '18 at 04:16
  • possible duplicate of https://stackoverflow.com/questions/45474191/an-error-occurred-while-installing-mysql2-0-4-8-and-bundler-cannot-continue#answer-45474878 – Emu Jul 04 '18 at 04:19
  • Hello @Emu, in my post I have mentioned that the post you just mentioned doesn't work – Henry Yang Jul 04 '18 at 04:23
  • Libraries are not auto installed with gem, they are installed at system level while gems can be installed be at project level ex. gemset – Bijendra Jul 04 '18 at 04:34
  • Hi @Bijendra Do you know how one can check which library is missing and how to install them? For example, in this case. – Henry Yang Jul 04 '18 at 04:50
  • Follow this: https://www.digitalocean.com/community/tutorials/how-to-use-mysql-with-your-ruby-on-rails-application-on-centos-7 . centos/RHE are similar in command usage – Bijendra Jul 04 '18 at 05:47
  • `yum install MariaDB-shared` solves my problem – Henry Yang Jul 04 '18 at 05:57

3 Answers3

2

First run

sudo apt-get install libmysqlclient-dev

Or

yum install mysql-devel

then

bundle install
Anand
  • 6,457
  • 3
  • 12
  • 26
  • For `yum install mysql-devel`, as I have mentioned in my original post, it doesn't work – Henry Yang Jul 04 '18 at 04:51
  • @HenryYang probably it `brew install libmysqlclient-dev` for mac os. lets try it. – Anand Jul 04 '18 at 04:54
  • My OS is Red Hat Enterprise Linux Server 7.4 (Maipo), and I used yum to install mariadb, I don't know if brew is available in my OS but I assume it's a good idea to stick to one package manager? – Henry Yang Jul 04 '18 at 05:04
  • Brew is available only for OSX. – Bijendra Jul 04 '18 at 06:59
  • @Aman Error: No available formula or cask with the name "libmysqlclient-dev" – Hakanai Apr 21 '21 at 04:32
  • As of 2022 and Debian 10, I had to install `libmariadb-dev-compat` instead of `libmysqlclient-dev`, since the above command returned: `Package libmysqlclient-dev is not available […]. However the following packages replace it: libmariadb-dev-compat libmariadb-dev` – David M. Feb 23 '22 at 13:11
0

I just had the same problem and this answer on a mysql2 issue worked for me. Installing mysql-devel did not work for me.

Their answer and what worked for me … yum install MariaDB-shared.

Addendum: I was installing mysql2 v0.5.2 along side MariaDB 10.3.9.

matte
  • 1
0

To intall gem mysql2 in a rail project on a mac you need mariadb-connector-c to compile it.

A bit off topic but for mac users with homebrew if libmysqlclient-dev doesn't work you can do:

    brew unlink mariadb    
    brew install mariadb-connector-c    

    # cd into the rails project directory    

    bundle install (or gem install mysql2)    
    brew uninstall mariadb-connector-c    
    brew link mariadb
timbillstrom
  • 1,176
  • 16
  • 33