70

I would appreciate some help in getting gem install therubyracer to work. Here is the error:

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

        /Users/david/.rvm/rubies/ruby-1.9.3-p194/bin/ruby extconf.rb
checking for main() in -lobjc... yes
*** 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.

Provided configuration options:
    --with-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/Users/david/.rvm/rubies/ruby-1.9.3-p194/bin/ruby
    --with-objclib
    --without-objclib
extconf.rb:15:in `<main>': undefined method `include_path' for Libv8:Module (NoMethodError)

Here are some notable steps that I ran before the error. They worked fine:

$ gem install libv8
$ brew install v8

My environment is:

  • Mac OS X Lion 10.7.4
  • ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0] (via rvm)
  • V8 version 3.9.24 (via homebrew)
David J.
  • 31,569
  • 22
  • 122
  • 174
  • Note: I hoped that http://wellconsidered.be/post/18996655760/therubyracer-compile-error-osx-10-7-lion would be of use, but it did not help. – David J. Jun 05 '12 at 22:29

11 Answers11

106

This worked for me:

$ gem uninstall libv8
$ gem install therubyracer

(A big thanks to http://www.ruby-forum.com/topic/4306127)

David J.
  • 31,569
  • 22
  • 122
  • 174
  • 1
    I'm on ruby 1.9.3p194 and this didn't work. "make: g++-4.2: No such file or directory" – jspooner Jun 20 '12 at 16:22
  • I tried installing every combination of the gems but nothing worked. I ended up just copying the gemdir from the ruby1.9.2p125 directory over to the new p194 directory. – jspooner Jun 20 '12 at 18:33
  • @jspooner This is relatively tricky install process for many people. If you feel up to it, post a question like I did, with your configuration settings in detail, what you tried, and so on. – David J. Jun 20 '12 at 20:06
  • :( didn't work for me, this is what i get: ERROR: Error installing therubyracer: ERROR: Failed to build gem native extension. /Users/lionel_lei/.rvm/rubies/ruby-1.9.3-p194/bin/ruby extconf.rb checking for main() in -lobjc... yes creating Makefile make compiling rr.cpp make: clang++: No such file or directory make: *** [rr.o] Error 1 – lionel Oct 03 '12 at 07:24
  • 1
    @lionel Can you post a Gist of exactly what you've tried? Have you tried setting up a new `rbenv` or `rvm` install from scratch and writing down the steps (in a Gist) to isolate the problem? Did you do `brew install v8` first? – David J. Oct 03 '12 at 13:36
  • Hi David, I posted this as a new question so I could accept answer: http://stackoverflow.com/questions/12720909/therubyracer-install-error-i-tried-all-other-so-solutions-and-still-no-luck I have not tried rbenv or brew install v8. I haven't used those before. But I could look into it. Thanks for the hint. – lionel Oct 04 '12 at 05:41
  • I set a version for therubyracer as '0.11.4' and it helped. The default therubyracer version resolved happens to be '0.9.x' which has issues with available versions of 'libv8's. – Priya Ranjan Singh Jul 19 '13 at 11:40
  • for Rails the second step here should be just running "bundle install" after uninstalling libv8. Also make sure to do "gem "therubyracer", :require => 'v8'" in your gemfile – concept47 Jan 24 '14 at 20:33
43
gem uninstall libv8
brew install v8
gem install therubyracer
Ken Mazaika
  • 1,142
  • 10
  • 12
  • Thanks Ken. Took about 3-5 minutes to install. Everything working with therubyracer-0.11.2 which auto-installed libv8-3.11.8.13. – scarver2 Jan 12 '13 at 15:56
40

But, why is this happening, you ask? And why does uninstalling libv8 and reinstalling therubyracer fix the problem?

The answer is at the bottom of the error message (from orig post). Ignore the stuff about

probably lack of necessary libraries and/or headers

This is an incorrect assumption by whoever wrote that error message. At the bottom, you see what Ruby has to say about it:

undefined method `include_path' for Libv8:Module

In my case, I was trying to install therubyracer-0.9.8 with bundle install, and for some reason, it was trying to use my copy of libv8-3.11.8.13, which had been installed at some point, probably as a dependency of some other gem.

I don't know why it was trying to use the newer version, because therubyracer.gemspec contains s.add_dependency "libv8", "~> 3.3.10". And my Gemfile.lock says to use libv8 (3.3.10.2). But alas, that is indeed what was happening.

And it's true that Libv8:Module does not have the method include_path in libv8-3.11.8.13, but it does in libv8-3.3.10.2

So that is why uninstalling all of your versions of libv8 and then re-installing therubyracer works. Because all the versions of libv8 that do not have the method include_path are removed completely, and the libv8 that does have the method include path is reinstalled when you reinstall of therubyracer.

Mars Redwyne
  • 1,267
  • 13
  • 8
  • 2
    Thank you for taking the time to write the explanation for why this works – concept47 Jan 24 '14 at 20:32
  • What if I don't want to delete the newer version of libv8, in case of other projects depending on it? What do I do then? – Magne Nov 28 '14 at 12:16
  • Found it! Checked that my `Gemfile` didn't need a specific therubyracer version. Then I deleted the old `Gemfile.lock` which specified an old therubyracer version, which in turn required the old `libv8` version. (Someone must have committed it to the project previously, and then bundle install tried to use it when installing.) When I ran `bundle install` again, it made a new `Gemfile.lock` with new connections, and hooked the latest `therubyracer` version up to the latest `libv8` version, no problem. – Magne Nov 28 '14 at 12:35
15

Considering none if the above worked for me 100%, I thought I'd post what did (as part of a rails project):

gem uninstall libv8
bundle update therubyracer

This made sure I got the latest therubyracer, and also a more recent version of libV8, and seem to fix the multiple issues I was hitting, from missing libv8.a files, to undefined methods.

Abe Petrillo
  • 2,368
  • 23
  • 34
12

At last I use therubyracer 0.11.0beta5 as a solution.

Using therubyracer (0.11.0beta5)

add following on Gemfile

gem 'therubyracer', '~> 0.11.0beta5'
group :libv8 do
  gem 'libv8', "~> 3.11.8"
end

then bundle install

Mac OSX 10.8 Moutain Lion

Holger Just
  • 52,918
  • 14
  • 115
  • 123
szpapas
  • 305
  • 2
  • 5
  • 1
    I tried this, but: Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension. /Users/lionel_lei/.rvm/rubies/ruby-1.9.3-p194/bin/ruby extconf.rb checking for main() in -lpthread... yes checking for main() in -lobjc... yes creating Makefile make compiling accessor.cc make: clang++: No such file or directory make: *** [accessor.o] Error 1 i am on Rails 3.2.6, ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.1], osx 10.7.4 – lionel Oct 03 '12 at 07:19
10

If you need 0.11.3 and it's failing give this a shot for Mac OS X 10.9...

gem uninstall libv8
brew install v8
gem install libv8 -- --with-system-v8
gem install therubyracer -v '0.11.3' -- --with-system-v8

See this issue for more details.

You probably don't need the -- --with-system-v8 on the last line but I did it just to be safe since I saw it start doing Fetching: libv8-3.11.8.17-x86_64-darwin-13.gem (1%) when I ran the command...

Anyhow, it worked for me when all the other things did not.

cwd
  • 53,018
  • 53
  • 161
  • 198
7

For anyone encountering this issue on Mac OSX 10.8 Mountain Lion when attempting to upgrade their Gemfile with gem 'therubyracer', '0.11.0', just upgrading the system libv8 gem worked for me (no uninstallation of any other gem necessary):

$ gem update libv8
$ bundle install

Edit

If you use Travis-CI (or other CI tools located on other servers, I assume), you will need to explicitly add the libv8 gem to your Gemfile as well:

Gemfile

gem 'libv8', '3.11.8.3'

then bundle install as usual. Just note that libv8 can take a significant amount of time to install and I've noticed that it may end up being the cause of going over Travis CI's timeout limits, causing your build to fail. You can mitigate this slightly be not including development environment gems in your builds:

.travis.yml

# ...
bundler_args: --binstubs=./bundler_stubs --without development

Update

Yep, pretty much all my Travis builds timeout and fail because of this. If anyone knows a way to solve this problem (I would hope "downgrade therubyracer" is a last resort), please leave a comment!

Update 2

This may not work for all apps, but it seems that my Rails 3.2.9 apps didn't actually need therubyracer or libv8 after all. After removing those gems from my Gemfile, I confirmed that my specs passed, pushed again to Travis and it built successfully. So, I guess getting rid of those gems (if you're not sure you actually need them) is at least worth a try.

Update 3

Thanks to Paul Annesley for confirming that if you're on Mac OS X 10.8 Mountain Lion, you don't need therubyracer gem at all since the OS already comes bundled with Apple JavaScriptCore, its own Javascript runner. At the time of the original answer, I was on Snow Leopard and hence needed it.

Community
  • 1
  • 1
Paul Fioravanti
  • 16,423
  • 7
  • 71
  • 122
  • Regarding "Update 2" and not needing `therubyracer` or `libv8`, presumably that means you're either not using CoffeeScript, or using an alternative JS runner. – Paul Annesley Mar 14 '13 at 11:04
  • I do use Coffeescript, for sure. Looking back on it, I believe I made that edit soon after I upgraded OS X from Snow Leopard to Mountain Lion, so perhaps Mountain Lion has its own JS runner that made `therubyracer` unnecessary...? – Paul Fioravanti Mar 14 '13 at 12:10
  • 1
    Ah yes; “Apple JavaScriptCore - Included with Mac OS X” is listed as one of the runtimes on https://github.com/sstephenson/execjs/blob/master/README.md – Paul Annesley Mar 14 '13 at 22:17
  • Thanks very much for confirming that. I've added it to the answer. – Paul Fioravanti Mar 14 '13 at 23:03
  • for some reason, I have to run this command EVERY time I start running one application locally. Can someone explain, why I have to run it every time as opposed to one time. – Som Poddar Mar 24 '14 at 17:04
4

For me, removing the Gemfile.lock file and running bundle install worked it's magic.

Alex Ishida
  • 1,021
  • 8
  • 9
  • 1
    Note that running `bundle update` is effectively the same thing as deleting `Gemfile.lock` then running `bundle install`. – GMA Jul 22 '15 at 10:00
3

OSX 10.8.2, ruby 1.9.3p125

None of the above worked for me... I got tired of trying to find the right gem for my environment, so I just soft linked to the g++ target this things was missing:

sudo ln -s `which g++` /usr/bin/g++-4.2

Not as helpfully for remote deployments, but get the job done on my workstation.

Colby Blair
  • 396
  • 4
  • 15
1

I got a similar issue, but it was also complaining about not finding g++-4.2. I did have XCode command line tools installed, but it was looking for /usr/bin/g++-4.2, I had g++ (which was a symbolic link pointing to llvm-g++-4.2). Anyway, I just created a symbolic link to g++ and tried the bundle install again... it worked!

$ cd /usr/bin

$ sudo ln -s g++ g++-4.2

richardun
  • 693
  • 5
  • 11
0

Had the same error, this worked for me:

  1. From console: gem uninstall libv8

  2. In your Gemfile, add the following:

    gem 'therubyracer', :platforms => :ruby, :require => 'v8'
    gem 'libv8', '~> 3.11.8'  # Update version number as needed
    
  3. From console: bundle install

If you were in the middle of upgrading therubyracer gem, you may want to run bundle update therubyracer after that as well. (Consider specifying a version number)

This was on Mac 10.6 (Snow Leopard).

michaeldwp
  • 431
  • 5
  • 10