Rails console を実行すると Library not loaded: /usr/local/opt/readline/lib/libreadline.7.dylib (LoadError) が発生するようになった

brew upgrade を実行したところ、bin/rails console を実行する際にエラーが発生するようになりました。

  • macOS Mojave 10.14
  • Ruby 2.6.1
  • Rails 5.2.2
  • readline 8.0.0

readline のメジャーバージョンが上がって、ライブラリのパスが変わったのが問題だったようです。

% bin/rails c
Traceback (most recent call last):
        42: from bin/rails:4:in `<main>'
        41: from /.bundle/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `require'
        40: from /path/to/.bundle/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
        39: from /path/to/.bundle/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `block in require'
        38: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
        37: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
        36: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:83:in `register'
        35: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
        34: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
        33: from /path/to/.bundle/gems/railties-5.2.2/lib/rails/commands.rb:18:in `<main>'
        32: from /path/to/.bundle/gems/railties-5.2.2/lib/rails/command.rb:44:in `invoke'
        31: from /path/to/.bundle/gems/railties-5.2.2/lib/rails/command.rb:70:in `find_by_namespace'
        30: from /path/to/.bundle/gems/railties-5.2.2/lib/rails/command/behavior.rb:79:in `lookup'
        29: from /path/to/.bundle/gems/railties-5.2.2/lib/rails/command/behavior.rb:79:in `each'
        28: from /path/to/.bundle/gems/railties-5.2.2/lib/rails/command/behavior.rb:80:in `block in lookup'
        27: from /path/to/.bundle/gems/railties-5.2.2/lib/rails/command/behavior.rb:80:in `each'
        26: from /path/to/.bundle/gems/railties-5.2.2/lib/rails/command/behavior.rb:84:in `block (2 levels) in lookup'
        25: from /path/to/.bundle/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `require'
        24: from /path/to/.bundle/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
        23: from /path/to/.bundle/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `block in require'
        22: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
        21: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
        20: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:83:in `register'
        19: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
        18: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
        17: from /path/to/.bundle/gems/railties-5.2.2/lib/rails/commands/console/console_command.rb:4:in `<main>'
        16: from /path/to/.bundle/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `require'
        15: from /path/to/.bundle/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
        14: from /path/to/.bundle/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `block in require'
        13: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
        12: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
        11: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:83:in `register'
        10: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
         9: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
         8: from /Users/chocoby/.rbenv/versions/2.6.1/lib/ruby/2.6.0/irb/completion.rb:10:in `<main>'
         7: from /path/to/.bundle/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `require'
         6: from /path/to/.bundle/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
         5: from /path/to/.bundle/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `block in require'
         4: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
         3: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
         2: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:83:in `register'
         1: from /path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/path/to/.bundle/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require': dlopen(/Users/chocoby/.rbenv/versions/2.6.1/lib/ruby/2.6.0/x86_64-darwin17/readline.bundle, 9): Library not loaded: /usr/local/opt/readline/lib/libreadline.7.dylib (LoadError)
  Referenced from: /Users/chocoby/.rbenv/versions/2.6.1/lib/ruby/2.6.0/x86_64-darwin17/readline.bundle
  Reason: image not found - /Users/chocoby/.rbenv/versions/2.6.1/lib/ruby/2.6.0/x86_64-darwin17/readline.bundle

調べてみると libreadline.dylib のシンボリックリンクを貼り直すとか、rb-readline gem をインストールするなどの情報がありましたが、 素直に Ruby を再インストールすることで解決できました。

rbenv で Ruby を再インストールする (自分の環境は 2.6.1):

$ rbenv install 2.6.1
rbenv: /Users/chocoby/.rbenv/versions/2.6.1 already exists
continue with installation? (y/N)
ruby-build: use openssl from homebrew
Downloading ruby-2.6.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.1.tar.bz2
Installing ruby-2.6.1...
ruby-build: use readline from homebrew
Installed ruby-2.6.1 to /Users/chocoby/.rbenv/versions/2.6.1

一度 .bundle ディレクトリを全削除 (環境によっては vendor/bundle ディレクトリなど) し、Gem を再インストールする:

% rm -rf .bundle
% bundle install

再度 bin/rails console を実行したところ、Rails console を起動できました。

% bin/rails c
Loading development environment (Rails 5.2.2)
[1] pry(main)>

© 2023 暇人じゃない. All rights reserved.