Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while installing json gem 'mkmf.rb can't find header files for ruby'

For context, it on a remote server which has a firewall. I'm setting up my environment through a proxy. I have ruby 1.8.7. When I try to gem install..

sudo gem install --http-proxy <host address>:<port> json 

I get the following error:

Building native extensions.  This could take a while... ERROR:  Error installing json:         ERROR: Failed to build gem native extension.  /usr/bin/ruby extconf.rb mkmf.rb can't find header files for ruby at /usr/lib/ruby/ruby.h  Gem files will remain installed in /usr/lib64/ruby/gems/1.8/gems/json-1.8.1 for inspection. Results logged to /usr/lib64/ruby/gems/1.8/gems/json-1.8.1/ext/json/ext/generator/gem_make.out 

Since I was unsure what the problem is, I googled and found these

  • gem install: Failed to build gem native extension (can't find header files) - the instructions here seem to be specific to the gem being installed.

  • How to install json gem - Failed to build gem native extension This seems to be slightly different error.

Any hints? Thanks!

like image 369
Navneet Avatar asked Dec 13 '13 04:12

Navneet


People also ask

Where are the Ruby SDK header files on Xcode 11 / Catalina?

I prefer sudo zypper in ruby-devel. On Xcode 11 / macOS Catalina, the header files are no longer in the old location and the old /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg file is no longer available. Instead, the headers are now installed to the /usr/include directory of the current SDK path:

Where can I find the Ruby SDK in Xcode?

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0 However, Xcode 11 installs a macOS 10.14 SDK within /Library/Developer/CommandLineTools/SDKs/MacOS10.14.sdk.

What do I need to install Ruby on my target machine?

I would suggest you to install ruby-dev ( ruby-devel for rpm-based distros) package onto you target machine. gcc package might be needed as well. Thanks! I meant the answer in that question seemed module-specific. I think instead of ruby-dev its ruby-devel. Here's where I found what I exactly needed - digitalocean.com/community/articles/…

How do I install Ruby on Fedora 21?

In Fedora 21 and up, you simply open a terminal and install the Ruby Development files as root. On Mac 10.14, the header files don't seem to be installed in the correct place. Rather than changing paths like the other fixes, I was able to just run this: Follow the instructions and it resolved this problem for me.


2 Answers

Modern era update, as stated by mimoralea:

In case that you are using ruby 2.0 or 2.2 (thanks @patrick-davey).

sudo apt-get install ruby2.0-dev sudo apt-get install ruby2.2-dev sudo apt-get install ruby2.3-dev 

or, generic way:

sudo apt-get install ruby-dev 

or

sudo apt-get install ruby`ruby -e 'puts RUBY_VERSION[/\d+\.\d+/]'`-dev 

The first link you’ve posted is exactly your case: there is no ruby development environment installed. Development env is needed to compile ruby extensions, which are mostly written in C. Proxy has nothing to do with the problem: everything is downloaded fine, just compilation fails.

I would suggest you to install ruby-dev (ruby-devel for rpm-based distros) package onto you target machine.

gcc package might be needed as well.

Try:

$ sudo apt-get install ruby-dev 

Or, for Redhat distro:

$ sudo yum install ruby-devel 

Or, for [open]SuSE:

$ sudo zypper install ruby-devel 
like image 58
Aleksei Matiushkin Avatar answered Sep 20 '22 05:09

Aleksei Matiushkin


For Xcode 11 on macOS 10.14, this can happen even after installing Xcode and installing command-line tools and accepting the license with

sudo xcode-select --install sudo xcodebuild -license accept 

The issue is that Xcode 11 ships the macOS 10.15 SDK which includes headers for ruby2.6, but not for macOS 10.14's ruby2.3. You can verify that this is your problem by running

ruby -rrbconfig -e 'puts RbConfig::CONFIG["rubyhdrdir"]' 

which on macOS 10.14 with Xcode 11 prints the non-existent path

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0 

However, Xcode 11 installs a macOS 10.14 SDK within /Library/Developer/CommandLineTools/SDKs/MacOS10.14.sdk. It isn't necessary to pollute the system directories by installing the old header files as suggested in other answers. Instead, by selecting that SDK, the appropriate ruby2.3 headers will be found:

sudo xcode-select --switch /Library/Developer/CommandLineTools ruby -rrbconfig -e 'puts RbConfig::CONFIG["rubyhdrdir"]' 

This should now correctly print

/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0 

Likewise, gem install should work while that SDK is selected.

To switch back to the current Xcode SDK, use

sudo xcode-select --switch /Applications/Xcode.app 
like image 43
joki Avatar answered Sep 21 '22 05:09

joki