如何在 Ubuntu 16.04 上使用 Rails 安装 Nokogiri

How to install Nokogiri with Rails on Ubuntu 16.04

我正在尝试使用

安装 Rails
sudo gem install rails

Ubuntu 16.04 的新版本。 Ruby 2.3.1p112 已经安装。在安装 Rails 期间,完成获取 Nokogiri 后,出现以下错误:

Fetching: nokogiri-1.8.0.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing rails:
        ERROR: Failed to build gem native extension.

    current directory: /var/lib/gems/2.3.0/gems/nokogiri-1.8.0/ext/nokogiri
/usr/bin/ruby2.3 -r ./siteconf20170608-1635-1vnwqbn.rb extconf.rb
checking if the C compiler accepts ... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/bin/$(RUBY_BASE_NAME)2.3
        --help
        --clean
/usr/lib/ruby/2.3.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
        from /usr/lib/ruby/2.3.0/mkmf.rb:571:in `block in try_compile'
        from /usr/lib/ruby/2.3.0/mkmf.rb:522:in `with_werror'
        from /usr/lib/ruby/2.3.0/mkmf.rb:571:in `try_compile'
        from extconf.rb:138:in `nokogiri_try_compile'
        from extconf.rb:162:in `block in add_cflags'
        from /usr/lib/ruby/2.3.0/mkmf.rb:629:in `with_cflags'
        from extconf.rb:161:in `add_cflags'
        from extconf.rb:407:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /var/lib/gems/2.3.0/extensions/x86_64-linux/2.3.0/nokogiri-1.8.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /var/lib/gems/2.3.0/gems/nokogiri-1.8.0 for inspection.
Results logged to /var/lib/gems/2.3.0/extensions/x86_64-linux/2.3.0/nokogiri-1.8.0/gem_make.out

这是mkmf.log的内容:

"gcc -o conftest -I/usr/include/x86_64-linux-gnu/ruby-2.3.0 -I/usr/include/ruby-2.3.0/ruby/backward -I/usr/include/ruby-2.3.0 -I. -Wdate-time -D_FORTIFY_SOURCE=2   -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fPIC  conftest.c  -L. -L/usr/lib/x86_64-linux-gnu -L. -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fstack-protector -rdynamic -Wl,-export-dynamic     -lruby-2.3  -lpthread -lgmp -ldl -lcrypt -lm   -lc "
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

我不知道出了什么问题。日志没有帮助,我已经尝试安装 libgmp-devruby-devzlib1g-devliblzma-dev,因为他们被建议给在 Nokogiri gem 期间遇到错误的人安装。

在尝试找到丢失的依赖项一段时间后,我 运行 mkmf.log 中的 gcc 命令以防某些编译错误消息被遗漏。

原来是我没有安装gcc。安装后,我在安装方面取得了一些进展,但遇到了另一个类似的错误。最终安装下面的依赖,成功安装了Rails。

请确保在 运行 之前安装了 gcc, make, zlib1g-dev, sqlite3 gem install rails:

sudo apt-get install gcc make zlib1g-dev sqlite3