macOS, Rails: "Failed to build gem native extension"
Asked Answered
R

14

38

I'm stuck trying to install rails on my mac. I have OS X 10.6.8 and I have confirmed that I have Ruby, version 1.8.7

I ran sudo gem update and sudo gem update --system to get the latest versions of the software.

However, when I run sudo gem install rails I get this error:

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

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb
mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ruby.h


Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/json-1.6.3 for inspection.
Results logged to /Library/Ruby/Gems/1.8/gems/json-1.6.3/ext/json/ext/parser/gem_make.out
Reine answered 5/12, 2011 at 17:26 Comment(0)
G
48

If you have XCode 4 or later you will need to open it and go to Preferences -> Downloads -> Components and install the Command Line tools as they aren't installed by default. Couldn't install Rails until this happened.

Geometry answered 1/9, 2012 at 17:40 Comment(2)
Thanks Rob! That was the missing piece of the puzzle!Struthious
I did need to install command line tools, but this solution did not work for me (Command Line tools wasn't listed in components). This alternate solution worked for meFranko
M
41

Im using osx 10.10. You can download from the command-line

xcode-select --install
Millisent answered 20/1, 2015 at 2:41 Comment(2)
This was the correct answer for me. Tried the one above and I didn't see the CLI tools in the list.Evslin
This works for mesmo with Mac OS X 10.11.5 - El CapitanJulianajuliane
S
8

Not sure what it needs to compile, but OSX can't compile any native ruby extensions at all unless the Apple developer tools are installed. On 10.7 Lion you can download it free from the app store, or Download it here for 10.6: http://developer.apple.com/xcode/index.php

It may also be on your OSX install discs, though probably much more out of date.

Subedit answered 5/12, 2011 at 18:12 Comment(1)
that worked - thanks very much. Brand new to developing on mac so apologies if this question was rudimentaryReine
B
6

Just a follow up ...

it may be that you are on a mac and rails cannot find the right compiler for c headers.

just install xcode from apps store / homebrew or go to terminal ...

$ xcode-select --install

complete the installation and agree on the licensing etc, then ...

$ sudo gem install rails

Bowling answered 1/11, 2016 at 20:12 Comment(0)
L
5

Did you install the OS X developer tools? You'll need to do this to be able to build native extensions

Locally answered 5/12, 2011 at 18:11 Comment(0)
G
5

Steps to sort out this issue: Follow these steps unless error is fixed.

  1. Open terminal and run commands:
    sudo xcode-select --install
    sudo xcodebuild -license accept
  2. Make sure you have installed only one Xcode and it is the latest one. If more than one version is installed then keep only the latest one with name in applications like Xcode.app
  3. Open Xcode->preferences->locations then check if any command line tools are selected; if not then select
  4. Install Ruby by:
    brew install ruby
  5. Install CocoaPods by
    sudo gem install cocoapods
Gallican answered 1/12, 2020 at 21:26 Comment(3)
In my case, the point #2 was the problem since by default I can't work with the latest Xcode. I was going nuts! PS: In my cases, also a software update will be needed. Thanks for sharing!Mediation
Might want to close and reopen the terminal before running sudo gem install cocoapodsKathikathiawar
case #2 fixed my problem, u made my day!Jota
G
3

There are two possible reasons for the fail:

  1. PRIMARY REASON: Missing Xcode Command Line Tools

Verifying Xcode Command Line Tools Installation manually: Check for presence of "/usr/include/iconv.h" (if absent=>Missing or improperly installed Xcode CLT)

Installing Xcode CLT: Try running xcode-select --install on terminal and follow the instructions. If it fails, open Xcode.app, select from menu "Xcode" - "Open Developer Tool" - "More Developer Tools" to open the developer site, download the installer for your OS version and run it.

  1. SECONDARY REASON(if 1. fails): Version issues Try upgrading the ruby version using rbenv.

Hope it helps!

Grippe answered 29/7, 2015 at 6:53 Comment(0)
B
2

Switch Ruby to Homebrew version:

$ brew install ruby
$ brew link --overwrite ruby

$ echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile
$ echo 'export LDFLAGS="-L/usr/local/opt/ruby/lib"' >> ~/.bash_profile
$ echo 'export CPPFLAGS="-I/usr/local/opt/ruby/include"' >> ~/.bash_profile
Brummell answered 20/1, 2020 at 9:5 Comment(0)
Q
1

Try to install Ruby via RVM. I solved in this way

How to install ruby on Ubuntu with rvm

Quantifier answered 11/4, 2013 at 17:41 Comment(0)
F
1

The Command line tools didn't solve this issue for me. I upgraded ruby installation through rbenv to 2.2.0, made that the global default ruby installation, and this issue was fixed.

Formation answered 2/7, 2015 at 3:49 Comment(0)
S
0

I had this problem on macOS Catalina 10.15.7, and it seems that Xcode 12.3's Ruby is a variant of 2.6, but my software needed 2.7.

I installed rvm and ran rvm install ruby-2.7, and now it works just fine 🎉

Spadiceous answered 19/1, 2021 at 22:9 Comment(0)
O
-1

Just had a similar issue. I can confirm that installing the command line tools fixes it.

Ori answered 8/12, 2012 at 22:23 Comment(0)
H
-1

https://mcmap.net/q/176141/-docker-for-mac-mkmf-rb-can-39-t-find-header-files-for-ruby

Solved after run:

sudo xcode-select --install
sudo xcodebuild -license accept
Homogenize answered 16/7, 2020 at 15:24 Comment(0)
W
-1

I am not sure this could be the case of the majority, but I had a MacOS software update hanging in system settings, once I updated it, everything was somehow magically resolved 🤷‍♂️

Worked answered 19/11, 2023 at 23:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.