Aaron
Aaron

Reputation:

When I try sudo gem install json I get the following error

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

Upvotes: 35

Views: 52704

Answers (10)

Janco
Janco

Reputation: 1140

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.

Upvotes: 2

p01nd3xt3r
p01nd3xt3r

Reputation: 841

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

Upvotes: 3

Javid Jamae
Javid Jamae

Reputation: 8999

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

Upvotes: 22

Alan David Garcia
Alan David Garcia

Reputation: 1533

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.

Upvotes: 0

Jackie Chan
Jackie Chan

Reputation: 2662

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

Upvotes: -1

Diego Freniche
Diego Freniche

Reputation: 5414

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

Upvotes: 13

Aaron
Aaron

Reputation:

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

Upvotes: 4

adu
adu

Reputation: 987

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

Upvotes: 2

Anar Jafarov
Anar Jafarov

Reputation: 7

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 )))

Upvotes: -1

neomorphic
neomorphic

Reputation: 588

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

aptitude install ruby1.8-dev

Upvotes: 10

Related Questions