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
Ky -
  • 30,724
  • 51
  • 192
  • 308
user884913
  • 421
  • 1
  • 4
  • 4

13 Answers13

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.

Rob
  • 481
  • 4
  • 2
  • Thanks Rob! That was the missing piece of the puzzle! – devlord Sep 09 '12 at 05:04
  • 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](http://stackoverflow.com/questions/9329243/xcode-4-4-and-later-install-command-line-tools) worked for me – connorbode Apr 16 '14 at 17:35
41

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

xcode-select --install
Abdullah Aden
  • 882
  • 9
  • 13
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.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • that worked - thanks very much. Brand new to developing on mac so apologies if this question was rudimentary – user884913 Dec 06 '11 at 17:21
5

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

mirageglobe
  • 2,446
  • 2
  • 24
  • 30
5

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

RyanWilcox
  • 13,890
  • 1
  • 36
  • 60
4

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
ad absurdum
  • 19,498
  • 5
  • 37
  • 60
Javed Iqbal
  • 413
  • 1
  • 6
  • 10
  • 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! – Alessandro Francucci Mar 03 '21 at 18:08
  • Might want to close and reopen the terminal before running sudo gem install cocoapods – T M Sep 28 '21 at 07:38
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!

Mohit Chawla
  • 181
  • 7
1

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

How to install ruby on Ubuntu with rvm

sparkle
  • 7,530
  • 22
  • 69
  • 131
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.

tzharg
  • 313
  • 2
  • 11
1

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
Christopher
  • 136
  • 9
0

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

0

https://stackoverflow.com/a/58226876/8070378

Solved after run:

sudo xcode-select --install
sudo xcodebuild -license accept
caperonce
  • 61
  • 6
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

Ky -
  • 30,724
  • 51
  • 192
  • 308