如何在 mac 上安装 therubyracer? "gem install therubyracer -v " 不工作

How to install therubyracer on mac? "gem install therubyracer -v " not working

我一直在尝试安装 rubyracer 并尝试了几乎所有我能在 Whosebug 上找到的东西,但它似乎仍然给我同样的错误。我也是 rails 上 ruby 的新手,所以我无法准确浏览错误消息。以下是错误消息:

ERROR:  Error installing therubyracer:
        ERROR: Failed to build gem native extension.

    current directory: /Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19/ext/libv8
/Users/zoepa/.rvm/rubies/ruby-2.7.0/bin/ruby -I /Users/zoepa/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0 -r ./siteconf20200616-2984-xp1oct.rb extconf.rb --with-v8-dir\=/usr/local/opt/v8@3.15
creating Makefile
Applying /Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19/patches/disable-building-tests.patch
Applying /Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19/patches/disable-werror-on-osx.patch
Applying /Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19/patches/disable-xcode-debugging.patch
Applying /Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19/patches/do-not-imply-vfp3-and-armv7.patch
Applying /Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19/patches/do-not-use-MAP_NORESERVE-on-freebsd.patch
Applying /Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19/patches/do-not-use-vfp2.patch
Applying /Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19/patches/fPIC-for-static.patch
Compiling v8 for x64
Using python 2.7.16
Using compiler: c++ (clang version 11.0.0)
Unable to find a compiler officially supported by v8.
It is recommended to use GCC v4.4 or higher
Beginning compilation. This will take some time.
Building v8 with env CXX=c++ LINK=c++  /usr/bin/make x64.release ARFLAGS.target=crs werror=no
GYP_GENERATORS=make \
        build/gyp/gyp --generator-output="out" build/all.gyp \
                      -Ibuild/standalone.gypi --depth=. \
                      -Dv8_target_arch=x64 \
                      -S.x64  -Dv8_enable_backtrace=1 -Dv8_can_use_vfp2_instructions=true -Darm_fpu=vfpv2 -Dv8_can_use_vfp3_instructions=true -Darm_fpu=vfpv3 -Dwerror=''
  CXX(target) /Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19/vendor/v8/out/x64.release/obj.target/preparser_lib/src/allocation.o
warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
In file included from ../src/allocation.cc:33:
../src/utils.h:33:10: fatal error: 'climits' file not found
#include <climits>
         ^~~~~~~~~
1 warning and 1 error generated.
make[1]: *** [/Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19/vendor/v8/out/x64.release/obj.target/preparser_lib/src/allocation.o] Error 1
make: *** [x64.release] Error 2
/Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19/ext/libv8/location.rb:36:in `block in verify_installation!': libv8 did not install properly, expected binary v8 archive '/Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19/vendor/v8/out/x64.release/obj.target/tools/gyp/libv8_base.a'to exist, but it was not found (Libv8::Location::Vendor::ArchiveNotFound)
        from /Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19/ext/libv8/location.rb:35:in `each'
        from /Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19/ext/libv8/location.rb:35:in `verify_installation!'
        from /Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19/ext/libv8/location.rb:26:in `install!'
        from extconf.rb:7:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in /Users/zoepa/.rvm/gems/ruby-2.7.0/gems/libv8-3.16.14.19 for inspection.
Results logged to /Users/zoepa/.rvm/gems/ruby-2.7.0/extensions/x86_64-darwin-19/2.7.0/libv8-3.16.14.19/gem_make.out

如错误信息所述,您使用的是Clang,但您安装的libv8版本过时,仅支持GCC。

请注意,therubyracer 的最新版本是从 2017 年 1 月开始的(看起来自 2013 年初以来没有任何开发),它使用的 libv8 版本也是如此(当然,V8 的版本该版本的 libv8 随附)。它建议使用 2012 年的 GCC 4.4。

我怀疑您是否会得到对这样旧版本的 libv8、V8 或 GCC 的任何支持。另外,请注意 Ruby 2.7 当时还不存在,Ruby 的当前版本在 2017 年初应该是 2.4,在 2013 年初应该是 2.0.

你可能没有v8的系统版本,你可以这样做:

env \
  CXX=clang++ \
  GYPFLAGS=-Dmac_deployment_target=10.9 \
gem install libv8 --version 3.16.14.19

这假设您在 Mac 上有典型的开发人员设置 – 最新 Xcode 安装了命令行工具。

原答案:https://github.com/rubyjs/libv8/issues/282#issuecomment-568538097