无法构建 gem 本机扩展 - 解决方案

Failed to build gem native extension - solution

当我安装 gem <insert gem name> 时失败并出现错误 ERROR: Failed to build gem native extension.

例如,当我尝试在新服务器上安装 gems json、eventmachine、mysql2 时,它几乎总是失败。

注意:这是一个QA类的问题,即我提出的解决方案请见下文或加入讨论。

这个错误经常发生在新创建的服务器上,所以,这个错误自然意味着缺少一些依赖项。

"twist" 是,错误本身通常会告诉我们缺少哪些依赖项,但我们略过描述并尝试 google 'failed to build gem ...'。最好先检查错误输出。

例如,让我们看一下尝试在新机器上安装 gem 'json' 时的输出:

user@server:~$ gem install json -v '1.8.3'
Fetching: json-1.8.3.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing json:
    ERROR: Failed to build gem native extension.

    .../.rvm/rubies/ruby-2.2.2/bin/ruby -r ./siteconf20151204-22068-1ek4f2f.rb extconf.rb
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
compiling generator.c
linking shared-object json/ext/generator.so
/usr/bin/ld: cannot find -lgmp
collect2: error: ld returned 1 exit status
make: *** [generator.so] Error 1

make failed, exit code 2

Gem files will remain installed in .../.rvm/gems/ruby-2.2.2/gems/json-1.8.3 for inspection.
Results logged to .../.rvm/gems/ruby-2.2.2/extensions/x86_64-linux/2.2.0/json-1.8.3/gem_make.out

检查错误后,您可以看到缺少的内容:

/usr/bin/ld: cannot find -lgmp

那么什么是-lgmp?让我描述一下我在发现它时的反复试验路径。在谷歌搜索了一下之后,我发现 lgmp 中的 l 代表库,而 GMP 是一个 C 库,我的机器上还没有安装。我的下一个 google 查询是 'install gmp ubuntu',这让我安装了 libgmp3-dev,问题就解决了。

现在,让我们看一下在新服务器上安装 mysql2 时的输出:

user@server:~$ gem install mysql2 -v '0.3.20'
Fetching: mysql2-0.3.20.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    .../.rvm/rubies/ruby-2.2.2/bin/ruby -r ./siteconf20151204-9782-1eobqf2.rb extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for mysql_query() in -lmysqlclient... no
-----
libmysqlclient is missing. Trying again with extra runtime libraries...
-----
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
-----
libmysqlclient is missing. You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try again.
-----
*** 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.
# SNIP

同样,仔细观察您可以看到 libmysqlclient is missing,错误甚至提供了最明显的解决方案:You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try again.

我希望这将帮助开发人员更快地解决此类错误,而不必针对每个特定的 gem 故障 google,这通常不会带来任何结果。

你可以去终端写下来sudo apt-get install libmysqlclient-dev

希望对你有所帮助。