错误:尝试下载 Rubocop 时无法构建 gem 原生扩展

错误:尝试下载 Rubocop 时无法构建 gem 原生扩展

当我尝试“sudo gem install rubocop”时,我得到了......

Building native extensions.  This could take a while...
ERROR:  Error installing rubocop:
    ERROR: Failed to build gem native extension.

    current directory: /var/lib/gems/2.3.0/gems/rainbow-2.2.1/ext
/usr/bin/ruby2.3 mkrf_conf.rb

current directory: /var/lib/gems/2.3.0/gems/rainbow-2.2.1/ext
/usr/bin/ruby2.3 -rubygems /usr/share/rubygems-integration/all/gems/rake-10.5.0/bin/rake RUBYARCHDIR=/var/lib/gems/2.3.0/extensions/x86_64-linux/2.3.0/rainbow-2.2.1 RUBYLIBDIR=/var/lib/gems/2.3.0/extensions/x86_64-linux/2.3.0/rainbow-2.2.1
/usr/bin/ruby2.3: No such file or directory -- /usr/share/rubygems-integration/all/gems/rake-10.5.0/bin/rake (LoadError)

rake failed, exit code 1

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

因此,我尝试了许多解决方案,特别是来自https://stackoverflow.com/questions/22544754/failed-to-build-gem-native-extension-installing-compass(不同的宝石,同样的错误)

我尝试使用“rvm reinstall --disable-binary 2.2.0”重新安装 RVM,这花了一段时间,但没有解决问题

接下来我尝试了 $ "sudo apt-get install ruby​​-dev",结果是

Reading package lists... Done
Building dependency tree       
Reading state information... Done
ruby-dev is already the newest version (1:2.3.0+1).
The following packages were automatically installed and are no longer required:
  dkms linux-headers-4.4.0-53 linux-headers-4.4.0-53-generic
  linux-image-4.4.0-53-generic linux-image-extra-4.4.0-53-generic
  python-appindicator python-gconf python-gi python-glade2
  python-gobject python-pexpect python-ptyprocess python-xdg
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 40 not upgraded.

我也尝试过“gem update --system”,结果我得到了......

Latest version currently installed. Aborting.

任何建议都将不胜感激,因为我是一个完全的菜鸟而且我陷入了困境。

答案1

重复sudo gem install rake做一遍sudo gem install rubocop,它就会起作用。

似乎安装的 rake 版本正确与否并不重要。我rake --version已报告此问题10.5.0,但仍然收到与您相同的错误消息。

查看错误消息,它正在寻找rake特定路径,而不仅仅是我的任何地方PATH

/usr/bin/ruby2.3: No such file or directory -- /usr/share/rubygems-integration/all/gems/rake-10.5.0/bin/rake (LoadError)

which rake报告/usr/local/bin/rake,不是/usr/share/rubygems-integration/all/gems/rake-10.5.0/bin/rake

事实上,正如错误消息所说,没有文件/usr/share/rubygems-integration/all/gems/rake-10.5.0/bin/rake

rake这可能与被apt-get installed 而不是ed有关gem installsudo gem install rake创建该文件。

答案2

我能够解决这个问题

sudo mkdir -p /usr/share/rubygems-integration/all/gems/rake-10.5.0/bin
sudo ln -s /usr/bin/rake /usr/share/rubygems-integration/all/gems/rake-10.5.0/bin/

GitHub 上的此提交

答案3

当我在 2017 年回答这个问题时,我对 @SeanHammond 的解决方案持怀疑态度,因为 rake gem 似乎已经安装好了。事实证明,这是通过 Debian 的 rake 包和他们的 ruby​​gems 集成系统进行的。根据 Hammond 先生的说法,今天我再次安装了 rake gem,因为它覆盖了 Debian 的版本,从而避免了底层错误。好吧,首先它说:

martind@balance:~$ sudo gem2.1 install --no-ri --no-rdoc rake
Fetching: rake-12.3.1.gem (100%)
ERROR:  While executing gem ... (TypeError)
    no implicit conversion of nil into String
martind@balance:~$ 

在我申请之后上游修复对于错误报告,即:

rake's executable "rake" conflicts with an unknown executable
Overwrite the executable? [yN]  n
ERROR:  Error installing rake:
    "rake" from rake conflicts with installed executable from 
martind@balance:~$ 

... 这让我成功了:

martind@balance:~$ sudo gem2.1 install --no-ri --no-rdoc --format-executable rake
Successfully installed rake-12.3.1
1 gem installed
martind@balance:~$ 

这足以让 ruby​​gems 找到更新的二进制文件:

martind@balance:~$ ruby2.1 -we 'puts(Gem.bin_path("rake", "rake"))'
/var/lib/gems/2.1.0/gems/rake-12.3.1/exe/rake
martind@balance:~$ ls -l /var/lib/gems/2.1.0/gems/rake-12.3.1/exe/rake
-rwxr-xr-x 1 root root 1182 Apr 17 15:03 /var/lib/gems/2.1.0/gems/rake-12.3.1/exe/rake
martind@balance:~$ 

@ShaunJackman 的解决方案对我来说也有效。这可能是 Debian 应该修复它的方法,尽管我不想将文件留在我机器上的目录中,因为 Debian 以后可能会更改这些文件。

一个不太痛苦的解决方法是rake=/usr/bin/rake在我安装我想要的 gem 的环境中设置:

martind@balance:~$ rake=/usr/bin/rake sudo gem2.1 install --no-ri --no-rdoc mediawiki_api
Fetching: unf-0.2.0.beta2.gem (100%)
Building native extensions.  This could take a while...
Successfully installed unf-0.2.0.beta2
Fetching: mediawiki_api-0.7.1.gem (100%)
Successfully installed mediawiki_api-0.7.1
2 gems installed
martind@balance:~$ 

... 正如现在所指出的我的 Debian 错误报告。您的 sudo 可能需要 --preserve-env 来传递该设置。

(在这个答案的早期版本中,我声称“相当于sudo gem install rainbow解决了它”,但事实证明我的问题并不等同。我的问题不是 gem、rainbow 和 rubocop,而是 gem2.1、mediawiki-gateway(或其后继者 mediawiki_api)和 unf。手动安装依赖项首先适用于我的情况的原因是 gem2.1 有一个错误,它倾向于将预发布版本作为依赖项进行安装。我怀疑,从代码而不是散文来看,这个错误是这个,当被要求安装 unf 本身时,它反而正确地选择了一个发布版本,这个版本恰好使用 ruby​​gem 的 ExtConfBuilder 代码路径而不是 RakeBuilder 代码路径。这不会帮助原始海报解决他们的彩虹问题,因为他们没有获得预发布版本。抱歉说了这么多题外话,但也许这会对其他面临类似问题的人有所帮助。)

相关内容