简体   繁体   中英

How to run "rails console" without nokogiri "cannot load such file -- nokogiri/nokogiri (LoadError)" error on Mac?

I'm trying to build a Rails application on Mac OS Big Sur with the following versions...

$ rails --version
Rails 5.0.7.2

$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]

After running "bundle clean --force" and "bundle install"...

$ bundle install
Your Gemfile lists the gem jquery-rails (>= 0) more than once.
You should probably keep only one of them.
Remove any duplicate entries and specify the gem only once.
While it's not a problem now, it could cause errors if you change the version of one of them later.
Using rake 13.0.3
Using concurrent-ruby 1.1.8
Using i18n 1.8.10
Using minitest 5.14.4
Using thread_safe 0.3.6
Using tzinfo 1.2.9
Using activesupport 5.0.7.2
Using builder 3.2.4
Using erubis 2.7.0
Using racc 1.5.2
Using nokogiri 1.11.3 (arm64-darwin)
Using rails-dom-testing 2.0.3
Using crass 1.0.6
Using loofah 2.9.1
Using rails-html-sanitizer 1.3.0
Using actionview 5.0.7.2
Using rack 2.2.3
Using rack-test 0.6.3
Using actionpack 5.0.7.2
Using nio4r 2.5.7
Using websocket-extensions 0.1.5
Using websocket-driver 0.6.5
Using actioncable 5.0.7.2
Using globalid 0.4.2
Using activejob 5.0.7.2
Using mini_mime 1.1.0
Using mail 2.7.1
Using actionmailer 5.0.7.2
Using activemodel 5.0.7.2
Using arel 7.1.4
Using activerecord 5.0.7.2
Using bindex 0.8.1
Using bundler 2.2.16
Using byebug 11.1.3
Using coffee-script-source 1.12.2
Using execjs 2.7.0
Using coffee-script 2.4.1
Using method_source 1.0.0
Using thor 1.1.0
Using railties 5.0.7.2
Using coffee-rails 4.2.2
Using faraday-net_http 1.0.1
Using multipart-post 2.1.1
Using ruby2_keywords 0.0.4
Using faraday 1.3.0
Using ffi 1.15.0
Using hashie 4.1.0
Using jbuilder 2.11.2
Using jquery-rails 4.4.0
Using jquery-ui-rails 6.0.1
Using json 2.5.1
Using jwt 1.5.6
Using rb-fsevent 0.10.4
Using rb-inotify 0.10.1
Using listen 3.0.8
Using multi_json 1.15.0
Using multi_xml 0.6.0
Using oauth 0.5.6
Using oauth2 1.4.7
Using omniauth 1.9.1
Using omniauth-oauth2 1.3.1
Using omniauth-facebook 8.0.0
Using omniauth-google-oauth2 0.5.2
Using omniauth-linkedin-oauth2 1.0.0
Using omniauth-oauth 1.2.0
Using omniauth-twitter 1.4.0
Using pg 0.21.0
Using puma 3.12.6
Using pundit 2.1.0
Using sprockets 3.7.2
Using sprockets-rails 3.2.2
Using rails 5.0.7.2
Using rails-controller-testing 1.0.5
Using recaptcha 5.7.0
Using rmagick 4.2.2
Using ruby-filemagic 0.7.2
Using sass-listen 4.0.0
Using sass 3.7.4
Using tilt 2.0.10
Using sass-rails 5.0.7
Using spring 2.1.1
Using spring-watcher-listen 2.0.1
Using sqlite3 1.4.2
Using turbolinks-source 5.2.0
Using turbolinks 5.2.1
Using uglifier 4.2.0
Using web-console 3.7.0
Bundle complete! 28 Gemfile dependencies, 87 gems now installed.
Bundled gems are installed into `./vendor/bundle`

I tried to fire up my rails console, but got this error

$ rails console
/Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/nokogiri-1.11.3-arm64-darwin/lib/nokogiri/extension.rb:25:in `require': cannot load such file -- nokogiri/nokogiri (LoadError)
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/nokogiri-1.11.3-arm64-darwin/lib/nokogiri/extension.rb:25:in `rescue in <top (required)>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/nokogiri-1.11.3-arm64-darwin/lib/nokogiri/extension.rb:4:in `<top (required)>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/nokogiri-1.11.3-arm64-darwin/lib/nokogiri.rb:11:in `require'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/nokogiri-1.11.3-arm64-darwin/lib/nokogiri.rb:11:in `<top (required)>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/loofah-2.9.1/lib/loofah.rb:4:in `require'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/loofah-2.9.1/lib/loofah.rb:4:in `<top (required)>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/rails-html-sanitizer-1.3.0/lib/rails-html-sanitizer.rb:2:in `require'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/rails-html-sanitizer-1.3.0/lib/rails-html-sanitizer.rb:2:in `<top (required)>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers/sanitize_helper.rb:2:in `require'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers/sanitize_helper.rb:2:in `<top (required)>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers/text_helper.rb:32:in `require'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers/text_helper.rb:32:in `<module:TextHelper>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers/text_helper.rb:29:in `<module:Helpers>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers/text_helper.rb:6:in `<module:ActionView>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers/text_helper.rb:4:in `<top (required)>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers/form_tag_helper.rb:18:in `require'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers/form_tag_helper.rb:18:in `<module:FormTagHelper>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers/form_tag_helper.rb:14:in `<module:Helpers>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers/form_tag_helper.rb:8:in `<module:ActionView>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers/form_tag_helper.rb:6:in `<top (required)>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers/form_helper.rb:4:in `require'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers/form_helper.rb:4:in `<top (required)>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers.rb:50:in `require'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers.rb:50:in `<module:Helpers>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers.rb:4:in `<module:ActionView>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/actionview-5.0.7.2/lib/action_view/helpers.rb:3:in `<top (required)>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/sprockets-rails-3.2.2/lib/sprockets/rails/context.rb:1:in `require'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/sprockets-rails-3.2.2/lib/sprockets/rails/context.rb:1:in `<top (required)>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/sprockets-rails-3.2.2/lib/sprockets/railtie.rb:7:in `require'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/sprockets-rails-3.2.2/lib/sprockets/railtie.rb:7:in `<top (required)>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/sass-rails-5.0.7/lib/sass/rails/railtie.rb:3:in `require'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/sass-rails-5.0.7/lib/sass/rails/railtie.rb:3:in `<top (required)>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/sass-rails-5.0.7/lib/sass/rails.rb:11:in `require'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/sass-rails-5.0.7/lib/sass/rails.rb:11:in `<top (required)>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/sass-rails-5.0.7/lib/sass-rails.rb:1:in `require'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/sass-rails-5.0.7/lib/sass-rails.rb:1:in `<top (required)>'
    from /Library/Ruby/Gems/2.6.0/gems/bundler-2.2.16/lib/bundler/runtime.rb:66:in `require'
    from /Library/Ruby/Gems/2.6.0/gems/bundler-2.2.16/lib/bundler/runtime.rb:66:in `block (2 levels) in require'
    from /Library/Ruby/Gems/2.6.0/gems/bundler-2.2.16/lib/bundler/runtime.rb:61:in `each'
    from /Library/Ruby/Gems/2.6.0/gems/bundler-2.2.16/lib/bundler/runtime.rb:61:in `block in require'
    from /Library/Ruby/Gems/2.6.0/gems/bundler-2.2.16/lib/bundler/runtime.rb:50:in `each'
    from /Library/Ruby/Gems/2.6.0/gems/bundler-2.2.16/lib/bundler/runtime.rb:50:in `require'
    from /Library/Ruby/Gems/2.6.0/gems/bundler-2.2.16/lib/bundler.rb:173:in `require'
    from /Users/davea/Documents/workspace/scale/config/application.rb:7:in `<top (required)>'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/spring-2.1.1/lib/spring/application.rb:92:in `require'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/spring-2.1.1/lib/spring/application.rb:92:in `preload'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/spring-2.1.1/lib/spring/application.rb:157:in `serve'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/spring-2.1.1/lib/spring/application.rb:145:in `block in run'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/spring-2.1.1/lib/spring/application.rb:139:in `loop'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/spring-2.1.1/lib/spring/application.rb:139:in `run'
    from /Users/davea/Documents/workspace/scale/vendor/bundle/ruby/2.6.0/gems/spring-2.1.1/lib/spring/application/boot.rb:19:in `<top (required)>'
    from /Library/Ruby/Site/2.6.0/rubygems/core_ext/kernel_require.rb:85:in `require'
    from /Library/Ruby/Site/2.6.0/rubygems/core_ext/kernel_require.rb:85:in `require'
    from -e:1:in `<main>'

Is there another version of Ruby or Nokogiri I should be using to fix things? My Gemfile is below

$ cat Gemfile
source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.6'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'pg', '0.21.0'
gem 'omniauth-oauth2', '~> 1.3.1'
gem 'omniauth-google-oauth2'
gem 'omniauth-facebook'
gem 'omniauth-twitter'
gem 'omniauth-linkedin-oauth2'
gem 'ruby-filemagic' 
gem 'rmagick'
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem "recaptcha", require: "recaptcha/rails"
gem 'pundit'
gem 'rails-controller-testing'

From your ruby -v , I see that you are in an Intel x86 Mac, but the gem that your trying to build ( nokogiri-1.11.3-arm64-darwin ) is for new Mac ARM M1 chips. If this is the cause, it means your are using precompiled gems.

Try uninstalling the gem, specify that you don't want to use precompiled gems, and reinstall.

gem uninstall nokogiri
bundle config set force_ruby_platform true
bundle install

Checkout this related docs

The first way of installation on Nogori website works for my mac (Big Sur 11.6):

1. gen uninstall nokogiri (remove all existing versions)
2. gem install nokogiri --platform=ruby

In case the bundle install kept re-install both versions for you.

Got stuck with the same issue on Mac. These steps helped to resolve my issue.

Run gem uninstall nokogiri

Add gem "nokogiri", ">= 1.6.7.rc" to your Gemfile (Look for the latest version of Nokogiri in Nokogiri's github repo)

Run bundle install

Run bundle update nokogiri if bundle has locked Nokogiri at some version.

Reference: https://stackoverflow.com/a/33410164/5210943

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