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

Yar*_*iuk 2 ruby gem ruby-on-rails bundler

当我安装gem时,<insert gem name>它会失败并显示错误ERROR: Failed to build gem native extension.

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

注意:这是一个质量检查类型的问题,即请参阅以下我提出的解决方案或加入讨论。

Yar*_*iuk 5

此错误经常发生在新创建的服务器上,因此,自然,该错误意味着缺少某些依赖项。

“扭曲”是,错误本身通常告诉我们缺少哪些依赖项,但我们浏览描述并尝试谷歌搜索“无法构建 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
Run Code Online (Sandbox Code Playgroud)

检查错误后,您可以看到缺少什么:

/usr/bin/ld: cannot find -lgmp
Run Code Online (Sandbox Code Playgroud)

那么-lgmp是什么?让我描述一下我在寻找过程中的尝试和错误之路。经过一番谷歌搜索后,我发现linlgmp代表库,而 GMP 是一个尚未安装在我的机器上的 C 库。我的下一个谷歌查询是“安装 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
Run Code Online (Sandbox Code Playgroud)

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

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


akb*_*bin 5

您可以转到终端并将其记录下来sudo apt-get install libmysqlclient-dev

希望对您有所帮助。