Error occurred while installing mysql2

Hello I have download a repo from github. The app is built on rails 4.2. And ruby 2.3.1 But when i am trying to run bundle install, it is showing error occUrred while installing mysql2(0.5.3)

How can i solve this problem.

My local machine configure with rails 6 and ruby 2.7.

1 Like

Hi, @SM_Ehsan -

I’ve had this issue on my local machine while working with MySQL.

This is what has worked for me - though I can’t guarantee it’ll work on your computer.

gem install mysql2 -v '0.5.3' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include

thank you for your replay. now this error is showing.

	ERROR: Failed to build gem native extension.

    current directory: /home/smehsan/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3/ext/mysql2
/home/smehsan/.rbenv/versions/2.7.1/bin/ruby -I /home/smehsan/.rbenv/versions/2.7.1/lib/ruby/2.7.0 -r ./siteconf20200913-24911-1k35oj2.rb extconf.rb --with-ldflags\=-L/usr/local/opt/openssl/lib --with-cppflags\=-I/usr/local/opt/openssl/include
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/home/smehsan/.rbenv/versions/2.7.1/bin/$(RUBY_BASE_NAME)
	--with-mysql-dir
	--without-mysql-dir
	--with-mysql-include
	--without-mysql-include=${mysql-dir}/include
	--with-mysql-lib
	--without-mysql-lib=${mysql-dir}/lib
	--with-mysql-config
	--without-mysql-config
	--with-mysqlclient-dir
	--without-mysqlclient-dir
	--with-mysqlclient-include
	--without-mysqlclient-include=${mysqlclient-dir}/include
	--with-mysqlclient-lib
	--without-mysqlclient-lib=${mysqlclient-dir}/lib
	--with-mysqlclientlib
	--without-mysqlclientlib
/home/smehsan/.rbenv/versions/2.7.1/lib/ruby/2.7.0/mkmf.rb:1050:in `block in find_library': undefined method `split' for nil:NilClass (NoMethodError)
	from /home/smehsan/.rbenv/versions/2.7.1/lib/ruby/2.7.0/mkmf.rb:1050:in `collect'
	from /home/smehsan/.rbenv/versions/2.7.1/lib/ruby/2.7.0/mkmf.rb:1050:in `find_library'
	from extconf.rb:87:in `<main>'

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

  /home/smehsan/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/extensions/x86_64-linux/2.7.0/mysql2-0.5.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/smehsan/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3 for inspection.
Results logged to /home/smehsan/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/extensions/x86_64-linux/2.7.0/mysql2-0.5.3/gem_make.out

You need to install the specific MySQL client dependencies. For example, on Ubuntu Linux:

sudo apt-get install libmysqlclient-dev

6 Likes

On macOS I fixed it by first running brew install mysql, then bundle install worked fine.

It’s a bummer though that MySQL needs to be installed even for running Rails tests unrelated to MySQL.

1 Like

@sdubois You could remove the Gemfile.lock (I do this often) and then bundle without the db group.

Just make sure you don’t commit the changes to Gemfile.lock :slight_smile:

1 Like