[json] 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

Any hints? Thanks!

This question is related to json ruby rubygems

The answer is


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

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

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

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

And you get the pattern here...


I also encountered this problem because I install Ruby on Ubuntu via brightbox, and I thought ruby-dev is the trunk of ruby. So I did not install. Install ruby2.3-dev fixes it:

sudo apt-get install ruby2.3-dev

For those who are getting this on Mac OS X you may need to run the following command to install the XCode command-line tools, even if you already have XCode installed:

sudo xcode-select --install

Also you must agree the terms and conditions of XCode by running the following command:

sudo xcodebuild -license

Most voted solution didn't work on my machine (linux mint 18.04). After a careful look, i found that g++ was missing. Solved with

sudo apt-get install g++


I had a similar problem using cygwin to run the following command:

$ gem install rerun

I solved it by installing the following cygwin packages:

  • ruby-devel
  • libffi-devel
  • gcc-core
  • gcc-g++
  • make
  • automake1.15

in case you use SUSE

sudo yast2 -i ruby-devel

Xcode 11 / macOS Catalina

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:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include

Most of this directory can be found by using the output of xcrun --show-sdk-path. And if you add this path to the CPATH environment variable, then build scripts (including those called via bundle) will generally be able to find it.

I resolved this by setting my CPATH in my .zshrc file:

export CPATH="$(xcrun --show-sdk-path)/usr/include"

After opening a new shell (or running source .zshrc), I no longer receive the error message mkmf.rb can't find header files for ruby at /usr/lib/ruby/ruby.h and the rubygems install properly.

Note on Building to Non-macOS Platforms

If you are building to non-macOS platforms, such as iOS/tvOS/watchOS, this change will attempt to include the macOS SDK in those platforms, causing build errors. To resolve, either don't set CPATH environment variable on login, or temporarily set it to blank when running xcodebuild like so:

CPATH="" xcodebuild --some-args

In Fedora 21 and up, you simply open a terminal and install the Ruby Development files as root.

dnf install ruby-devel

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:

open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

Follow the instructions and it resolved this problem for me.


You may need to install gcc after install ruby-devel


Xcode -> Preferences -> Locations

change Command Line Tools to Xcode 11.2.1


BEFORE you follow the tip from Joki's answer (below) and IF :

  • you have MacOS 10.14.6

  • at /Library/Developer/CommandLineTools/SDKs/ you have folders MacOSX.sdk(symbolic), MacOSX10.14.sdk, MacOSX10.15.sdk

  • Move MacOSX10.15.sdk to anywhere (admin privileges needs)

  • Delete symbolic link (admin privileges needs)

  • At /Library/Developer/CommandLineTools/SDKs/ create another symbolic link now to MacOSX10.14.sdk folder using (admin privileges needs)

    sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk MacOSX.sdk

  • Now you can follow Joki's answer

WARNING! If you move MacOSX10.15.sdk folder to /Library/Developer/CommandLineTools/SDKs/ again, the command

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

will show MacOSX10.15.sdk folder like default again, nowadays I dunno how to fix it! My suggestion, compress the folder and put the original folder until fix will be available.


I faced a similar issue on Xcode 12 with macOS 10.15 and cocoapods. Just make sure that the xcode-select command points to the SDK you want to build against. It should build without issues afterwards.


You need to install the entire ruby and not just the minimum package. The correct command to use is:

sudo apt install ruby-full

The following command will also not install a complete ruby:

sudo apt-get install ruby2.3-dev

For Ubuntu 18, after checking log file mentioned while install

Results logged to /var/canvas/vendor/bundle/ruby/2.5.0/extensions/x86_64-linux/2.5.0/nio4r-2.5.2/gem_make.out

with

less /var/canvas/vendor/bundle/ruby/2.5.0/extensions/x86_64-linux/2.5.0/nio4r-2.5.2/gem_make.out

I noticed that make is not found. So installed make by

sudo apt-get install make

everything worked.


sudo apt-get --reinstall install ruby

try it for ubuntu 16.04


Questions with json tag:

Use NSInteger as array index Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) HTTP POST with Json on Body - Flutter/Dart Importing json file in TypeScript json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) Angular 5 Service to read local .json file How to import JSON File into a TypeScript file? Use Async/Await with Axios in React.js Uncaught SyntaxError: Unexpected token u in JSON at position 0 how to remove json object key and value.? JSON parse error: Can not construct instance of java.time.LocalDate: no String-argument constructor/factory method to deserialize from String value How to format JSON in notepad++ No String-argument constructor/factory method to deserialize from String value ('') Returning JSON object as response in Spring Boot TypeError: Object of type 'bytes' is not JSON serializable How to send json data in POST request using C# Passing headers with axios POST request How to convert JSON string into List of Java object? npm notice created a lockfile as package-lock.json. You should commit this file RestClientException: Could not extract response. no suitable HttpMessageConverter found Load json from local file with http.get() in angular 2 Angular: 'Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays' How to loop through a JSON object with typescript (Angular2) How to push JSON object in to array using javascript How to check if a key exists in Json Object and get its value REST API - Use the "Accept: application/json" HTTP Header react router v^4.0.0 Uncaught TypeError: Cannot read property 'location' of undefined ASP.NET Core return JSON with status code python JSON object must be str, bytes or bytearray, not 'dict Writing JSON object to a JSON file with fs.writeFileSync Convert a JSON Object to Buffer and Buffer to JSON Object back How to parse JSON in Kotlin? How to convert FormData (HTML5 object) to JSON console.log(result) returns [object Object]. How do I get result.name? tsconfig.json: Build:No inputs were found in config file Python - How to convert JSON File to Dataframe How to define Typescript Map of key value pair. where key is a number and value is an array of objects Retrofit 2: Get JSON from Response body Refused to execute script, strict MIME type checking is enabled? Decode JSON with unknown structure How to parse a JSON object to a TypeScript Object Deserialize Java 8 LocalDateTime with JacksonMapper Getting an object array from an Angular service Python - Convert a bytes array into JSON format Passing bash variable to jq Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ What is the difference between json.load() and json.loads() functions Import JSON file in React using setTimeout on promise chain Make XmlHttpRequest POST using JSON

Questions with ruby tag:

Uninitialized Constant MessagesController Embed ruby within URL : Middleman Blog Titlecase all entries into a form_for text field Ruby - ignore "exit" in code Empty brackets '[]' appearing when using .where find_spec_for_exe': can't find gem bundler (>= 0.a) (Gem::GemNotFoundException) How to update Ruby Version 2.0.0 to the latest version in Mac OSX Yosemite? How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? How to update Ruby with Homebrew? Ruby: How to convert a string to boolean Can't install gems on OS X "El Capitan" How to resolve "gpg: command not found" error during RVM installation? How to install Ruby 2.1.4 on Ubuntu 14.04 The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256 Cannot install Aptana Studio 3.6 on Windows AWS S3: The bucket you are attempting to access must be addressed using the specified endpoint How to avoid "cannot load such file -- utils/popen" from homebrew on OSX RVM is not a function, selecting rubies with 'rvm use ...' will not work How to solve error "Missing `secret_key_base` for 'production' environment" (Rails 4.1) Check if not nil and not empty in Rails shortcut? ActionController::UnknownFormat Failed to build gem native extension (installing Compass) Rails formatting date converting epoch time with milliseconds to datetime TypeError: no implicit conversion of Symbol into Integer RSpec: how to test if a method was called? Execute a command line binary with Node.js Error while installing json gem 'mkmf.rb can't find header files for ruby' How to downgrade or install an older version of Cocoapods How to select option in drop down using Capybara PG::ConnectionBad - could not connect to server: Connection refused Append key/value pair to hash with << in Ruby How to delete specific characters from a string in Ruby? Create Directory if it doesn't exist with Ruby SSL Error When installing rubygems, Unable to pull data from 'https://rubygems.org/ Rails 4 LIKE query - ActiveRecord adds quotes How to find where gem files are installed Installing RubyGems in Windows Rails 4: assets not loading in production Which Ruby version am I really running? Installing Bootstrap 3 on Rails App Rails 4: List of available datatypes Why do I get a "permission denied" error while installing a gem? How to install a specific version of a ruby gem? Rails 4: before_filter vs. before_action Rails 4 Authenticity Token Testing for empty or nil-value string Ruby class instance variable vs. class variable Determining type of an object in ruby

Questions with rubygems tag:

You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) find_spec_for_exe': can't find gem bundler (>= 0.a) (Gem::GemNotFoundException) How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting You don't have write permissions for the /var/lib/gems/2.3.0 directory Can't install gems on OS X "El Capitan" How to avoid "cannot load such file -- utils/popen" from homebrew on OSX How can I set a proxy server for gem? How to install CocoaPods? Error while installing json gem 'mkmf.rb can't find header files for ruby' How to downgrade or install an older version of Cocoapods How to find where gem files are installed cannot load such file -- bundler/setup (LoadError) Installing RubyGems in Windows How to install a gem or update RubyGems if it fails with a permissions error Failed to build gem native extension — Rails install Update just one gem with bundler bundle install fails with SSL certificate verification error Gem Command not found ruby LoadError: cannot load such file The 'json' native gem requires installed build tools Uninstall all installed gems, in OSX? How do I display Ruby on Rails form validation error messages one at a time? How do I specify local .gem files in my Gemfile? How to remove gem from Ruby on Rails application? rmagick gem install "Can't find Magick-config" List of installed gems? Uninitialized constant ActiveSupport::Dependencies::Mutex (NameError) unable to install pg gem Library not loaded: libmysqlclient.16.dylib error when trying to run 'rails server' on OS X 10.6 with mysql2 gem How can I specify a local gem in my Gemfile? gem install: Failed to build gem native extension (can't find header files) warning: Insecure world writable dir /usr/local/bin in PATH, mode 040777 Error installing mysql2: Failed to build gem native extension How to remove RVM (Ruby Version Manager) from my system no such file to load -- rubygems (LoadError) How to install gem from GitHub source? MySQL Install: ERROR: Failed to build gem native extension 'sudo gem install' or 'gem install' and gem locations How to make --no-ri --no-rdoc the default for gem install? How can I install a local gem?