简体   繁体   中英

Ruby script cannot load a gem installed via bundler

I am trying to include the ruby-mysql gem in my ruby script. 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) . Can you please help me troubleshoot what the problem is?

What I did

Installed rails in my home directory. 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/

Created a directory for my application. My application resides in my home directory in a folder called connector . It has a Gemfile that looks like this:

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

Call 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:

#!/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/

Run the script. I execute using bundle exec mysql_connector and see:

/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? 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. Here is what I see when I run $LOAD_PATH from IRB:

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:

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. 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

Better yet, if you instead call Bundler.require(:default) it will setup and require all the gems in your Gemfile for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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