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

Background

I wanted to learn a little about scraping, Implemented with reference to Making a scraping source in 20 minutes using Ruby. Since it says "Gem should be installed under vendor / bundle." Added --path vendor / bundle option when doing bundle install as instructed.

Error occurred

If you write the code as in the article to main.rb and execute 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

Solution

If you look closely at the details of the error, It looks like you are looking for a subordinate to /Users/yusaku/.rbenv/. I was surprised at that, but I didn't do bundle exec.

training $ bundle exec ruby main.rb

Now I was able to run main.rb without any errors (and also require "nokogiri"). By doing bundle exec, it looks like they searched for the gem under the same directory.

Recommended Posts

[Ruby] The gem should have been installed in vendor / bundle, but an error occurs in require
It should be the same as the sample when logging in to Twitter, but an error occurs ~ resolution
What to do if you should have installed Rails but an error occurs with rails -v (for beginners)
I can't import the package that should have been conda installed in Anaconda
An error occurs when codedeploy-agent is installed in Ubuntu Server 20.04
I don't see an error in Rails bundle install ... the solution
If the JDK version is correct in Eclipse but an error occurs or the compiler does not work properly
repository The specified gem is bundle installed but not updated to the latest version
Customize the display when an error such as 404 Not Found occurs in Spring Boot