LoginSignup
1
1

More than 1 year has passed since last update.

rails s を実行した際に`Gem Load Error is: AddDllDirectory failed for C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/x64-mingw32`が発生した時の解決方法

Posted at

はじめに

Railsで簡単なsnsアプリを作成している際に以下の問題が発生して1日沼にはまったのでそれの過程と、同じようなことが起きても忘れないよう記録として残しておきます。
同じ問題に直面した方々の役に立てれば幸いです。

前提・使用環境

・Windows 10
・Ruby 3.0.2
・Rails 6.1.4.1
・Postgresql 12.8

起きた問題

rails sを実行した際に
Gem Load Error is: AddDllDirectory failed for C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/x64-mingw32
というエラーが発生。エラーは以下の通り。

コマンドプロンプト
C:/Ruby30-x64/lib/ruby/3.0.0/bundler/runtime.rb:69:in `rescue in block (2 levels) in require': There was an error while trying to load the gem 'pg'. (Bundler::GemRequireError)
Gem Load Error is: AddDllDirectory failed for C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/x64-mingw32
Backtrace for gem load error is:
C:/Ruby30-x64/lib/ruby/site_ruby/3.0.0/ruby_installer/runtime/dll_directory.rb:83:in `add_dll_directory_winapi'
C:/Ruby30-x64/lib/ruby/site_ruby/3.0.0/ruby_installer/runtime/dll_directory.rb:50:in `initialize'
C:/Ruby30-x64/lib/ruby/site_ruby/3.0.0/ruby_installer/runtime/singleton.rb:12:in `new'
C:/Ruby30-x64/lib/ruby/site_ruby/3.0.0/ruby_installer/runtime/singleton.rb:12:in `add_dll_directory'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/pg.rb:15:in `block in <main>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/pg.rb:26:in `rescue in <main>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/pg.rb:4:in `<main>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
C:/Ruby30-x64/lib/ruby/3.0.0/bundler/runtime.rb:66:in `block (2 levels) in require'
C:/Ruby30-x64/lib/ruby/3.0.0/bundler/runtime.rb:61:in `each'
C:/Ruby30-x64/lib/ruby/3.0.0/bundler/runtime.rb:61:in `block in require'
C:/Ruby30-x64/lib/ruby/3.0.0/bundler/runtime.rb:50:in `each'
C:/Ruby30-x64/lib/ruby/3.0.0/bundler/runtime.rb:50:in `require'
C:/Ruby30-x64/lib/ruby/3.0.0/bundler.rb:174:in `require'
C:/Users/nisin/ruby_on_rails/sns_app/config/application.rb:7:in `<main>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.1.4.1/lib/active_support/dependencies.rb:332:in `block in require'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.1.4.1/lib/active_support/dependencies.rb:299:in `load_dependency'C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.1.4.1/lib/active_support/dependencies.rb:332:in `require'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/railties-6.1.4.1/lib/rails/commands/server/server_command.rb:138:in `block in perform'
<internal:kernel>:90:in `tap'
Gem Load Error is: AddDllDirectory failed for C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/x64-mingw32

こちらのエラーを調べてみると
環境変数 RUBY_DLL_PATH に <your-path-to-postgresql>/bin
をセットすればいいらしい。

<your-path-to-postgresql>は自分のPostgresqlをインストールした場所です。
インストールする際に保存場所を変更していなかったら通常 
C:/Program Files/PostgreSQL/13
だと思います。(一応自分の保存場所を確認してください)
それではコマンドプロンプトで以下のコマンドを実行。

コマンドプロンプト
set RUBY_DLL_PATH=C:/Program Files/PostgreSQL/13/bin

実行した後に、setコマンドを実行してパスが通っているか確認。

コマンドプロンプト
C:\User\sns_app> set
RUBY_DLL_PATH=C:\ProgramFiles\PostgreSQL\13\bin
SENDGRID_API_KEY=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
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\WINDOWS
TEMP=C:\Users\nisin\AppData\Local\Temp
TMP=C:\Users\nisin\AppData\Local\Temp

沢山パスが出てきますが、目を凝らしてよく見てください。

RUBY_DLL_PATH=C:\ProgramFiles\PostgreSQL\13\bin

があるはずです。これがあればOKです。
よし。これでrails sすれば行けると思いきや、サーバーは立ち上がるが次は以下のエラーが発生。

ActiveRecord::ConnectionNotEstablished (fe_sendauth: no password supplied):

これは単にconfig/database.ymlのdevelopmentのpasswordを書き忘れていただけ。なので、Postgresqlをインストールする際に入力したパスワードを入れるだけ。

config/database.yml
development:
  <<: *default
  database: sns_development
  host: localhost
  username: postgres
  password: インストールする際に決めたパスワードを入力

これでやっとうまくいくかなと思いrails sをしたら次は以下のエラーが発生。

ActiveRecord::NoDatabaseError

これもただ単に

rails db:create

をしてなかっただけなので、それを実行。そしたらまたまたエラーが。もうやめてほしいww

次は以下のようなエラー

Created database 'sns_development'
fe_sendauth: no password supplied
Couldn't create 'sns_test' database. Please check your configuration.
rails aborted!
ActiveRecord::ConnectionNotEstablished: fe_sendauth: no password supplied

これも調べてみるとconfig/database.ymlのtestのpasswordを入力してなかっただけでした。なのでこちらもPostgresqlをインストールした際に決めたパスワードを入力

test:
  <<: *default
  database: sns_test
  host: localhost
  username: postgres
  password: インストールする際に決めたパスワードを入力

入力し終えたらrails db:createを実行。今度はうまくいった。
そしてrails db:migrateも忘れず実行。
最後にrails sを実行したらやっとアプリが立ち上がりました。

感想

独学でやっているので、こんなにエラーがでると本当に心が折れそうになりますww
けど今回でパスの通し方、開発環境でPostgresqlを使えるようになったので割と有意義な時間になりました。
同じ問題が起きた人が入れば参考にしてください。

参考文献

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1