简体   繁体   English

Ruby脚本无法加载通过捆绑程序安装的gem

[英]Ruby script cannot load a gem installed via bundler

I am trying to include the ruby-mysql gem in my ruby script. 我正在尝试将ruby-mysql gem包含在我的ruby脚本中。 I have installed the gem using bundler but when I run bundle exec ./mysql_connector , I receive the error ./mysql_connector:4:in ``require': cannot load such file -- ruby-mysql (LoadError) . 我已经使用bundler安装了gem,但是当我运行bundle exec ./mysql_connector ,在./mysql_connector:4:in ``require': cannot load such file -- ruby-mysql (LoadError)收到错误./mysql_connector:4:in ``require': cannot load such file -- ruby-mysql (LoadError) Can you please help me troubleshoot what the problem is? 您能帮我解决问题是什么吗?

What I did 我做了什么

Installed rails in my home directory. 在我的主目录中安装了rails。 I do not have root access to the server so I have installed rails in my local directory by following the instructions here: http://www.r-bloggers.com/installing-ruby-on-linux-as-a-user-other-than-root/ 我没有对该服务器的root访问权限,因此我已按照此处的说明在本地目录中安装了rails: http : //www.r-bloggers.com/installing-ruby-on-linux-as-a-user-非根目录/

Created a directory for my application. 为我的应用程序创建了一个目录。 My application resides in my home directory in a folder called connector . 我的应用程序位于我的主目录中的一个名为connector的文件夹中。 It has a Gemfile that looks like this: 它具有一个Gemfile,如下所示:

source 'https://rubygems.org'
gem 'ruby-mysql'

Call bundle install . 调用bundle install

Using ruby-mysql 2.9.14
Using bundler 1.11.2
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Bundled gems are installed into ./vendor/bundle.

Add dependencies to my script. 向我的脚本添加依赖项。 My script is in connector/mysql_connector and it reads: 我的脚本在connector / mysql_connector中,内容为:

#!/home/dcox/bin/ruby
require 'rubygems'
require 'bundler/setup'
require 'ruby-mysql'

Make script executable. 使脚本可执行。 I saw that you need to run bundle exec using an executable file, so I followed the instructions here to make my script executable: http://commandercoriander.net/blog/2013/02/16/making-a-ruby-script-executable/ 我看到您需要使用可执行文件运行bundle exec ,因此我按照此处的说明将脚本设为可执行文件: http : //commandercoriander.net/blog/2013/02/16/making-a-ruby-script-可执行文件/

Run the script. 运行脚本。 I execute using bundle exec mysql_connector and see: 我使用bundle exec mysql_connector执行,请参见:

/home/dcox/bin/mysql_connector:4:in `require': cannot load such file -- ruby-mysql (LoadError)
    from /home/dcox/bin/mysql_connector:4:in `<main>'

Is it the $LOAD_PATH? 是$ LOAD_PATH吗? After searching around for answers, I discovered a lot of SO answers as well as a blog post ( https://codedecoder.wordpress.com/2013/09/23/require-and-load-in-ruby-loaderror-cannot-load-such-file/ ) that seem to suggest the problem is that the gem is not installed in a directory on the $LOAD_PATH. 在寻找答案之后,我发现了很多SO答案以及一篇博客文章( https://codedecoder.wordpress.com/2013/09/23/require-and-load-in-ruby-loaderror-cannot-似乎暗示问题的load-such-file / )是gem没有安装在$ LOAD_PATH的目录中。 Here is what I see when I run $LOAD_PATH from IRB: 这是从IRB运行$LOAD_PATH时看到的内容:

irb(main):002:0> $LOAD_PATH
=> ["/home/dcox/lib/ruby/site_ruby/2.1.0", 
"/home/dcox/lib/ruby/site_ruby/2.1.0/x86_64-linux", 
"/home/dcox/lib/ruby/site_ruby", "/home/dcox/lib/ruby/vendor_ruby/2.1.0", 
"/home/dcox/lib/ruby/vendor_ruby/2.1.0/x86_64-linux", 
"/home/dcox/lib/ruby/vendor_ruby", "/home/dcox/lib/ruby/2.1.0", 
"/home/dcox/lib/ruby/2.1.0/x86_64-linux"]

Next I checked to see the location of ruby-mysql: 接下来,我检查了ruby-mysql的位置:

dcox@analytics1:~/connector$ bundle show ruby-mysql
/data/home/dcox/connector/vendor/bundle/ruby/2.1.0/gems/ruby-mysql-2.9.14

Obviously my connector/vendor/bundle path is not on the $LOAD_PATH. 显然,我的connector/vendor/bundle路径不在$ LOAD_PATH上。 I could add it, but I have a feeling I am missing something simple here because bundler is supposed to work as long as you follow the instructions, right? 我可以添加它,但是我感觉这里缺少一些简单的东西,因为只要您按照说明进行操作,捆绑程序就可以正常工作,对吗?

Any advice or help is very much appreciated! 任何建议或帮助都非常感谢! Thanks!! 谢谢!!

如果您只需要此特定的gem,则require 'mysql'应该可以工作(例如, https : //github.com/tmtm/ruby-mysql/blob/master/test/test_mysql.rb#L10 )。

Your file should call Bundler.setup http://bundler.io/bundler_setup.html 您的文件应调用Bundler.setup http://bundler.io/bundler_setup.html

Better yet, if you instead call Bundler.require(:default) it will setup and require all the gems in your Gemfile for you. 更好的是,如果您改为调用Bundler.require(:default) ,它将设置并要求您Gemfile中的所有gem。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM