rails 安装出错

「环境配置」gem rails -v 5.0.0 安装失败

@Daniel 蛋牛

错误信息
运行 gem rails后出现 nogogiri 报错
Building native extensions. This could take a while...
ERROR: Error installing rails:
ERROR: Failed to build gem native extension.
current directory: /Users/comet/.rvm/gems/ruby-2.3.1/gems/nokogiri-1.7.0.1/ext/nokogiri
/Users/comet/.rvm/rubies/ruby-2.3.1/bin/ruby -r ./siteconf20170214-8282-54id8f.rb extconf.rb

或直接出现openssl 错误
ERROR: While executing gem ... (Gem::Exception)
Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources

系统环境与问题症状
mac OS X 系统升级至:10.11.6和 10.12 后提示 failed to build gem native extension** 显示open-ssl 匹配问题**或提示 nokogiri 安装不成功: gem install nokogiri 提示 failed to build gem native extension

Diagnose
rvm osx-ssl-certs status all确认没有输出匹配信息:/usr/local/etc/openssl/cert.pem: Up to date.

Solution 解决方法

解决链接给 https://gist.github.com/cndaniel/d202b9de7f19d9e788bd32012cfc8c45

  1. 确认 Xcode升级后 agree 了用户协议(新学员一般刚刚按教材来的,可跳过该步骤)
  2. 终端运行'rvm osx-ssl-certs status all' 确认没有输出匹配信息:/usr/local/etc/openssl/cert.pem: Up to date.
  3. 确认brew正常: 终端运行brew update, brew doctor,没有报错。
  4. 确保 rmv 知道 rmv 走 autolib, 运行rvm autolibs homebrew(无反应是正常情况)
  5. 重设ssl环境变量: 依次终端输入运行:(无反应是正常情况)

export LDFLAGS=-L/usr/local/opt/openssl/lib

export CPPFLAGS=-I/usr/local/opt/openssl/include

export PKG_CONFIG_PATH=/usr/local/opt/openssl/lib/pkgconfig

. 6.重装ruby:

rvm reinstall 2.3.1 --autolibs=homebrew

以上运行完成后,再次检查 ssl 证书环境:
rvm osx-ssl-certs status all
出现
Certificates for /usr/local/etc/openssl/cert.pem: Up to date.
即表示问题解决,可以直接安装rails

Others
如果还是没有解决可以试着在step3 插入一步重装ssl :brew install --forece openssl
如果继续提醒nokogiri报错可以重装nokogiri后,再安装rails:gem install nokogiri -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/libxml2 —use-system-libraries

或直接用 rbev 替换 rvm 管理 ruby 版本:
https://gorails.com/setup/osx/10.12-sierra

Reference
http://stackoverflow.com/questions/41946652/setup-rvm-ruby-on-mac-os-x-sierra-unable-to-require-openssl
https://github.com/sparklemotion/nokogiri/issues/1486
http://stackoverflow.com/a/41491487/7081592

Comments

comments powered by Disqus