44

I am using macOS Mojave and when I try to install cocoapods using gem I am getting the following error.

Building native extensions. This could take a while... ERROR: Error installing cocoapods: ERROR: Failed to build gem native extension.

current directory: /Library/Ruby/Gems/2.3.0/gems/ffi-1.12.2/ext/ffi_c

/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby -r ./siteconf20200302-707-1iveybq.rb extconf.rb mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/include/ruby.h

extconf failed, exit code 1

Gem files will remain installed in /Library/Ruby/Gems/2.3.0/gems/ffi-1.12.2 for inspection. Results logged to /Library/Ruby/Gems/2.3.0/extensions/universal-darwin-18/2.3.0/ffi-1.12.2/gem_make.out

This error comes when I run the following commands.

sudo gem install cocoapods

sudo gem install -n /usr/local/bin cocoapods

I have already installed xcode command line tools and I get the following message when I run xcode-select --install

xcode-select: error: command line tools are already installed, use "Software Update" to install updates

I have also agreed to xcode license agreements with the command sudo xcodebuild -license

The error log in /Library/Ruby/Gems/2.3.0/extensions/universal-darwin-18/2.3.0/ffi-1.12.2/gem_make.out gives the following

current directory: /Library/Ruby/Gems/2.3.0/gems/ffi-1.12.2/ext/ffi_c /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby -r ./siteco$ mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.fra$

extconf failed, exit code 1

What do I do to fix this issue ?

Muljayan
  • 3,588
  • 10
  • 30
  • 54
  • Do you have ruby installed? Try `ruby --version` from the command line – Rob C Mar 02 '20 at 02:16
  • Hi yes I get this . ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18] – Muljayan Mar 02 '20 at 02:20
  • What are the contents of the log file? /Library/Ruby/Gems/2.3.0/extensions/universal-darwin-18/2.3.0/ffi-1.12.2/gem_make.out – Rob C Mar 02 '20 at 02:38
  • I included the error log – Muljayan Mar 02 '20 at 03:02
  • 1
    I would suggest you take a look at this [HERE](https://stackoverflow.com/questions/46377667/docker-for-mac-mkmf-rb-cant-find-header-files-for-ruby) – Rob C Mar 02 '20 at 04:35
  • Thank you soo much. It worked. If you could draft an answer and link that question ill select it as the answer. – Muljayan Mar 02 '20 at 04:56

11 Answers11

81

The answer for this can be found here here although it is unrelated to this question. Below is a snippet of the answer which works best.

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 using the current Xcode 11 SDK, use

sudo xcode-select --switch /Applications/Xcode.app
Muljayan
  • 3,588
  • 10
  • 30
  • 54
Anshul Kataria
  • 956
  • 8
  • 8
  • 1
    Many thanks , after 10 hours of searching i just found your solution and it's really helpful – Moumen Alisawe Sep 09 '20 at 09:49
  • @MoumenAlisawe Thank you. Happy To help :) – Anshul Kataria Sep 10 '20 at 12:25
  • At my 10.14 path wasn't changing from 11.1 to 10.15. I had MacOSX10.14.sdk and MacOSX11.1.sdk In SDK folder. Changing MacOSX11.1.sdk name to MacOSX11.1.sdk.backup made its unused and running Xcode select successfully changed the path. I don't know any outcome of changing name or deleting that folder but it solved my question in short term. – Ataberk Jan 28 '21 at 00:23
  • Worked too on MacOS 10.15.7 and Xcode 11.7, I just had to run `sudo xcode-select --switch /Applications/Xcode.app`, because `ruby -rrbconfig` was printing the right path – Ricardo Isidro Feb 08 '21 at 16:20
  • This, combined with xcrun, and it worked for me. The config.h file is in `universal-darwin20/ruby/` instead of `ruby/` in that rubyhdrdir – drawnonward Feb 12 '21 at 04:00
  • works for mac os monterey beta 2 and xcode 12.5 – andreich Jul 06 '21 at 15:38
22

For me use a command sudo gem install cocoapods -v 1.8.4 instead of sudo gem install cocoapods

Reference: https://github.com/CocoaPods/CocoaPods/issues/9568#issuecomment-599235420

kb920
  • 3,039
  • 2
  • 33
  • 44
  • I'm on catalina 10.15.5 and xcode 11.5. The missing headers business above wasn't the issue for me - but installing -v 1.8.4 did work so thanks. – O Wigley Sep 17 '20 at 18:31
18
sudo xcode-select --switch /Library/Developer/CommandLineTools

ruby -rrbconfig -e 'puts RbConfig::CONFIG["rubyhdrdir"]'
Cainã Max Couto-Silva
  • 4,839
  • 1
  • 11
  • 35
13
  1. Run
    ruby -rrbconfig -e 'puts RbConfig::CONFIG["rubyhdrdir"]'
    
    to figure out your ruby configuration.
  2. If the output is a nonexistent MacOSX10.15 path. Like
    /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
    
    then you should correct the configuration by doing next step.
  3. Run
    sudo xcode-select --switch /Library/Developer/CommandLineTools
    
  4. You can run
    ruby -rrbconfig -e 'puts RbConfig::CONFIG["rubyhdrdir"]'
    
    again to see if the path has changed.
  5. The output should be something like
    /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0
    

Now you can run gem install to install cocoapods!

ndmeiri
  • 4,979
  • 12
  • 37
  • 45
Nico Pan
  • 131
  • 3
10

This helped/ worked for me like charm:

# brew cleanup -d -v    
# brew install cocoapods

P.S.: You should have Homebrew installed. You can use below command to install brew:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Good luck :)

mAc
  • 2,434
  • 2
  • 22
  • 39
  • 1
    Tried everything else on arm based Mac and nothing worked besides this. No problems so far. Brew coming in clutch again! – maxtuzz Nov 14 '22 at 21:19
5

Try this on mac os big sur

sudo gem install cocoapods -v 1.8.4
Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
r3drum
  • 67
  • 1
  • 1
  • 1
    This is plagiarised from https://stackoverflow.com/a/63555919/7733418 , the older existing answer by kb920 but has less explanation. – Yunnosch Sep 28 '20 at 06:33
3

Try

  1. Install homebrew via terminal.

  2. After homebrew is successfully installed, type in the terminal

    $ brew install cocoapods

arango_86
  • 4,236
  • 4
  • 40
  • 46
2

just spent my day figuring out solution, I am using MacOS Big Sur Beta 8. to upgrade cocoapods with gem, try this:

  1. install rbenv
  • git clone https://github.com/rbenv/rbenv.git ~/.rbenv

  • cd ~/.rbenv && src/configure && make -C src

  • Add ~/.rbenv/bin to your $PATH for access to the rbenv command-line utility. see: https://github.com/rbenv/rbenv#basic-github-checkout

  • make sure export PATH="$HOME/.rbenv/shims:${PATH}" was added to your $PATH

  1. xcode-select --switch /Applications/Xcode.app/Contents/Develope

  2. gem install cocoapods should work like a charm!

ukiyoevega
  • 129
  • 1
  • 6
0

You should install the latest version of CommandLineTools for Xcode to fix this issue.

0

I copied these commands from other threads. After trying several approaches answered above and in other threads. Only this one works for me.

  1. Run this command and then close the Terminal

    curl -L https://get.rvm.io | bash -s stable

  2. Run these commands one by one or just copy and paste them all in the Terminal. The latest version now is 3.1.0

    rvm install ruby-3.1.0

    rvm --default use 3.1.0

    sudo gem install cocoapods

William Tong
  • 479
  • 7
  • 14
0

Short answer is:

curl -sSL https://rvm.io/mpapis.asc | gpg --import -
curl -sSL https://rvm.io/pkuczynski.asc | gpg --import -


curl -sSL https://get.rvm.io | bash -s stable --ruby

sudo gem install cocoapods

For detail pls refer: another post's answer

crifan
  • 12,947
  • 1
  • 71
  • 56