Ruby 2.0.0 cannot load such file even though gem i

2019-07-20 17:16发布

问题:

I've tried all of the other solutions people have talked about on here, but none of them have helped / applied.

I've written a Ruby script that requires the spreadsheet gem. The requiring works fine when I execute the script normally with ruby myscript.rb, but after running chmod +x myscript.rb, and then trying to run the program with ./myscript.rb I get the following error....

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- spreadsheet (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /Users/fcangialosi/dev/mTC/parse.rb:2:in `<top (required)>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /Users/fcangialosi/dev/mTC/interpreter.rb:1:in `<top (required)>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from ./pmcnp.rb:7:in `<main>'

The beginning of my script looks like this:

#!/usr/bin/ruby

require 'rubygems'
require 'spreadsheet'

If anyone has any ideas, I would really appreciate it.

回答1:

From your answers to the comments, the ruby you're running normally - and therefore the one in which your gems are installed - is /Users/fcangialosi/.rbenv/shims/ruby. When you make the script executable, it uses the hint in the script to know which program to use to execute the script. In your case, you have:

#!/usr/bin/ruby

So that's using whichever ruby version you have installed in /usr/bin/ruby. In order to use your rbenv ruby instead of /usr/bin/ruby, change the shebang line to:

#!/usr/bin/env ruby