Error: failed to build native extension

I am trying to install the mysql2 gem v0.3.20 as required for the project I’m working on. I’m running rails 3.2 and ruby 2.3.5 on ubuntu 20.04 installing the latest v0.3.5 works fine, though.

Thanks in advance!

the complete error message:

ERROR:  Error installing mysql2:
        ERROR: Failed to build gem native extension.

    current directory: /home/t480s/.rbenv/versions/2.3.5/lib/ruby/gems/2.3.0/gems/mysql2-0.3.20/ext/mysql2
/home/t480s/.rbenv/versions/2.3.5/bin/ruby -r ./siteconf20210218-24269-108odh1.rb extconf.rb
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
-----
Using mysql_config at /usr/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Setting libpath to /usr/lib/x86_64-linux-gnu
-----
creating Makefile

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /home/t480s/.rbenv/versions/2.3.5/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0-static/mysql2-0.3.20/mkmf.log

current directory: /home/t480s/.rbenv/versions/2.3.5/lib/ruby/gems/2.3.0/gems/mysql2-0.3.20/ext/mysql2
make "DESTDIR=" clean

current directory: /home/t480s/.rbenv/versions/2.3.5/lib/ruby/gems/2.3.0/gems/mysql2-0.3.20/ext/mysql2
make "DESTDIR="
compiling infile.c
compiling result.c
result.c: In function ‘msec_char_to_uint’:
result.c:189:17: warning: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Wsign-compare]
  189 |   for (i = 0; i < (len - 1); i++) {
      |                 ^
result.c: In function ‘rb_mysql_result_fetch_row’:
result.c:259:14: warning: this statement may fall through [-Wimplicit-fallthrough=]
  259 |           if (castBool && fields[i].length == 1) {
      |              ^
result.c:263:9: note: here
  263 |         case MYSQL_TYPE_SHORT:      /* SMALLINT field */
      |         ^~~~
compiling client.c
client.c: In function ‘nogvl_read_query_result’:
client.c:439:3: error: unknown type name ‘my_bool’; did you mean ‘bool’?
  439 |   my_bool res = mysql_read_query_result(client);
      |   ^~~~~~~
      |   bool
client.c: In function ‘_mysql_client_options’:
client.c:775:3: error: unknown type name ‘my_bool’; did you mean ‘bool’?
  775 |   my_bool boolval;
      |   ^~~~~~~
      |   bool
client.c:810:10: error: ‘MYSQL_SECURE_AUTH’ undeclared (first use in this function); did you mean ‘MYSQL_DEFAULT_AUTH’?
  810 |     case MYSQL_SECURE_AUTH:
      |          ^~~~~~~~~~~~~~~~~
      |          MYSQL_DEFAULT_AUTH
client.c:810:10: note: each undeclared identifier is reported only once for each function it appears in
client.c: In function ‘set_secure_auth’:
client.c:1198:38: error: ‘MYSQL_SECURE_AUTH’ undeclared (first use in this function); did you mean ‘MYSQL_DEFAULT_AUTH’?
 1198 |   return _mysql_client_options(self, MYSQL_SECURE_AUTH, value);
      |                                      ^~~~~~~~~~~~~~~~~
      |                                      MYSQL_DEFAULT_AUTH
client.c:1199:1: warning: control reaches end of non-void function [-Wreturn-type]
 1199 | }
      | ^
make: *** [Makefile:239: client.o] Error 1

make failed, exit code 2

and the error file:

have_header: checking for ruby/thread.h... -------------------- yes

"gcc -o conftest -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/x86_64-linux -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/ruby/backward -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0 -I. -I/home/t480s/.rbenv/versions/2.3.5/include    -I/usr/local/opt/openssl/include conftest.c  -L. -L/home/t480s/.rbenv/versions/2.3.5/lib -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/usr/local/opt/openssl/lib     -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/home/t480s/.rbenv/versions/2.3.5/lib -lruby-static  -lpthread -ldl -lcrypt -lm   -lc"
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

"gcc -E -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/x86_64-linux -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/ruby/backward -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0 -I. -I/home/t480s/.rbenv/versions/2.3.5/include    -I/usr/local/opt/openssl/include  conftest.c -o conftest.i"
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: #include <ruby/thread.h>
/* end */

--------------------

have_func: checking for rb_thread_call_without_gvl() in ruby/thread.h... -------------------- yes

"gcc -o conftest -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/x86_64-linux -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/ruby/backward -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0 -I. -I/home/t480s/.rbenv/versions/2.3.5/include    -I/usr/local/opt/openssl/include conftest.c  -L. -L/home/t480s/.rbenv/versions/2.3.5/lib -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/usr/local/opt/openssl/lib     -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/home/t480s/.rbenv/versions/2.3.5/lib -lruby-static  -lpthread -ldl -lcrypt -lm   -lc"
checked program was:
/* begin */
 1: #include "ruby.h"
 2: 
 3: #include <ruby/thread.h>
 4: 
 5: /*top*/
 6: extern int t(void);
 7: int main(int argc, char **argv)
 8: {
 9:   if (argc > 1000000) {
10:     printf("%p", &t);
11:   }
12: 
13:   return 0;
14: }
15: int t(void) { void ((*volatile p)()); p = (void ((*)()))rb_thread_call_without_gvl; return !p; }
/* end */

--------------------

have_func: checking for rb_thread_blocking_region()... -------------------- no

"gcc -o conftest -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/x86_64-linux -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/ruby/backward -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0 -I. -I/home/t480s/.rbenv/versions/2.3.5/include    -I/usr/local/opt/openssl/include conftest.c  -L. -L/home/t480s/.rbenv/versions/2.3.5/lib -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/usr/local/opt/openssl/lib     -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/home/t480s/.rbenv/versions/2.3.5/lib -lruby-static  -lpthread -ldl -lcrypt -lm   -lc"
conftest.c: In function ‘t’:
conftest.c:13:57: error: ‘rb_thread_blocking_region’ undeclared (first use in this function)
   13 | int t(void) { void ((*volatile p)()); p = (void ((*)()))rb_thread_blocking_region; return !p; }
      |                                                         ^~~~~~~~~~~~~~~~~~~~~~~~~
conftest.c:13:57: note: each undeclared identifier is reported only once for each function it appears in
checked program was:
/* begin */
 1: #include "ruby.h"
 2: 
 3: /*top*/
 4: extern int t(void);
 5: int main(int argc, char **argv)
 6: {
 7:   if (argc > 1000000) {
 8:     printf("%p", &t);
 9:   }
10: 
11:   return 0;
12: }
13: int t(void) { void ((*volatile p)()); p = (void ((*)()))rb_thread_blocking_region; return !p; }
/* end */

"gcc -o conftest -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/x86_64-linux -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/ruby/backward -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0 -I. -I/home/t480s/.rbenv/versions/2.3.5/include    -I/usr/local/opt/openssl/include conftest.c  -L. -L/home/t480s/.rbenv/versions/2.3.5/lib -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/usr/local/opt/openssl/lib     -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/home/t480s/.rbenv/versions/2.3.5/lib -lruby-static  -lpthread -ldl -lcrypt -lm   -lc"
/usr/bin/ld: /tmp/ccudyuGB.o: in function `t':
conftest.c:(.text+0x49): undefined reference to `rb_thread_blocking_region'
collect2: error: ld returned 1 exit status
checked program was:
/* begin */
 1: #include "ruby.h"
 2: 
 3: /*top*/
 4: extern int t(void);
 5: int main(int argc, char **argv)
 6: {
 7:   if (argc > 1000000) {
 8:     printf("%p", &t);
 9:   }
10: 
11:   return 0;
12: }
13: extern void rb_thread_blocking_region();
14: int t(void) { rb_thread_blocking_region(); return 0; }
/* end */

--------------------

have_func: checking for rb_wait_for_single_fd()... -------------------- yes

"gcc -o conftest -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/x86_64-linux -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/ruby/backward -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0 -I. -I/home/t480s/.rbenv/versions/2.3.5/include    -I/usr/local/opt/openssl/include conftest.c  -L. -L/home/t480s/.rbenv/versions/2.3.5/lib -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/usr/local/opt/openssl/lib     -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/home/t480s/.rbenv/versions/2.3.5/lib -lruby-static  -lpthread -ldl -lcrypt -lm   -lc"
conftest.c: In function ‘t’:
conftest.c:13:57: error: ‘rb_wait_for_single_fd’ undeclared (first use in this function)
   13 | int t(void) { void ((*volatile p)()); p = (void ((*)()))rb_wait_for_single_fd; return !p; }
      |                                                         ^~~~~~~~~~~~~~~~~~~~~
conftest.c:13:57: note: each undeclared identifier is reported only once for each function it appears in
checked program was:
/* begin */
 1: #include "ruby.h"
 2: 
 3: /*top*/
 4: extern int t(void);
 5: int main(int argc, char **argv)
 6: {
 7:   if (argc > 1000000) {
 8:     printf("%p", &t);
 9:   }
10: 
11:   return 0;
12: }
13: int t(void) { void ((*volatile p)()); p = (void ((*)()))rb_wait_for_single_fd; return !p; }
/* end */

"gcc -o conftest -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/x86_64-linux -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/ruby/backward -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0 -I. -I/home/t480s/.rbenv/versions/2.3.5/include    -I/usr/local/opt/openssl/include conftest.c  -L. -L/home/t480s/.rbenv/versions/2.3.5/lib -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/usr/local/opt/openssl/lib     -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/home/t480s/.rbenv/versions/2.3.5/lib -lruby-static  -lpthread -ldl -lcrypt -lm   -lc"
checked program was:
/* begin */
 1: #include "ruby.h"
 2: 
 3: /*top*/
 4: extern int t(void);
 5: int main(int argc, char **argv)
 6: {
 7:   if (argc > 1000000) {
 8:     printf("%p", &t);
 9:   }
10: 
11:   return 0;
12: }
13: extern void rb_wait_for_single_fd();
14: int t(void) { rb_wait_for_single_fd(); return 0; }
/* end */

--------------------

have_func: checking for rb_hash_dup()... -------------------- yes

"gcc -o conftest -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/x86_64-linux -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/ruby/backward -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0 -I. -I/home/t480s/.rbenv/versions/2.3.5/include    -I/usr/local/opt/openssl/include conftest.c  -L. -L/home/t480s/.rbenv/versions/2.3.5/lib -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/usr/local/opt/openssl/lib     -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/home/t480s/.rbenv/versions/2.3.5/lib -lruby-static  -lpthread -ldl -lcrypt -lm   -lc"
checked program was:
/* begin */
 1: #include "ruby.h"
 2: 
 3: /*top*/
 4: extern int t(void);
 5: int main(int argc, char **argv)
 6: {
 7:   if (argc > 1000000) {
 8:     printf("%p", &t);
 9:   }
10: 
11:   return 0;
12: }
13: int t(void) { void ((*volatile p)()); p = (void ((*)()))rb_hash_dup; return !p; }
/* end */

--------------------

have_func: checking for rb_intern3()... -------------------- yes

"gcc -o conftest -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/x86_64-linux -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/ruby/backward -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0 -I. -I/home/t480s/.rbenv/versions/2.3.5/include    -I/usr/local/opt/openssl/include conftest.c  -L. -L/home/t480s/.rbenv/versions/2.3.5/lib -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/usr/local/opt/openssl/lib     -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/home/t480s/.rbenv/versions/2.3.5/lib -lruby-static  -lpthread -ldl -lcrypt -lm   -lc"
conftest.c: In function ‘t’:
conftest.c:13:57: error: ‘rb_intern3’ undeclared (first use in this function); did you mean ‘rb_intern2’?
   13 | int t(void) { void ((*volatile p)()); p = (void ((*)()))rb_intern3; return !p; }
      |                                                         ^~~~~~~~~~
      |                                                         rb_intern2
conftest.c:13:57: note: each undeclared identifier is reported only once for each function it appears in
checked program was:
/* begin */
 1: #include "ruby.h"
 2: 
 3: /*top*/
 4: extern int t(void);
 5: int main(int argc, char **argv)
 6: {
 7:   if (argc > 1000000) {
 8:     printf("%p", &t);
 9:   }
10: 
11:   return 0;
12: }
13: int t(void) { void ((*volatile p)()); p = (void ((*)()))rb_intern3; return !p; }
/* end */

"gcc -o conftest -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/x86_64-linux -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/ruby/backward -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0 -I. -I/home/t480s/.rbenv/versions/2.3.5/include    -I/usr/local/opt/openssl/include conftest.c  -L. -L/home/t480s/.rbenv/versions/2.3.5/lib -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/usr/local/opt/openssl/lib     -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/home/t480s/.rbenv/versions/2.3.5/lib -lruby-static  -lpthread -ldl -lcrypt -lm   -lc"
checked program was:
/* begin */
 1: #include "ruby.h"
 2: 
 3: /*top*/
 4: extern int t(void);
 5: int main(int argc, char **argv)
 6: {
 7:   if (argc > 1000000) {
 8:     printf("%p", &t);
 9:   }
10: 
11:   return 0;
12: }
13: extern void rb_intern3();
14: int t(void) { rb_intern3(); return 0; }
/* end */

--------------------

have_header: checking for mysql.h... -------------------- yes

"gcc -E -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/x86_64-linux -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/ruby/backward -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0 -I. -I/usr/include/mysql -I/home/t480s/.rbenv/versions/2.3.5/include    -I/usr/local/opt/openssl/include  conftest.c -o conftest.i"
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: #include <mysql.h>
/* end */

--------------------

have_header: checking for errmsg.h... -------------------- yes

"gcc -E -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/x86_64-linux -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/ruby/backward -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0 -I. -I/usr/include/mysql -I/home/t480s/.rbenv/versions/2.3.5/include    -I/usr/local/opt/openssl/include  conftest.c -o conftest.i"
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: #include <errmsg.h>
/* end */

--------------------

have_header: checking for mysqld_error.h... -------------------- yes

"gcc -E -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/x86_64-linux -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/ruby/backward -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0 -I. -I/usr/include/mysql -I/home/t480s/.rbenv/versions/2.3.5/include    -I/usr/local/opt/openssl/include  conftest.c -o conftest.i"
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: #include <mysqld_error.h>
/* end */

--------------------

"gcc -o conftest -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/x86_64-linux -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0/ruby/backward -I/home/t480s/.rbenv/versions/2.3.5/include/ruby-2.3.0 -I. -I/usr/include/mysql -I/home/t480s/.rbenv/versions/2.3.5/include    -I/usr/local/opt/openssl/include conftest.c  -L. -L/home/t480s/.rbenv/versions/2.3.5/lib -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/usr/local/opt/openssl/lib    -L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -lrt -lssl -lcrypto -ldl -lresolv  -Wl,-rpath,/home/t480s/.rbenv/versions/2.3.5/lib -L/home/t480s/.rbenv/versions/2.3.5/lib -lruby-static  -Wall -funroll-loops -lpthread -ldl -lcrypt -lm   -lc"
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main() {return 0;}
/* end */


For anyone got the error : Failed to build gem native extension ”, while running sudo gem install rails , and it said somethings about “nokogiri”. It is possible because Target 401k are using the same ruby version as system is using (for mac user). The solution would be install RVM, use RVM to install another ruby version.

Is there an alternative solution that doesn’t involve changing the ruby version?

I’m upgrading to LTS version, and working well roomme best regards

This worked for me on a regular install of Ubuntu 18.04 LTS when trying to install Typhoeus. Took ages to get there, thanks! FaceTime PC