34

I need to install the JSON gem to start my app but I get the error below whenever I try to install the json gem. Can anyone help me out. I'm using rails 2.2.2 and gems 1.3.1.

Arions-macbook-pro:.ssh arion$ sudo gem install json
Password:
Sorry, try again.
Password:
Building native extensions.  This could take a while...
ERROR:  Error installing json:
    ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb install json
can't find header files for ruby.


Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/json-1.1.4 for inspection.
Results logged to /Library/Ruby/Gems/1.8/gems/json-1.1.4/ext/json/ext/parser/gem_make.out
Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
  • Be careful installing/changing the Apple-installed Ruby. Unless you're sure you know what you're doing, you're better off installing RVM or rbenv, and letting it manage the sandboxed Ruby. – the Tin Man Nov 12 '12 at 21:51
  • 2
    If installing xcode command line tools doesn't fix the issue, and `make` can't find `gcc` see this question: http://stackoverflow.com/q/12256616/637283 – Mike Lyons Nov 01 '13 at 23:37
  • 4
    Have you installed ruby-dev ? – LB40 Apr 17 '09 at 18:02
  • How do you install ruby-dev on a mac? is it a port or a gem or something like that? –  Apr 17 '09 at 18:15

10 Answers10

22

With Mavericks, you can solve this problem by installing the Xcode Command Line Tools from the command line like this:

xcode-select --install

Update: Don't forget to accept the license agreement after you install or update:

sudo xcodebuild -license

Javid Jamae
  • 8,741
  • 4
  • 47
  • 62
13

If you have this problem:

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

and you are using a Mac with OS X (tested with 10.8.1 Mountain Lion), you need to download Xcode from the Mac App Store (or from developer.apple.com if you have a dev account - it's free BTW and you can download a DMG installer)

The latest Xcode versions (I'm using Xcode 4.4.1) does NOT include command line tools. Now you need to install this additional package inside Xcode. Once done, your problem is fixed.

To do that, open Xcode, go to Preferences > Downloads > Components. Install "Command Line Tools". You're done. Now you can install the json Ruby gem.

enter image description here

Diego Freniche
  • 5,225
  • 3
  • 32
  • 45
10

You are missing the ruby development headers. To install these on an OS like ubuntu you should use:

aptitude install ruby1.8-dev
neomorphic
  • 588
  • 5
  • 15
  • Im using a mac... do I still just need to install ruby-dev? –  Apr 17 '09 at 18:12
  • 6
    -1 `aptitude` won't help since it's not installed on Mac OS X. – the Tin Man Nov 12 '12 at 21:57
  • 7
    The question is obviously about Mac OS X, so, your answer is not relevant. – nikolay May 24 '13 at 02:41
  • After installing ruby-dev for my current version (the version cited in your json-install error), the json installation succeeded for me. $ sudo apt-get install ruby1.9.1-dev $ sudo gem install json -v '1.8.1' – OscuroAA Feb 09 '15 at 03:55
  • This is the correct answer. Guys seriously, it's just an example for a Debian-like OS, simply adapt the command to your system (e.g. `yum install ruby-devel` or whatever). – Sylvain B Jan 05 '16 at 13:10
4

The ruby headers don’t come installed with the base ruby install with Mac OS X. These can been found on Mac OS X Install Disc 2 by installing the XCode Tools.

Once installed, all was well!

UPDATE As of at least OS X 10.7.3 this is no longer a problem, it "just works"

MacBook-Pro:tmp$ sudo gem install json
Password:
Building native extensions.  This could take a while...
Successfully installed json-1.6.5
1 gem installed
Installing ri documentation for json-1.6.5...
Installing RDoc documentation for json-1.6.5...
MacBook-Pro:tmp$ uname -a
Jay
  • 19,649
  • 38
  • 121
  • 184
3

Installing Xcode command line tools... should fix this issue.

Keith Smiley
  • 61,481
  • 12
  • 97
  • 110
p01nd3xt3r
  • 821
  • 1
  • 9
  • 19
2

A simple google search reveals this:

If you get the can't find header files for ruby error message when trying to build an extension or a gem, it means that Ruby cannot locate its header files.

Header files are not delivered by default with Mac OS X, you need to install the Xcode Tools package after the installation. You can find it in the Optional Installs / Xcode Tools directory on the Leopard DVD.

TreeTree
  • 3,200
  • 3
  • 27
  • 39
Janco
  • 1,130
  • 1
  • 8
  • 15
2

It turns out certain gems require some ruby header files to install properly. These header files do not ship with Lion (OSX 10.7).

It also turns out Xcode 4 (latest release) also does not install these header files. I tried this.

Solution - Assuming you are on Mac OS X (you appear to be based on the terminal output). To get it working I installed gcc with some developer tools from the following page

You should be able to install the gems now, either with gem install or bundle install.

Overhead If you are worried about the other junk this comes with, here is a summary.

  • gcc - useful
  • make - useful
  • clang - useful for ObjectiveC
  • Developer CLI Tools (purge, etc) - useful?
  • DevSDK (headers, etc) - what you need
adu
  • 947
  • 1
  • 8
  • 15
0

If you use homebrew, run brew doctor. You might find out that the required gcc libraries for compilation are outdated and/or missing.

Warning: No compiler found in /usr/bin!
Warning: Your Xcode (4.2) is outdated

checking for gcc... /Developer/usr/bin/llvm-gcc
checking whether the C compiler works... no
configure: error: in `/private/tmp/freexl-apNF/freexl-1.0.0d':
configure: error: C compiler cannot create executables

For Lion and Mountain Lion users, Apple now provides an official Command Line Tools for Xcode package that you can install without needing to install Xcode itself! You can download it from Apple's developer site https://developer.apple.com/downloads/index.action. If you still need gcc-4.2, and you use Homebrew, you can install it using the apple-gcc42 package from homebrew/dupes.

Alan David Garcia
  • 1,523
  • 2
  • 14
  • 23
-1

Here is my solution:

rvm gem install json -v=xxx

My setup:

OSX Lion
ruby-1.9.3
rvm
gcc-4.2 #=> honestly haven't seen that it's been used
Jackie Chan
  • 2,654
  • 6
  • 35
  • 70
-1

I had the same problem.

Here is my adventures (hope it will be useful):

To fix it I've done many manipulations :-)

I'm using Fedora Core. I've fixed it this way:

I've installed rails-devel:

sudo yum install rails-devel

tried again:

sudo gem install rails

but I've got another problem:

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

    /usr/bin/ruby extconf.rb creating Makefile

make gcc -I. -I/usr/lib/ruby/1.8/i386-linux -I/usr/lib/ruby/1.8/i386-linux -I. -D_FILE_OFFSET_BITS=64 -fPIC -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -fno-strict-aliasing -fPIC -O3 -Wall -c parser.c make: gcc: Command not found

So I've installed GCC:

sudo yum install gcc

tried again:

sudo gem install rails

Now I've got another problem ))) :

Installing ri documentation for json-1.6.3... ERROR: While generating documentation for json-1.6.3 ... MESSAGE: no such file to load -- irb/slex ... RDOC args: --ri --op /usr/lib/ruby/gems/1.8/doc/json-1.6.3/ri --title JSON implemention for Ruby --main README.rdoc ext/json/ext ext lib README.rdoc --title json-1.6.3 Documentation --quiet

So installed ruby-irb:

sudo yum install ruby-irb

tried again:

sudo gem install rails

Successfully installed rails-3.1.3

That's all )))