rails newしたあとのbundle install時に下記のようなnokogiriエラー
が発生することがよくあります。
私もたびたび発生し解決までに多大な時間を要してしまいましたが
なんとか解決することができましたので記録として残すこととしました。
【エラーメッセージ】
An error occurred while installing nokogiri (1.15.1), and Bundler cannot
continue.
In Gemfile:
rails was resolved to 7.0.4.3, which depends on
actioncable was resolved to 7.0.4.3, which depends on
actionpack was resolved to 7.0.4.3, which depends on
actionview was resolved to 7.0.4.3, which depends on
rails-dom-testing was resolved to 2.0.3, which depends on
nokogiri
【私のPC環境】
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x64-mingw32]
Rails 7.0.5
Bundler version 2.4.13
gem 3.4.13
nokogiri (1.15.2, 1.15.1 x64-mingw32)
【解決方法】
下記コマンドを実行する。
C:#> gem install nokogiri --platform=ruby
Fetching nokogiri-1.15.2.gem
Temporarily enhancing PATH for MSYS/MINGW...
Building native extensions. This could take a while...
Successfully installed nokogiri-1.15.2
Parsing documentation for nokogiri-1.15.2
Installing ri documentation for nokogiri-1.15.2
Done installing documentation for nokogiri after 45 seconds
1 gem installed
そのあとに下記コマンドを事項
C:#> bundle install
.
.
.
Fetching web-console 4.2.0
Installing web-console 4.2.0
Bundle complete! 15 Gemfile dependencies, 69 gems now installed.
Use bundle info [gemname]
to see where a bundled gem is installed.
bundleに成功したので下記コマンドでrailsサーバを立ち上げる。
ブラウザーに localhost:3000 を入力してrails画面がでれば成功です。
しかし、Windowsの場合下記エラーメッセージがでます。
C:#> rails s
=> Booting Puma
=> Rails 7.0.5 application starting in development
=> Run bin/rails server --help
for more startup options
Exiting
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/tzinfo-2.0.6/lib/tzinfo/data_source.rb:159:in `rescue in create_default_data_source': tzinfo-data is not present. Please add gem 'tzinfo-data' to your Gemfile and run bundle install (TZInfo::DataSourceNotFound)
gemfileに下記いちにコメントアウト”#”を入力する。
(コメントアウトの後ろ部分は削除してもよい)
gem "tzinfo-data"#, platforms: %i[ mingw mswin x64_mingw jruby ]
そのあとに再度下記コマンドを実行する。
C:#> bundle install
bundleが成功し gemfile.lock ファイルが作成されれば成功です。
再度下記コマンドを事項し、ブラウザーに
C:#> rails s
=> Booting Puma
=> Rails 7.0.5 application starting in development
=> Run bin/rails server --help
for more startup options
*** SIGUSR2 not implemented, signal based restart unavailable!
*** SIGUSR1 not implemented, signal based restart unavailable!
*** SIGHUP not implemented, signal based logs reopening unavailable!
Puma starting in single mode...
- Puma version: 5.6.5 (ruby 3.0.2-p107) ("Birdie's Version")
- Min threads: 5
- Max threads: 5
- Environment: development
-
PID: 2780
- Listening on http://[::1]:3000
- Listening on http://127.0.0.1:3000
Use Ctrl-C to stop
再度ブラウザーに localhost:3000 を入力してrails画面がでれば成功です。ブラウザーに localhost:3000 を入力してrails画面がでれば成功です。