![](/img/trans.png)
[英]install ruby on rails but got error when starting the server with “rails s”
[英]Ruby on Rails / PostgreSQL - Library not loaded error when starting server
嘿嘿。 如果這個問題已經在另一個線程中得到回答,我提前道歉。 我已經搜索了該網站,但沒有找到答案。 但是,最接近的相關問題是Ruby on Rails / PostgreSQL - Library not Loaded error when started server- libq.5.dylib ,但從未解決,答案對我沒有幫助。
我試圖讓 PostgreSQL 在我的 Mac OSX 10.6.8 上運行。 我已經安裝了 Ruby 1.9.3p392 和 Rails 3.2.13。
由於我有 10.6.8,而 PostGresApp 需要 10.7+,我嘗試使用此處提供的手動安裝程序 (v9.2.3) 安裝 PG: http://www.enterprisedb.com/products-services-training/pgdownload
我首先將它安裝到默認路徑:/Library/PostgreSQL/9.2/。 然后我安裝了 pg gem( PATH=$PATH:/Library/PostgreSQL/9.2/bin
sudo gem install pg
)。 當我嘗試使用rails s
啟動我的 localhost 時,出現以下錯誤。
經過一番閱讀,我猜測可能是路徑錯誤,所以我刪除了 pg gem,然后使用了 postgresql 卸載程序。 然后我在 /users/stewartmccoy/Library/PostgreSQL/9.2/... 中重新安裝了 pg,然后重新安裝了 pg gem。 但我仍然得到同樣的錯誤。
關於如何正確安裝 PostgreSQL 並讓我的 rails 服務器運行的任何想法?
stewart-mccoys-macbook:footy_subs stewartmccoy$ rails s
/Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg.rb:4:in `require': dlopen(/Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle, 9): Library not loaded: libpq.5.dylib (LoadError)
Referenced from: /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle
Reason: image not found - /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg.rb:4:in `<top (required)>'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `require'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `block (2 levels) in require'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:70:in `each'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:70:in `block in require'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:59:in `each'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:59:in `require'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler.rb:132:in `require'
from /Users/stewartmccoy/Code/footy_subs/config/application.rb:13:in `<top (required)>'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:53:in `require'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:53:in `block in <top (required)>'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
stewart-mccoys-macbook:footy_subs stewartmccoy$ gem uninstall pgSuccessfully uninstalled pg-0.14.1
stewart-mccoys-macbook:footy_subs stewartmccoy$ PATH=$PATH:/users/stewartmccoy/Library/PostgreSQL/9.2/bin sudo gem install pg
Password:
Fetching: pg-0.14.1.gem (100%)
Building native extensions. This could take a while...
Successfully installed pg-0.14.1
1 gem installed
Installing ri documentation for pg-0.14.1...
Installing RDoc documentation for pg-0.14.1...
stewart-mccoys-macbook:footy_subs stewartmccoy$ rails s
/Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg.rb:4:in `require': dlopen(/Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle, 9): Library not loaded: libpq.5.dylib (LoadError)
Referenced from: /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle
Reason: image not found - /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg.rb:4:in `<top (required)>'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `require'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `block (2 levels) in require'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:70:in `each'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:70:in `block in require'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:59:in `each'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:59:in `require'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler.rb:132:in `require'
from /Users/stewartmccoy/Code/footy_subs/config/application.rb:13:in `<top (required)>'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:53:in `require'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:53:in `block in <top (required)>'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
錯誤的關鍵部分是:
Library not loaded: libpq.5.dylib (LoadError)
這表明ruby在運行時找不到libpq
。 為了解決這個問題,您應該將DYLD_LIBRARY_PATH
環境變量設置為指向PostgreSQL安裝的lib
目錄,無論是全局還是用於啟動Rails的包裝腳本。 有關更多信息,請參閱此超級用戶問題 。
Pg
gem可以在編譯和安裝期間找到庫,因為pg_config
可執行文件位於PATH
,並使用它來查找libpq
。 它似乎不存儲在運行時使用的路徑,因此您必須自己設置運行時動態鏈接器。
一個簡單的包裝腳本(如果你不想修改你的全局環境)是這樣的:
#!/bin/bash
export DYLD_LIBRARY_PATH=/path/to/pg/lib
exec rails "$@"
“$ @”基本上意味着“將所有參數傳遞給此腳本,就好像它們是直接傳遞給這里一樣”。 它保留了正確的引用,實質上意味着rails命令無法告訴你沒有直接運行它。
類似的stackoverflow答案建議從你的PATH中刪除Postgres.app,卸載pg gem,然后再次安裝pg gem。
(我沒有花時間仔細檢查這是否適用於這個問題,但它解決了我遇到的問題,因為我無法找到它,所以我想傳播解決方案。)
我想把我的兩分錢放在上面的答案上。
有時只需重新安裝pg
gem 就足夠了。
gem uninstall pg && gem install pg
就我而言,我將 postgresql 服務器從9.6
升級到14.x
,並且更改了 dylib 路徑。
/opt/local/lib/postgresql96/libpq.5.dylib
-> /opt/local/lib/postgresql14/libpq.5.dylib
gem 使用pg_config來確定/opt/local/lib/postgresql[version]
文件夾(名為PKGLIBDIR
)的路徑。 要為 gem rake 構建本機庫,通過在 compile 中從pg_config
中獲取動態庫來設置動態庫的路徑。 編譯pg
gem 后,路徑將永遠鏈接。 因此,重新安裝會重新編譯並將路徑重新鏈接到新庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.