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

19

517

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!

Chaffinch answered 13/12, 2013 at 4:44 Comment(1)
check presense of /usr/lib/ruby/ruby.h file, if it is present, show us error message from gem_make.out.Dish
S
936

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
Seepage answered 13/12, 2013 at 7:52 Comment(15)
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/…Chaffinch
After installing ruby-devel (on Fedora 20), I got the error *** extconf.rb failed ***. Once installing gcc as you suggested, everything worked fine.Dogcart
You can add sudo zypper install ruby-devel for openSUSEThence
This does nothing on Ubuntu. The missing package is ruby2.2-dev (or whichever version you have installed).Phosphorous
@LuísdeSousa Back in 2013 there was no ruby2.2-dev package in Ubuntu, and I bet this answer helped you to solve the problem.Seepage
Nope, this answer did not help solving anything. The right answer is the one by mimoralea.Phosphorous
saved my life, in some application's docs there is only ruby-dev (and not ruby-devel). That is confusing to linux beginners.Starks
On Ubuntu / debian: Run ruby -v to determine your ruby version. Run apt-cache search ruby | grep "[0-9].[0-9]-dev" to locate the corresponding version-specific dev package.Cresida
or sudo apt-get install ruby2.4-dev (check your ruby version[ruby -v], and install correspondingly)Ruby
@mudasobwa Okay, nice :)Ruby
@mudasobwa Bless your heart stranger.Foresight
How about it for Darwin/macOS?Rohr
No need for you to comment then :PRohr
i noticed on debian i had to install gcc and make as wellDiscount
sudo apt-get install ruby`ruby -e 'puts RUBY_VERSION[/\d+\.\d+/]'`-dev This helped me in WSL! Thank you so muchEugenol
P
253

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
Pileate answered 4/10, 2019 at 7:1 Comment(2)
If you need (or want) to switch the chosen Xcode SDK for a single command or script, you can make use of the DEVELOPER_DIR environment variable as documented in xcode-select(1). For example: DEVELOPER_DIR=/Library/Developer/CommandLineTools/ bundle installGreegree
The solution worked but switching it this way breaks some functionality. xcodebuild for example stops working.Jaenicke
R
81

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

Remove answered 6/10, 2014 at 21:53 Comment(5)
Or, if you're using 2.2 ... sudo apt-get install ruby2.2-devIngunna
@patrick-davey E: Unable to locate package ruby2.2-dev E: Couldn't find any package by regex 'ruby2.2-dev'Wooton
@dson probably you use an old Ubuntu version. Try upgrading.Brietta
I got the same error as well, and I'm using a fully updated version of Linux Mint. I don't think the OS verison is the issue.Chinchin
If you're using an old version of Ubuntu such as Trusty (14.04), add the brightbox PPA for Ruby: sudo apt-add-repository -y ppa:brightbox/ruby-ng && sudo apt-get update && sudo apt-get install -y ruby2.2-devUteutensil
B
24

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
Beshore answered 9/3, 2016 at 15:16 Comment(0)
M
21

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
Mcandrew answered 8/3, 2018 at 16:42 Comment(2)
This was the correct answer for me. I had no Ruby Development Headers. Once I installed xCode the original command I tried running worked. Thanks!Wilberwilberforce
On OSX with Mojave I had to run that as: su - $USER -c 'xcode-select --install'Jule
H
14

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
Hustings answered 11/11, 2016 at 17:49 Comment(3)
This segfaults for me: /cygdrive/z/.gem/ruby/extensions/x86-cygwin/json-1.8.3/json/ext/parser.so: [BUG] Segmentation fault at 0x0001b8Harty
Resolved by doing rm -rf ~/.gem/ and reissuing the command.Harty
ruby-dev on raspberry piPeltier
D
13

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

Dander answered 7/5, 2018 at 22:19 Comment(2)
This worked for me on Ubuntu 18.04. I also had to do a sudo apt-get install make prior to the g++ install to work.Flied
Can confirm installing make and g++ fixed my issue on Ubuntu 18.04, too!Invalidate
R
8

in case you use SUSE

sudo yast2 -i ruby-devel
Refutative answered 16/4, 2014 at 14:36 Comment(1)
I prefer sudo zypper in ruby-devel.Roseannaroseanne
V
8

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
Vaasta answered 16/9, 2019 at 2:43 Comment(4)
Do you have any documentation on CPATH? Setting it is not solving the issue for me. When I checked it’s value in its first place it appeared undefined.Rohr
gcc.gnu.org/onlinedocs/cpp/Environment-Variables.html - you may need to find a different environment variable depending on the language/compiler you’re using. It’s normal that it would be unset by defaultVaasta
And, in case you haven't seen it elsewhere, there can't be spaces in the Xcode path/nameAmbler
From all the answers here, this the only solution that worked for me. I am on Mojave 10.14.6 and XCode 11.3.1. Thank you so much, took me 1 day to resolve this :/Aimo
C
5

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

dnf install ruby-devel
Costumer answered 28/1, 2016 at 17:44 Comment(0)
L
5

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.

Libertinage answered 24/3, 2020 at 13:18 Comment(0)
C
3

You may need to install gcc after install ruby-devel

Complex answered 2/2, 2017 at 16:47 Comment(0)
D
1

Xcode -> Preferences -> Locations

change Command Line Tools to Xcode 11.2.1

Dalmatic answered 4/3, 2020 at 6:16 Comment(0)
S
0

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
Swag answered 5/10, 2019 at 21:0 Comment(0)
B
0

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.

Broadfaced answered 20/3, 2020 at 12:2 Comment(0)
C
0

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.

Cruel answered 7/10, 2020 at 17:11 Comment(0)
P
0

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.

Pot answered 18/11, 2020 at 14:8 Comment(0)
P
0

macOS RubyMine Gem installation failure

My problem with this error message was when trying to install a Gem via RubyMine. It didn't like that I had changed the global Ruby version with rbenv, so I fixed it by changing back to the system default Ruby version with:

rbenv global system

and restarted RubyMine.

Phaedra answered 31/10, 2022 at 13:4 Comment(0)
D
-2
sudo apt-get --reinstall install ruby

try it for ubuntu 16.04

Dealing answered 25/10, 2019 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.