【问题标题】:Ruby on Rails / PostgreSQL - Library not loaded error when starting serverRuby on Rails / PostgreSQL - 启动服务器时未加载库错误
【发布时间】:2013-03-19 22:58:41
【问题描述】:

嘿嘿。如果这个问题已经在另一个线程中得到回答,我提前道歉。我已经搜索了该网站,但没有找到答案。但是,最接近的相关问题是Ruby on Rails / PostgreSQL - Library not Loaded error when starting 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 启动我的本地主机时,我收到了以下错误。

经过一番阅读,我猜测可能是路径错误,所以我删除了 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>'

【问题讨论】:

标签: ruby-on-rails macos postgresql pg libpq


【解决方案1】:

错误的关键部分是:

Library not loaded: libpq.5.dylib (LoadError)

这表明 ruby​​ 在运行时找不到libpq。为了解决这个问题,您可能应该将DYLD_LIBRARY_PATH 环境变量设置为指向PostgreSQL 安装的lib 目录,无论是全局还是在用于启动Rails 的包装脚本中。请参阅this superuser question 了解更多信息。

Pg gem 可以在编译和安装期间找到该库,因为pg_config 可执行文件位于PATH 上,它使用它来查找libpq。它似乎没有存储运行时使用的路径,因此您必须自己设置运行时动态链接器。

一个简单的包装脚本(如果您不想修改全局环境)类似于:

#!/bin/bash
export DYLD_LIBRARY_PATH=/path/to/pg/lib 
exec rails "$@"

“$@”基本上意味着“将所有参数传递给这个脚本,就好像它们直接传递到这里一样”。它保留了正确的引用,本质上意味着 rails 命令无法告诉您没有直接运行它。

【讨论】:

  • 谢谢克雷格。是否有我不想全局设置 DYLD_LIBRARY_PATH 环境变量的原因?如果包装脚本是最好的方法,我应该如何保存脚本以及保存在哪里?即具有什么文件名和扩展名以及在什么目录中,所以 Rails 知道在运行时运行脚本?
  • @stewartmccoy Rails 不运行脚本,您使用脚本启动 Rails。像使用rails 命令一样使用它。在大多数情况下,可以全局设置DYLD_LIBRARY_PATH,但唯一不这样做的原因是如果存在库名称冲突。
  • 我尝试搜索 SO / 谷歌搜索如何在全球范围内设置 DYLD_LIBRARY_PATH,但找不到有意义的解决方案。我怎样才能做到这一点?我假设我需要将路径设置为/users/stewartmccoy/Library/PostgreSQL/9.2/,因为那是我安装 pg 的地方?而且,如果我采用这种方法,我是否可以只使用rails s 在 localhost 上查看应用程序?
  • @stewartmccoy 是的,如果你在全局范围内设置它,你可以使用rails 命令而不需要包装器。您想将包含libpq.5.dylib 的目录添加到DYLD_LIBRARY_PATH,它将位于Pg 安装的lib 目录中。为了设置它 - 把它放在你的 .bashrc 或其他任何东西中,很多选项。我不经常使用 Apple 设备,所以我不能更具体地介绍这部分。
  • 对于后面的人,我通过在 VIM 中编辑 .bash_profile 并添加 export DYLD_LIBRARY_PATH=/Users/stewartmccoy/Library/PostgreSQL/9.2/lib:$DYLD_LIBRARY_P‌​‌​ATH,然后在执行 rails s 之前重新启动终端来永久修复此问题。
【解决方案2】:

类似的 stackoverflow 答案建议从您的 PATH 中删除 Postgres.app,卸载 pg gem,然后再次安装 pg gem。

(我没有花时间仔细检查这是否适用于这个问题,但它解决了我遇到的一个问题,我想传播这个解决方案,因为我找不到它。)

【讨论】:

    猜你喜欢
    • 2017-01-23
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多