没有加载Ruby宝石

时间:2013-11-21 18:06:36

标签: ruby gem

我在Debian上运行Ruby 1.9.3,没有Rails。我有一个需要mysql的脚本,所以我运行gem install mysql,安装得很好。

但是,当我执行脚本时,我收到“无法加载此类文件”错误。

/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>'

更多信息:我已更新到RBENV,但现在不会安装mysql。

ruby --version给出:

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

which ruby给出:

/home/poconnor/.rbenv/shims/ruby

gem environment的输出是:

    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

当我尝试gem install mysql时,我得到了:

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

非常感谢你。

3 个答案:

答案 0 :(得分:2)

您的设置存在问题。 /usr/lib/ruby/gems/1.9.1/gems/mysql-2.9.1表示gem已安装在ruby 1.9.1下,但你提到你使用的是ruby 1.9.3。您使用的是ruby版本控制系统,例如RVMRBENV吗?

<强>更新

根据您的gem environment输出,您的系统ruby设置肯定存在问题。它说它是ruby版本1.9.3,但它指向一个名为1.9.1的文件夹。我的建议是忘记你的系统ruby并安装RBENV(上面链接)。它是RVM的简化版本,我相信它可以解决您的问题。当您决定使用ruby 2及更高版本时,它也将大大简化您的生活。

答案 1 :(得分:1)

要验证的项目:

  1. 将/usr/lib/ruby/gems/1.9.1/gems/添加到PATH变量中
  2. 在您的脚本中需要'mysql'之前需要'rubygems'&lt; - 我的不好。这与1.9无关,因为隐含地要求'rubygems'
  3. 建议:使用rvm(https://rvm.io/rvm/install)管理ruby和gem安装(使用gemsets)

    <强>更新

    Josh是正确的,你应该使用像RVM或RBENV这样的ruby版本控制工具,但是在短期内你可以修改你的.bashrc文件并添加:

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

    ...然后运行source ~/.bashrc将gem路径更改为1.9.3,然后再次gem install mysql

答案 2 :(得分:0)

非常感谢Josh和grenierm5的时间和建议--RBENV是一个很好的帮助,绝对让生活更轻松。

答案是两部分:RBENV解决了我奇怪的环境问题,但后来还解决了mysql的gem安装问题,我不得不安装以下Debian软件包:

apt-get install mysql-client libmysqlclient-dev

这回答:ERROR: Failed to build gem native extension (mysql2 on rails 3.2.3)