There are no versions of nokogiri (~> 1.14) compatible with your Ruby & RubyGems. エラーの解消

仕事で

- Ruby 2.5.1
- Rails 5.2.6

という古い環境で開発することがあるのだが、タイトルのエラーで環境構築に失敗したでの調査して解決するところまでをメモしておく。ちなみにこの環境構築は以前うまくいっていた。

```bash
❯ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [-darwin22]
```

で、

```bash
❯ gem install rails -v 5.2.6
Fetching: concurrent-ruby-1.2.2.gem (100%)
Successfully installed concurrent-ruby-1.2.2
Fetching: thread_safe-0.3.6.gem (100%)
Successfully installed thread_safe-0.3.6
Fetching: tzinfo-1.2.11.gem (100%)
Successfully installed tzinfo-1.2.11
Fetching: i18n-1.13.0.gem (100%)
Successfully installed i18n-1.13.0
Fetching: activesupport-5.2.6.gem (100%)
Successfully installed activesupport-5.2.6
Fetching: racc-1.6.2.gem (100%)
Building native extensions. This could take a while...
Successfully installed racc-1.6.2
Fetching: mini_portile2-2.8.2.gem (100%)
Successfully installed mini_portile2-2.8.2
Fetching: nokogiri-1.15.2.gem (100%)
ERROR:  Error installing rails:
        The last version of nokogiri (>= 1.6) to support your Ruby & RubyGems was 1.12.5. Try installing it with `gem install nokogiri -v 1.12.5` and then running the current command again
        nokogiri requires Ruby version >= 2.7.0. The current ruby version is 2.5.0.
```

rails をインストールすると nokogiri のバージョンでエラーになる。とりあえず言われた通り、1.12.5 をインストールしてみる。

```bash
gem install nokogiri -v 1.12.5
```

で、再度インストールしてみると

```bash
❯ gem install rails -v 5.2.6
ERROR:  Error installing rails:
        There are no versions of nokogiri (~> 1.14) compatible with your Ruby & RubyGems. Maybe try installing an older version of the gem you're looking for?
        nokogiri requires Ruby version >= 2.7.0. The current ruby version is 2.5.0.
```

1.14 以降が必要ということでエラーになる。1.4 系で一番新しいのが 1.4.5 なので、とりあえず 1.4.5 を入れてみる。

- https://rubygems.org/gems/nokogiri/versions

```bash
❯ gem install nokogiri -v 1.14.5
Fetching: nokogiri-1.14.5.gem (100%)
ERROR:  Error installing nokogiri:
        There are no versions of nokogiri (= 1.14.5) compatible with your Ruby & RubyGems
        nokogiri requires Ruby version >= 2.7.0. The current ruby version is 2.5.0.
```

1.4.5 は Ruby 2.5 系に対応していないらしい。ただ、以前はインストールできたので、どの gem が依存する nokogiri のバージョンを変更したのか調べる。まず Rails 5.2 系の依存系を見る。

- https://github.com/rails/rails/blob/5-2-stable/rails.gemspec

```ruby
# frozen_string_literal: true

version = File.read(File.expand_path("RAILS_VERSION", __dir__)).strip

Gem::Specification.new do |s|
  # 略

  s.add_dependency "activesupport", version
  s.add_dependency "actionpack",    version
  s.add_dependency "actionview",    version
  s.add_dependency "activemodel",   version
  s.add_dependency "activerecord",  version
  s.add_dependency "actionmailer",  version
  s.add_dependency "activejob",     version
  s.add_dependency "actioncable",   version
  s.add_dependency "activestorage", version
  s.add_dependency "railties",      version

  s.add_dependency "bundler",         ">= 1.3.0"
  s.add_dependency "sprockets-rails", ">= 2.0.0"
end
```

となっていて、先程のインストールログを見ると、

```bash
❯ gem install rails -v 5.2.6
Fetching: concurrent-ruby-1.2.2.gem (100%)
Successfully installed concurrent-ruby-1.2.2
Fetching: thread_safe-0.3.6.gem (100%)
Successfully installed thread_safe-0.3.6
Fetching: tzinfo-1.2.11.gem (100%)
Successfully installed tzinfo-1.2.11
Fetching: i18n-1.13.0.gem (100%)
Successfully installed i18n-1.13.0
Fetching: activesupport-5.2.6.gem (100%)
...
Fetching: nokogiri-1.15.2.gem (100%)
ERROR:  Error installing rails:
        The last version of nokogiri (>= 1.6) to support your Ruby & RubyGems was 1.12.5. Try installing it with `gem install nokogiri -v 1.12.5` and then running the current command again
        nokogiri requires Ruby version >= 2.7.0. The current ruby version is 2.5.0.
```

で、activesupport は成功してその後失敗している感がある。gemspec の順番的に actionpack っぽい。

```bash
s.add_dependency "activesupport", version
s.add_dependency "actionpack",    version
s.add_dependency "actionview",    version
s.add_dependency "activemodel",   version
```

とりあえず、ruby 3 系の 3.2.2 がローカルにあったので、3.2.2 で rails 5.2.6 をインストールしてみる。

```bash
❯ gem install rails -v 5.2.6
Fetching tzinfo-1.2.11.gem
Fetching i18n-1.13.0.gem
Fetching concurrent-ruby-1.2.2.gem
Fetching activesupport-5.2.6.gem
Fetching minitest-5.18.0.gem
Fetching sprockets-4.2.0.gem
Fetching rack-2.2.7.gem
Fetching thread_safe-0.3.6.gem
Fetching nokogiri-1.15.2-arm64-darwin.gem
Fetching crass-1.0.6.gem
Fetching loofah-2.21.3.gem
Fetching rails-html-sanitizer-1.6.0.gem
Fetching rails-dom-testing-2.0.3.gem
Fetching rack-test-2.1.0.gem
Fetching erubi-1.12.0.gem
Fetching builder-3.2.4.gem
Fetching actionview-5.2.6.gem
Fetching actionpack-5.2.6.gem
Fetching sprockets-rails-3.4.2.gem
Fetching thor-1.2.2.gem
Fetching rake-13.0.6.gem
Fetching method_source-1.0.0.gem
Fetching railties-5.2.6.gem
Fetching marcel-1.0.2.gem
Fetching arel-9.0.0.gem
Fetching activemodel-5.2.6.gem
Fetching activerecord-5.2.6.gem
Fetching activestorage-5.2.6.gem
Fetching globalid-1.1.0.gem
Fetching activejob-5.2.6.gem
Fetching net-smtp-0.3.3.gem
Fetching net-pop-0.1.2.gem
Fetching net-imap-0.3.4.gem
Fetching mini_mime-1.1.2.gem
Fetching mail-2.8.1.gem
Fetching actionmailer-5.2.6.gem
Fetching websocket-extensions-0.1.5.gem
Fetching websocket-driver-0.7.5.gem
Fetching nio4r-2.5.9.gem
Fetching actioncable-5.2.6.gem
Fetching rails-5.2.6.gem
Successfully installed rack-2.2.7
Successfully installed concurrent-ruby-1.2.2
Successfully installed sprockets-4.2.0
Successfully installed thread_safe-0.3.6
Successfully installed tzinfo-1.2.11
Successfully installed minitest-5.18.0
Successfully installed i18n-1.13.0
Successfully installed activesupport-5.2.6
Successfully installed nokogiri-1.15.2-arm64-darwin
Successfully installed crass-1.0.6
Successfully installed loofah-2.21.3
Successfully installed rails-html-sanitizer-1.6.0
Successfully installed rails-dom-testing-2.0.3
Successfully installed rack-test-2.1.0
Successfully installed erubi-1.12.0
Successfully installed builder-3.2.4
Successfully installed actionview-5.2.6
Successfully installed actionpack-5.2.6
Successfully installed sprockets-rails-3.4.2
Successfully installed thor-1.2.2
Successfully installed rake-13.0.6
Successfully installed method_source-1.0.0
Successfully installed railties-5.2.6
Successfully installed marcel-1.0.2
Successfully installed arel-9.0.0
Successfully installed activemodel-5.2.6
Successfully installed activerecord-5.2.6
Successfully installed activestorage-5.2.6
Successfully installed globalid-1.1.0
Successfully installed activejob-5.2.6
Successfully installed net-smtp-0.3.3
Successfully installed net-pop-0.1.2
Successfully installed net-imap-0.3.4
Successfully installed mini_mime-1.1.2
Successfully installed mail-2.8.1
Successfully installed actionmailer-5.2.6
Successfully installed websocket-extensions-0.1.5
Building native extensions. This could take a while...
Successfully installed websocket-driver-0.7.5
Building native extensions. This could take a while...
Successfully installed nio4r-2.5.9
Successfully installed actioncable-5.2.6
Successfully installed rails-5.2.6
Parsing documentation for rack-2.2.7
Installing ri documentation for rack-2.2.7
Parsing documentation for concurrent-ruby-1.2.2
Installing ri documentation for concurrent-ruby-1.2.2
Parsing documentation for sprockets-4.2.0
Installing ri documentation for sprockets-4.2.0
Parsing documentation for thread_safe-0.3.6
Installing ri documentation for thread_safe-0.3.6
Parsing documentation for tzinfo-1.2.11
Installing ri documentation for tzinfo-1.2.11
Parsing documentation for minitest-5.18.0
Couldn't find file to include 'README.rdoc' from lib/minitest.rb
Installing ri documentation for minitest-5.18.0
Parsing documentation for i18n-1.13.0
Installing ri documentation for i18n-1.13.0
Parsing documentation for activesupport-5.2.6
Installing ri documentation for activesupport-5.2.6
Parsing documentation for nokogiri-1.15.2-arm64-darwin
Installing ri documentation for nokogiri-1.15.2-arm64-darwin
Parsing documentation for crass-1.0.6
Installing ri documentation for crass-1.0.6
Parsing documentation for loofah-2.21.3
Installing ri documentation for loofah-2.21.3
Parsing documentation for rails-html-sanitizer-1.6.0
Installing ri documentation for rails-html-sanitizer-1.6.0
Parsing documentation for rails-dom-testing-2.0.3
Installing ri documentation for rails-dom-testing-2.0.3
Parsing documentation for rack-test-2.1.0
Installing ri documentation for rack-test-2.1.0
Parsing documentation for erubi-1.12.0
Installing ri documentation for erubi-1.12.0
Parsing documentation for builder-3.2.4
Installing ri documentation for builder-3.2.4
Parsing documentation for actionview-5.2.6
Installing ri documentation for actionview-5.2.6
Parsing documentation for actionpack-5.2.6
Installing ri documentation for actionpack-5.2.6
Parsing documentation for sprockets-rails-3.4.2
Installing ri documentation for sprockets-rails-3.4.2
Parsing documentation for thor-1.2.2
Installing ri documentation for thor-1.2.2
Parsing documentation for rake-13.0.6
Installing ri documentation for rake-13.0.6
Parsing documentation for method_source-1.0.0
Installing ri documentation for method_source-1.0.0
Parsing documentation for railties-5.2.6
Installing ri documentation for railties-5.2.6
Parsing documentation for marcel-1.0.2
Installing ri documentation for marcel-1.0.2
Parsing documentation for arel-9.0.0
Installing ri documentation for arel-9.0.0
Parsing documentation for activemodel-5.2.6
Installing ri documentation for activemodel-5.2.6
Parsing documentation for activerecord-5.2.6
Installing ri documentation for activerecord-5.2.6
Parsing documentation for activestorage-5.2.6
Installing ri documentation for activestorage-5.2.6
Parsing documentation for globalid-1.1.0
Installing ri documentation for globalid-1.1.0
Parsing documentation for activejob-5.2.6
Installing ri documentation for activejob-5.2.6
Parsing documentation for net-smtp-0.3.3
Installing ri documentation for net-smtp-0.3.3
Parsing documentation for net-pop-0.1.2
Installing ri documentation for net-pop-0.1.2
Parsing documentation for net-imap-0.3.4
Installing ri documentation for net-imap-0.3.4
Parsing documentation for mini_mime-1.1.2
Installing ri documentation for mini_mime-1.1.2
Parsing documentation for mail-2.8.1
Installing ri documentation for mail-2.8.1
Parsing documentation for actionmailer-5.2.6
Installing ri documentation for actionmailer-5.2.6
Parsing documentation for websocket-extensions-0.1.5
Installing ri documentation for websocket-extensions-0.1.5
Parsing documentation for websocket-driver-0.7.5
Installing ri documentation for websocket-driver-0.7.5
Parsing documentation for nio4r-2.5.9
Installing ri documentation for nio4r-2.5.9
Parsing documentation for actioncable-5.2.6
Installing ri documentation for actioncable-5.2.6
Parsing documentation for rails-5.2.6
Installing ri documentation for rails-5.2.6
Done installing documentation for rack, concurrent-ruby, sprockets, thread_safe, tzinfo, minitest, i18n, activesupport, nokogiri, crass, loofah, rails-html-sanitizer, rails-dom-testing, rack-test, erubi, builder, actionview, actionpack, sprockets-rails, thor, rake, method_source, railties, marcel, arel, activemodel, activerecord, activestorage, globalid, activejob, net-smtp, net-pop, net-imap, mini_mime, mail, actionmailer, websocket-extensions, websocket-driver, nio4r, actioncable, rails after 13 seconds
41 gems installed
```

で成功するが、

```bash
Successfully installed activesupport-5.2.6
Successfully installed nokogiri-1.15.2-arm64-darwin
Successfully installed crass-1.0.6
Successfully installed loofah-2.21.3
Successfully installed rails-html-sanitizer-1.6.0
Successfully installed rails-dom-testing-2.0.3
Successfully installed rack-test-2.1.0
Successfully installed erubi-1.12.0
Successfully installed builder-3.2.4
Successfully installed actionview-5.2.6
Successfully installed actionpack-5.2.6
```

なので、actionpack が依存している actionview をインストール中に nokogiri をインストールしにいっているっぽい。ということで、actionview 5.2.6 の依存 gem を順に見ていく。

- https://rubygems.org/gems/actionview/versions/5.2.6

![Screenshot 2023-05-28 at 13.15.53.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/34733/3db13ec4-eb0c-baa9-2dcf-d28ebbcf8e13.png)

そしたら rails-html-sanitizer が最近バージョンアップしているようで、

- https://github.com/rails/rails-html-sanitizer/releases/tag/v1.6.0

> - Dependencies have been updated:
>   - Loofah ~>2.21 and Nokogiri ~>1.14 for HTML5 parser support
>   - As a result, required Ruby version is now >= 2.7.0

actionview のバージョン指定も `~> 1.0, >= 1.0.3` なのでこれっぽい。

```bash
gem install rails-html-sanitizer -v 1.5.0
```

他にもすぐ解決する細かいエラーはでたが、とりあえずこれでいけた。