2

I am running Ruby 1.9.3 on Debian, with no Rails. I have a script that needs mysql, so I ran gem install mysql, and it installed fine.

However, when I execute the script I get a "cannot load such file" error.

/usr/local/lib/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:51:in `require': cannot load such file -- mysql (LoadError)
    from /usr/local/lib/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:51:in `require'
    from db-test.rb:5:in `<main>'

Further info: I have updated to RBENV, but now the mysql won't install.

ruby --version gives:

ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]

which ruby gives:

/home/poconnor/.rbenv/shims/ruby

Output of gem environment is:

    RubyGems Environment:
  - RUBYGEMS VERSION: 2.1.11
  - RUBY VERSION: 1.9.3 (2013-11-22 patchlevel 484) [x86_64-linux]
  - INSTALLATION DIRECTORY: /home/poconnor/ruby/gems
  - RUBY EXECUTABLE: /home/poconnor/.rbenv/versions/1.9.3-p484/bin/ruby
  - EXECUTABLE DIRECTORY: /home/poconnor/ruby/gems/bin
  - SPEC CACHE DIRECTORY: /home/poconnor/.gem/specs
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /home/poconnor/ruby/gems
     - /lib/ruby/gems/1.9.3
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /home/poconnor/.rbenv/versions/1.9.3-p484/bin
     - /home/poconnor/.rbenv/libexec
     - /home/poconnor/.rbenv/plugins/ruby-build/bin
     - /home/poconnor/.rbenv/shims
     - /home/poconnor/.rbenv/bin
     - /usr/lib/lightdm/lightdm
     - /usr/local/sbin
     - /usr/local/bin
     - /usr/sbin
     - /usr/bin
     - /sbin
     - /bin
     - /usr/games
     - /usr/local/games
     - /usr/java/jdk1.7.0_17/bin
     - /usr/java/jdk1.7.0_17/bin
     - /usr/java/jdk1.7.0_17/bin
     - /usr/java/jdk1.7.0_17/bin

When I try gem install mysql I get:

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

    /home/poconnor/.rbenv/versions/1.9.3-p484/bin/ruby extconf.rb
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** 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/poconnor/.rbenv/versions/1.9.3-p484/bin/ruby
        --with-mysql-config
        --without-mysql-config
        --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-mysqlclientlib
        --without-mysqlclientlib
        --with-mlib
        --without-mlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-zlib
        --without-zlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-socketlib
        --without-socketlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-nsllib
        --without-nsllib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-mygcclib
        --without-mygcclib
        --with-mysqlclientlib
        --without-mysqlclientlib


Gem files will remain installed in /home/poconnor/ruby/gems/gems/mysql-2.9.1 for inspection.
Results logged to /home/poconnor/ruby/gems/gems/mysql-2.9.1/ext/mysql_api/gem_make.out

Help much appreciated on this.

pokero
  • 1,009
  • 3
  • 13
  • 27

3 Answers3

2

You have an issue with your setup. /usr/lib/ruby/gems/1.9.1/gems/mysql-2.9.1 indicated the gem is installed under ruby 1.9.1 but you mention that you are using ruby 1.9.3. Are you using a ruby version control system such as RVM or RBENV?

Update

Based on your gem environment output, there is definitely something wrong with your system ruby setup. It says it is ruby version 1.9.3 but it points to a folder called 1.9.1. My recommendation is to forget about your system ruby and install RBENV(linked above). It is a simplified version of RVM and I believe it would fix your problem. It will also greatly simplify your life later when you decide you want to use ruby 2 and beyond.

Josh
  • 5,631
  • 1
  • 28
  • 54
  • Yes, when I do ruby --version I get ruby 1.9.3p194. I had RVM installed before (didn't really know what I was doing), and have since removed it using rvm implode, and gem uninstall rvm. Is there anything else I could do to sort out version setup issues? – pokero Nov 21 '13 at 18:33
  • @pokero can you run `which ruby` in your terminal and paste the output? – Josh Nov 21 '13 at 18:35
  • Sure, I get /usr/bin/ruby – pokero Nov 21 '13 at 18:38
  • @pokero when you had rvm, did you install ruby 1.9.1? – Josh Nov 21 '13 at 18:47
  • Hi Josh, I honestly can't remember. It was over a month ago unfortunately. – pokero Nov 21 '13 at 18:53
  • Hi Josh, thanks again. I have installed RBENV now, but the mysql gem will not install. I have updated the original question above with the new info. – pokero Nov 22 '13 at 13:41
1

Items to verify:

  1. Add /usr/lib/ruby/gems/1.9.1/gems/ to your PATH variable
  2. require 'rubygems' before you require 'mysql' in your script <-- My bad. This is irrelevant in 1.9 because 'rubygems' is implicitly required

Recommendation: Use rvm (https://rvm.io/rvm/install) to manage ruby and gem installations (with gemsets)

Update:

Josh is correct that you should use a ruby versioning tool like RVM or RBENV, however in the short-term you could modify your .bashrc file and add:

  • export GEM_HOME=$HOME/ruby/gems
  • export GEM_PATH=$GEM_HOME:/lib/ruby/gems/1.9.3
  • export GEM_CACHE=$GEM_HOME/cache
  • export PATH=$PATH:$HOME/ruby/gems/bin

...and then run source ~/.bashrc to change the gem path to 1.9.3 and then gem install mysql again.

grenierm5
  • 186
  • 4
  • 14
  • Thanks, I added that dir to my path and have require "rubygems" before mysql, but the exact same issue is there. – pokero Nov 21 '13 at 18:31
  • It looks like a permissions issue. Perhaps this issue is similar: http://stackoverflow.com/questions/7518665/ruby-gem-environment-issue-loaderror-no-such-file-to-load-robots?rq=1 – grenierm5 Nov 21 '13 at 18:37
  • I actually chmod'ed the whole /usr/lib/ruby/gems/1.9.1 directory to be 777, but same result. – pokero Nov 21 '13 at 18:40
  • Please post the output from 'gem environment' in the original post. – grenierm5 Nov 21 '13 at 18:46
  • I tried that (the bashrc changes), but no luck. I have installed RBENV, but gem will not install now. – pokero Nov 22 '13 at 13:41
0

Thanks very much to Josh and grenierm5 for their time and advice - RBENV was a great help, definitely makes life easier.

The answer is two-part: RBENV to solve my weird environment issues, but then also to solve the mysql gem install issue, I had to install the following Debian packages:

apt-get install mysql-client libmysqlclient-dev

This was answered here: ERROR: Failed to build gem native extension (mysql2 on rails 3.2.3)

Community
  • 1
  • 1
pokero
  • 1,009
  • 3
  • 13
  • 27