[英]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.