2021-06-12 16:53

Rubyインストールで「please install libyaml ...」のエラーが出るときは~/.gemを掃除してみると解決するかもしれない

ruby 3.0.1をインストールしようとしたらエラーが出た。
結論から言うと~/.gem/ruby/3.0.0を削除すると解決した。

以下やったことメモ。

エラー内容:

❯ asdf install ruby 3.0.1
Downloading openssl-1.1.1k.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/892a0875b9872acd04a9fde79b1f943075d5ea162415de3047c327df33fbaee5
Installing openssl-1.1.1k...
Installed openssl-1.1.1k to /Users/okonomi/.asdf/installs/ruby/3.0.1

Downloading ruby-3.0.1.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.1.tar.gz
Installing ruby-3.0.1...
ruby-build: using readline from homebrew

BUILD FAILED (macOS 11.4 using ruby-build 20210423)

Inspect or clean up the working tree at /var/folders/z3/9l1cmgy92175ph5q9dxg9h080000gp/T/ruby-build.20210612170008.31086.JdPjRE
Results logged to /var/folders/z3/9l1cmgy92175ph5q9dxg9h080000gp/T/ruby-build.20210612170008.31086.log

Last 10 log lines:
The Ruby yaml extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Configure options used:
  --prefix=/Users/okonomi/.asdf/installs/ruby/3.0.1
  --with-openssl-dir=/Users/okonomi/.asdf/installs/ruby/3.0.1/openssl
  --enable-shared
  --with-readline-dir=/opt/homebrew/opt/readline
  CC=clang
  LDFLAGS=-L/Users/okonomi/.asdf/installs/ruby/3.0.1/lib
  CPPFLAGS=-I/Users/okonomi/.asdf/installs/ruby/3.0.1/include

ログを見てみる:

❯ tail -n 20 /var/folders/z3/9l1cmgy92175ph5q9dxg9h080000gp/T/ruby-build.20210612170008.31086.log
                                    power_assert 1.2.0
                                    rake 13.0.3
                                    test-unit 3.3.7
                                    rexml 3.2.5
                                    rss 0.2.9
                                    rbs 1.0.4
                                    typeprof 0.12.0
installing bundled gem cache:       /Users/okonomi/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/cache
/Users/okonomi/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/yaml.rb:3: warning: It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
The Ruby yaml extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Configure options used:
  --prefix=/Users/okonomi/.asdf/installs/ruby/3.0.1
  --with-openssl-dir=/Users/okonomi/.asdf/installs/ruby/3.0.1/openssl
  --enable-shared
  --with-readline-dir=/opt/homebrew/opt/readline
  CC=clang
  LDFLAGS=-L/Users/okonomi/.asdf/installs/ruby/3.0.1/lib
  CPPFLAGS=-I/Users/okonomi/.asdf/installs/ruby/3.0.1/include

このエラーを誰が出してるのか色々調べたらruby-buildだった:
https://github.com/rbenv/ruby-build/blob/master/bin/ruby-build#L1138

    failed = %w[openssl readline zlib yaml].reject do |lib|
      begin
        require lib
      rescue LoadError
        $stderr.puts "The Ruby #{lib} extension was not compiled."
      end
    end

インストール後の動作確認で失敗してるらしい。
require 'yaml'がエラーになる?実際そうなるか試してみる。

いったん動作確認を無効化して3.0.1をインストールする。

ruby-buildの定義ファイルを編集してverify_opensslを削除:

❯ vi ~/.asdf/plugins/ruby/ruby-build/share/ruby-build/3.0.1
install_package "openssl-1.1.1k" "https://www.openssl.org/source/openssl-1.1.1k.tar.gz#892a0875b9872acd04a9fde79b1f943075d5ea162415de3047c327df33fbaee5" mac_openssl --if has_broken_mac_openssl
install_package "ruby-3.0.1" "https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.1.tar.gz#369825db2199f6aeef16b408df6a04ebaddb664fb9af0ec8c686b0ce7ab77727" ldflags_dirs enable_shared standard

ruby 3.0.1インストール:

❯ asdf install ruby 3.0.1
Downloading openssl-1.1.1k.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/892a0875b9872acd04a9fde79b1f943075d5ea162415de3047c327df33fbaee5
Installing openssl-1.1.1k...
Installed openssl-1.1.1k to /Users/okonomi/.asdf/installs/ruby/3.0.1

Downloading ruby-3.0.1.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.1.tar.gz
Installing ruby-3.0.1...
ruby-build: using readline from homebrew
Installed ruby-3.0.1 to /Users/okonomi/.asdf/installs/ruby/3.0.1
❯ asdf global ruby 3.0.1
❯ ruby -v
ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [arm64-darwin20]

インストールできた。

require 'yaml'を試す:

❯ irb
<internal:/Users/okonomi/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require': incompatible library version - /Users/okonomi/.gem/ruby/3.0.0/gems/io-console-0.5.9/lib/io/console.bundle (LoadError)
        from <internal:/Users/okonomi/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
        from /Users/okonomi/.gem/ruby/3.0.0/gems/reline-0.2.5/lib/reline.rb:1:in `<top (required)>'
        from <internal:/Users/okonomi/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
        from <internal:/Users/okonomi/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
        from /Users/okonomi/.gem/ruby/3.0.0/gems/irb-1.3.5/lib/irb.rb:13:in `<top (required)>'
        from <internal:/Users/okonomi/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
        from <internal:/Users/okonomi/.asdf/installs/ruby/3.0.1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
        from /Users/okonomi/.gem/ruby/3.0.0/gems/irb-1.3.5/exe/irb:9:in `<top (required)>'
        from /Users/okonomi/.asdf/installs/ruby/3.0.1/bin/irb:23:in `load'
        from /Users/okonomi/.asdf/installs/ruby/3.0.1/bin/irb:23:in `<main>'

irbが起動できない…?

incompatible library versionと言われてるgemをディレクトリごとどかしてみる:

❯ mv ~/.gem/ruby/3.0.0 ~/.gem/ruby/3.0.0_

改めて確認:

❯ irb
irb(main):001:0> require 'yaml'
=> true

irbが起動してrequireも成功した。

ruby-buildの定義ファイルからverify_opensslを削除したのを戻してもう一度3.0.1をインストールしてみる:


❯ vi ~/.asdf/plugins/ruby/ruby-build/share/ruby-build/3.0.1
❯ asdf uninstall ruby 3.0.1
❯ asdf install ruby 3.0.1
Downloading openssl-1.1.1k.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/892a0875b9872acd04a9fde79b1f943075d5ea162415de3047c327df33fbaee5
Installing openssl-1.1.1k...
Installed openssl-1.1.1k to /Users/okonomi/.asdf/installs/ruby/3.0.1

Downloading ruby-3.0.1.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.1.tar.gz
Installing ruby-3.0.1...
ruby-build: using readline from homebrew
Installed ruby-3.0.1 to /Users/okonomi/.asdf/installs/ruby/3.0.1

成功した!

M1 Macの問題かなとかエラーメッセージ通りlibyamlが認識されてないのかとか別のところ調べててだいぶ手こずった。