[Ruby] [Ruby] gem should have been installed in vendor/bundle, but require error

less than 1 minute read

Background

I wanted to learn a little about scraping, Implemented with reference to Making scraping masters in 20 minutes using Ruby. “Gem should be installed under vendor/bundle.” Add --path vendor/bundle option when installing bundle as instructed.

Error occurred

If you write the code as in the article to main.rb and run main.rb,

training $ ruby main.rb
Traceback (most recent call last):
2: from main.rb:2:in `<main>'
1: from /Users/yusaku/.rbenv/versions/2.6.0/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Users/yusaku/.rbenv/versions/2.6.0/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file --nokogiri (LoadError)

It says 'require': cannot load such file --nokogiri (LoadError). nokogiri has bundle installed under vender/bundle.

training $ ls vendor/bundle/ruby/2.6.0/gems/
byebug-11.1.3 method_source-1.0.0 nokogiri-1.10.10 pry-byebug-3.9.0
coderay-1.1.3 mini_portile2-2.4.0 pry-0.13.1

Resolution

Looking closely at the contents of the error, It looks like I’m looking for /Users/yusaku/.rbenv/. I was relieved, but I wasn’t doing bundle exec.

training $ bundle exec ruby main.rb

Now I can run main.rb without any error (require “nokogiri” was done). By doing bundle exec, it looks like it searched for the gem under the same directory.