Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errors when installing cocoapods with gem

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 ?

like image 451
Muljayan Avatar asked Mar 02 '20 02:03

Muljayan


People also ask

Should I install CocoaPods with GEM or homebrew?

If you try to install a gem through pre-installed Ruby, you've to use sudo by default witch is not a good practice. Another option is to install CocoaPods in your user-level directory by following instructions available on the Sudo-less installation section. I would suggest you install CocoaPods using brew .

Is CocoaPods a Ruby gem?

CocoaPods is built with Ruby and is installable with the default Ruby available on macOS. We recommend you use the default ruby. Using the default Ruby install can require you to use sudo when installing gems.


2 Answers

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 
like image 124
Anshul Kataria Avatar answered Sep 23 '22 11:09

Anshul Kataria


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

like image 39
kb920 Avatar answered Sep 19 '22 11:09

kb920