新手问题 Mac 构架 Ruby 环境,运行 Rails 时出错

chihwoo · June 05, 2018 · Last by IChou replied at June 17, 2018 · 2583 hits

bundle 时出错,出错信息是:

An error occurred while installing nokogiri (1.8.2), and Bundler cannot
continue.
Make sure that `gem install nokogiri -v '1.8.2' --source
'https://rubygems.org/'` succeeds before bundling.

求解。 Rails 版本:Rails 4.2.6 Ruby 版本:ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]

谢谢。

报错提示截取太少了 看不到原因 应该有生成一个 gem_make.out 的文件吧

打开看下里面爆了啥

看上去应该是你的环境缺少某个编译依赖

可能是找不到 libxml2 或者 libxslt

你看看 ls /usr/include/libxslt 有没有,如果没有那就

brew install libxml2 libxslt
gem install nokogiri -v '1.8.2' -- --with-xslt-dir=/usr/local/opt/libxslt --with-xml2-dir=/usr/local/opt/libxml2

然后 bundle

我自己录制了 mac 下 rails 开发环境的配置视频。你可以参考。https://itfun.tv/courses/25 直接 QQ 登录看,不需要注册了。

你直接 gem install nokogiri 会出现更详细的报错信息 然后报错信息里会指向一个页面 去那个页面里有解决方案~

可以看看官方文档,我比较喜欢用 brew 装 libxml2,因为编译时间快。

brew install libxml2
# If installing directly
gem install nokogiri -- --use-system-libraries \
  --with-xml2-include=$(brew --prefix libxml2)/include/libxml2
# If using Bundle
bundle config build.nokogiri --use-system-libraries \
  --with-xml2-include=$(brew --prefix libxml2)/include/libxml2
bundle install

另外如果你用 Rails 4.2 的话,应该尽早升级到 4.2.10 和 Ruby 2.3.7,否则会漏掉不少安全补丁。

Reply to luikore
chihwoo:~ goki$ brew install libxml2 libxslt
Error: libxml2 2.9.4 is already installed
To upgrade to 2.9.7, run `brew upgrade libxml2`
Error: libxslt 1.1.28_1 is already installed
To upgrade to 1.1.32, run `brew upgrade libxslt`

谢谢,貌似没有问题。

Reply to IChou

谢谢回复。 gem_make.out 里的内容如下。 如何看哪里爆了呢?

current directory: /Users/goki/tong_app/vendor/bundle/gems/nokogiri-1.8.2/ext/nokogiri
/Users/goki/.rbenv/versions/2.3.0/bin/ruby -r ./siteconf20180609-10739-7kuq0s.rb extconf.rb --use-system-libraries
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using system libraries.
checking for xmlParseDoc() in libxml/parser.h... yes
checking for xsltParseStylesheetDoc() in libxslt/xslt.h... yes
checking for exsltFuncRegister() in libexslt/exslt.h... yes
checking for xmlHasFeature()... yes
checking for xmlFirstElementChild()... yes
checking for xmlRelaxNGSetParserStructuredErrors()... yes
checking for xmlRelaxNGSetParserStructuredErrors()... yes
checking for xmlRelaxNGSetValidStructuredErrors()... yes
checking for xmlSchemaSetValidStructuredErrors()... yes
checking for xmlSchemaSetParserStructuredErrors()... yes
creating Makefile

current directory: /Users/goki/tong_app/vendor/bundle/gems/nokogiri-1.8.2/ext/nokogiri
make "DESTDIR=" clean

current directory: /Users/goki/tong_app/vendor/bundle/gems/nokogiri-1.8.2/ext/nokogiri
make "DESTDIR="
compiling html_document.c
couldn't understand kern.osversion `16.7.0'
In file included from /Users/goki/.rbenv/versions/2.3.0/include/ruby-2.3.0/ruby.h:33,
                 from ./nokogiri.h:33,
                 from ./html_document.h:4,
                 from html_document.c:1:
/Users/goki/.rbenv/versions/2.3.0/include/ruby-2.3.0/ruby/ruby.h:926: error: wrong number of arguments specified for deprecated attribute
/Users/goki/.rbenv/versions/2.3.0/include/ruby-2.3.0/ruby/ruby.h:1419: error: wrong number of arguments specified for deprecated attribute
/Users/goki/.rbenv/versions/2.3.0/include/ruby-2.3.0/ruby/ruby.h:1428: error: wrong number of arguments specified for deprecated attribute
make: *** [html_document.o] Error 1

make failed, exit code 2
Reply to chihwoo

这个报错,有点少见了,在编译阶段触发的,感觉像是 gcc 的问题呀 在你开始装环境之前 xcode-select --install 执行了么?

这个问题我也没遇到过,所以怎么处理也不是很确定

https://github.com/sparklemotion/nokogiri/issues/1111 这个里面稍微有提到一些

建议尝试:

xcode-select --install
rbenv uninstall 2.3.0
rbenv install 2.3.0
gem install nokogiri -v '1.8.2'

看看能不能解决

装个 node 就行了

Reply to chihwoo

mac 别用 gcc, 把 gcc 卸掉

Reply to IChou
Error: Your Xcode (7.3.1) is too outdated.
Please update to Xcode 9.4 (or delete it).
Xcode can be updated from the App Store.

好像提示我 Xcode 的版本低。。。我正在升级试试看。

Reply to luikore

其实并不确定他是不是装了 gcc 🤪

Reply to jasl

为什么装 node 可以解决这个呢?

Reply to IChou

我确定啊,couldn't understand kern.osversion 就是 gcc 出来的:

https://github.com/gcc-mirror/gcc/blob/master/gcc/config/darwin-driver.c#L80

根本原因可能是看了太古老的教程

Reply to IChou

看错了。。看成了 therubyracer

先执行 gem install nokogiri -v '1.8.2 看看这样的报错信息吧。

Reply to luikore

请教有没有最新的安装教程呢? 人在海外。谢啦

Reply to chihwoo

brew list 看看有没有 gcc 的包,如果有就 brew remove 包名.

如果有 macports, 也用类似的方式删掉 gcc.

然后 gcc -v 看一下,如果显示 Apple LLVM version ... (clang-...) 那就好了 (这是 LLVM 的假 gcc)

如果还有而且你不知道怎么删... 建议还是用 docker 搞吧 https://hub.docker.com/_/ruby/

Reply to chihwoo

《Rails 5 敏捷开发》/ 《Agile Web Development with Rails 5》

https://about.ac/books/agile-rails5/ 国内版本的介绍,论坛里也有不少介绍

或者

《Ruby on Rails 教程(原书第 4 版)》

https://railstutorial-china.org 这本更基础点,而且有电子书

Reply to luikore

好的,我试试看,谢谢!

Reply to IChou

多谢,这就去搜一搜。

最新教程看这里,各种系统,各种版本都有。https://gorails.com/setup/osx/10.13-high-sierra

Reply to chunlea

十分感谢。

谢谢各位前辈的指导,我都尝试了但是还是没能解决。。。555 我又重新创建了一个 app,可是进不去该目录,代码如下:

chihwoo:// goki$ rails new tweet_app
      create  
/Users/goki/.rbenv/versions/2.3.0/lib/ruby/2.3.0/fileutils.rb:253:in `mkdir': Permission denied @ dir_s_mkdir - /tweet_app (Errno::EACCES)
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/2.3.0/fileutils.rb:253:in `fu_mkdir'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/2.3.0/fileutils.rb:227:in `block (2 levels) in mkdir_p'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/2.3.0/fileutils.rb:225:in `reverse_each'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/2.3.0/fileutils.rb:225:in `block in mkdir_p'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/2.3.0/fileutils.rb:211:in `each'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/2.3.0/fileutils.rb:211:in `mkdir_p'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/actions/empty_directory.rb:50:in `block in invoke!'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/actions/empty_directory.rb:116:in `invoke_with_conflict_check'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/actions/empty_directory.rb:49:in `invoke!'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/actions.rb:94:in `action'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/actions/empty_directory.rb:14:in `empty_directory'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-4.2.6/lib/rails/generators/app_base.rb:138:in `create_root'
    from (eval):1:in `create_root'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `block in invoke_all'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `each'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `map'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `invoke_all'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/group.rb:232:in `dispatch'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-4.2.6/lib/rails/commands/application.rb:17:in `<top (required)>'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-4.2.6/lib/rails/cli.rb:14:in `<top (required)>'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    from /Users/goki/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-4.2.6/bin/rails:9:in `<top (required)>'
    from /Users/goki/.rbenv/versions/2.3.0/bin/rails:23:in `load'
    from /Users/goki/.rbenv/versions/2.3.0/bin/rails:23:in `<main>'
chihwoo:// goki$ cd tweet_app
-bash: cd: tweet_app: No such file or directory
chihwoo:// goki$ 

请问是什么原因呢?

Reply to chihwoo

你没有当前目录的权限啊

Reply to IChou

应该怎么解决呢 T_T

Reply to chihwoo

cd 到一个你有权限的目录去执行,或者修改当前目录的权限

You need to Sign in before reply, if you don't have an account, please Sign up first.