安装libv8出错:ERROR: 无法构建本地扩展宝石。

196

我用Rails创建了一个项目,其中包含:

rails new test_bootstrap.

操作成功。

进入项目目录并添加gems。

gem "therubyracer"
gem "less-rails" #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
gem "twitter-bootstrap-rails"

并运行

bundle install

在此之后,我遇到了这个错误。
Installing libv8 (3.16.14.3)
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

c:/RailsInstaller/Ruby1.9.3/bin/ruby.exe extconf.rb
creating Makefile
지정된 경로를 찾을 수 없습니다.                                                          지정된 경로를 찾을 수
없습니다.                                                          지정된 경로를 찾을 수 없습니다.

c:/RailsInstaller/Ruby1.9.3/lib/ruby/ge
ms/1.9.1/gems/libv8-3.16.14.3/ext/libv8/builder.rb:58:in `setup_python!': libv8 requires
python 2 to be installed in order to build, but it is currently not available (RuntimeErr
or) from c:/RailsInstaller/Ruby1.9.
3/lib/ruby/gems/1.9.1/gems/libv8-3.16.14.3/ext/libv8/builder.rb:42:in `block in build_lib
v8/builder.rb:42:in `block in build_libv8!'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/libv8-3.16.14.3/ext/lib
v8/builder.rb:40:in `chdir'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/libv8-3.16.14.3/ext/lib
v8/builder.rb:40:in `build_libv8!'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/libv8-3.16.14.3/ext/lib
v8/location.rb:24:in `install!'
        from extconf.rb:7:in `<main>'                                                    


Gem files will remain installed in c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/l
ibv8-3.16.14.3 for inspection.
Results logged to c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/libv8-3.16.14.3/ex
t/libv8/gem_make.out
An error occurred while installing libv8 (3.16.14.3), and Bundler cannot
continue.
Make sure that `gem install libv8 -v '3.16.14.3'` succeeds before bundling.

对于一些韩国人,很抱歉地告诉你们,它显示“找不到所选路径”或类似的信息。

我尝试运行这个命令:

gem install libv8 -v '3.16.14.3' 

抛出相同的错误。

你是否已经安装了 Devkit https://github.com/oneclick/rubyinstaller/wiki/Development-Kit? - Amit Thawait
我找到了这个,我需要安装Python并设置路径。现在它可以工作了。希望这个线程能帮助其他人解决问题。http://stackoverflow.com/questions/10812696/problems-with-jslint-v8-ruby-gem-installation-on-windows7-64-bit/10924081#10924081 - Canna
不要看赠送的马嘴 :) 来自Debian关于libv8和Node.js生态系统缺乏安全支持的声明:“Node.js平台是建立在libv8-3.14之上的,它经历了大量的安全问题,但目前项目或安全团队中没有足够感兴趣并愿意花费大量时间来解决这些问题的志愿者...” - jww
18个回答

636

试试这个:

gem install libv8 -v '3.16.14.3' -- --with-system-v8
注意: 因为libv8是therubyracer使用的V8引擎的接口,即使您已经安装了V8,您可能仍需要使用libv8。如果您希望使用自己的V8安装而不是由系统构建,请使用--with-system-v8选项。

如需了解更多信息,请查阅GitHub上关于libv8的文档


8
我使用它。编译没有问题,但在运行“bundle”时仍然存在错误。 - Sucrenoir
11
这部分内容可行,但安装 therubyracer 时仍出现错误。它给了我一个与之前 libv8 出现的类似错误,提示“在捆绑之前确保某些操作成功”,但当我尝试运行“gem install therubyracer”时,出现了“Failed to build gem native extension.” 错误。 - sixty4bit
1
我用上面给出的参数以root身份卸载并重新安装,但不起作用。我的错误是在安装libv8(3.16.14.3)时发生错误,Bundler无法继续。 Bundler :: GemspecError:无法读取位于/home/gitlab/gitlab/vendor/bundle/ruby/2.1.0/cache/libv8-3.16.14.3-x86_64-linux.gem的gem文件。它可能已损坏。我删除了整个缓存文件夹,然后重新运行bundle install --full-index --deployment --without development test postgres aws。现在可以使用了。 - Nick Dong
1
太好了!在我的OS X 10.11上,gem install libv8 -v '3.16.14.3' -- --with-system-v8生效了。 - Jagdeep Singh
我发现它找不到系统v8,所以运行brew install v8有所帮助。请按照这个答案中的说明操作:https://dev59.com/U2Af5IYBdhLWcg3w_G2r#25757171 - redfox05
2
在OS X 10.15上,bundle config build.libv8 --with-system-v8能够正常工作。 - Jagdeep Singh

163

如何解决libv8/therubyracer问题

我遇到了类似的问题,在安装了libv8后,安装therubyracer时出现了错误。以下是我的解决方案:

$ gem install libv8 -v '3.16.14.3' -- --with-system-v8
   
$ bundle install

-- 安装therubyracer时出现错误,请参考以下内容 --

$ gem uninstall libv8

$ brew install v8

$ gem install therubyracer

$ bundle install

-- 安装 libv8 发生错误,请查看 --

$ gem install libv8 -v '3.16.14.3' -- --with-system-v8

4
这个回复提到的步骤最有效,我们需要先安装libv8,然后卸载已有的v8,再在gem安装therubyracer之前安装系统范围内的v8(使用--with-system-v8标志安装libv8) :) - Parthan
1
我认为你不需要第一步。在运行bundle install之前,使用brew安装系统v8。然后手动安装therubyracer gem和libv8 gem。最后运行bundle install。但我还没有尝试过。 - gitb
这对我来说也适用于OSX 10.10.5,使用ruby 2.1.2p95和libv8 3.16.14.11。 - Michael Oakley
3
请记得将“3.16.14.3”更改为bundle尝试运行的版本,否则当您运行bundle install时它将无法工作。 - Jonathon Blok
很遗憾,对我来说没有起作用。在安装libv8(3.16.14.13)时发生了错误,因此Bundler无法继续。 在捆绑之前,请确保gem install libv8 -v '3.16.14.13'成功。 - Amanda Cavallaro
显示剩余2条评论

55

我尝试了上面列出的命令解决安装单个 gem 的问题,这看起来非常好,但对于 bundler 用户,您应该使用 bundle config 命令。

使用

bundle config build.libv8 --with-system-v8 

bundle config build.therubyracer --with-system-v8

配置 Bundler 以在安装特定 gem 时使用参数


8
出于某种原因,我的软件包(bundle)配置需要如下内容才能找到正确的构建(使用下面coding addicted答案中的brew安装):bundle config build.therubyracer --with-v8-dir=/usr/local/opt/v8-315 - iturgeon

50

有了Homebrew,这帮助我解决了这个错误。

brew tap homebrew/versions
brew install v8-315

gem install libv8 -v '3.16.14.13' -- --with-system-v8
gem install therubyracer -- --with-v8-dir=/usr/local/opt/v8-315

bundle install

在rubyracer的Github问题页面中看到。

针对Homebrew的新版本,因为homebrew/versions已经被移除:

brew install v8@3.15

gem install libv8 -v '3.16.14.13' -- --with-system-v8
gem install therubyracer -- --with-v8-dir=/usr/local/opt/v8@3.15

bundle install

感谢 @gorner 的建议


这对我有用,尽管它通过brew安装了一个较旧版本的v8。 - Sanjay
1
每次我切换 Ruby 版本时,这个错误都让我发疯。我认为你可以尝试调整 v8 版本选项和路径来解决它。 - coding addicted
只有这个解决方案适用于我,感谢您的解决方案。 - AMIC MING
@codingaddicted,你有没有解决这个错误一直出现的问题?我为了一个新项目更新了Ruby和Rails,但每次打开旧项目的新终端时都会出现这个错误。虽然rvm应该处理这些版本问题,但我需要每次打开旧项目的新终端时都执行上面的命令。 - iamse7en
据我所记,我必须为每个 Ruby 版本运行这些命令。当我更新 Ruby 时,我看到了这个错误(但是最近几个月我没有做太多的 Ruby,并且我没有尝试不同的版本)。@iamse7en - coding addicted
请参见 GitHub 问题以获取更多信息:https://github.com/cowboyd/libv8/issues/205#issuecomment-200872773 - Eliot Sykes

7
我认为在Windows系统上不需要使用therubyracer宝石。它是一个使用V8引擎的JavaScript运行时,因此正在尝试安装libv8
您可以安全地从Gemfile中删除宝石。
Rails愿意使用任何可用的运行时。execjs,nodejs等都是可能的选项。
微软已经嵌入了JScript运行时,用于在Windows上使用JavaScript,Rails也在使用它。有关更多信息,请参见此处

6

解决该问题的另一种方法是在Gemfile文件中将它们分开。

group :production do
 gem 'libv8', '~> 3.11.8.3'
 gem 'therubyracer', :platform => :ruby
end

然后运行捆绑命令:bundle install --without production


1
这个和@Litmus的答案结合起来效果很好,因为它展示了如何在不破坏Windows上的开发设置的情况下保留配置中的gem(这可能与部署相关)。 - glenatron

4
我无法安装这个宝石,而不是使用它。
--with-system-v8

我曾尝试执行bundle update,结果很顺利。


4

我在Github上找到了这个问题。

假设你已经尝试了上面提到的步骤,通过brew安装了v8-315和v8。

brew unlink v8
brew link --force v8-315
gem install therubyracer -v '0.12.2' -- --with-system-v8

2

我同意这个方案。请将以下代码添加到你的Gemfile文件中:

gem 'libv8', '~>3.16.14.7'


这个解决方案让我使用 bundle install --no-deployment,然后 bundle install 就可以正常工作了。 - simo

1
在我的情况下,我通过在Gemfile中要求'mini_racer','~> 0.2.6'来解决了这个问题。然后运行bundle install命令就可以了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接