Error installing cocoapods: Failed to build gem native extension

Published by Moncef Belyamani on
Updated on

Have you been trying to install cocoapods on your Mac and getting this “Failed to build gem native extension” error?

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

This is a common issue people face when trying to install cocoapods on a Mac that doesn’t have a proper Ruby development environment. This particular error is usually due to one or more of these issues:

  • You’re using the version of Ruby that came preinstalled on your Mac (known as the system Ruby)
  • You have missing or broken development tools

To find out whether or not you’re using the system Ruby, run this command:

which ruby

If it says /usr/bin/ruby, then that’s the system Ruby on a Mac, and you definitely don’t want to be using it. Keep reading to understand why you shouldn’t use it, and what to do instead.

Most people run into this issue after trying to install cocoapods using the system Ruby, either with the --user-install flag:

gem install cocoapods --user-install

or with sudo:

sudo gem install cocoapods

To make things worse, the cocoapods documentation recommends using the system Ruby, and to use sudo gem install cocoapods.

However, you should never use sudo to install gems!

Perhaps you initially tried to install cocoapods without sudo, using gem install cocoapods, but then you got the infamous error you don’t have write permissions for the /Library/Ruby/Gems/2.6.0 directory:

ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory

You’ll get this error for any gem you try to install using the system Ruby. By default, it tries to install gems in the /Library/Ruby/Gems/2.6.0 directory, which is not meant to be modified, and is why Apple doesn’t give you permissions to write to it.

Here are more reasons why you shouldn’t use the system Ruby to install gems on a Mac.

So what’s the proper way to install cocoapods on a Mac?

Glad you asked! The most reliable method, and the only one I recommend, is to install a separate and newer version of Ruby using a version manager. If you’re really interested in all the possible options, including ones I don’t recommend (such as installing Ruby with Homebrew), read my definitive guide to installing gems on a Mac.

I highly recommend using a Ruby version manager because it allows you to have multiple versions of Ruby installed at the same time, and makes it easy to switch between them. Even if you’re using Ruby for the first time, it’s worth your time to learn how to use a Ruby manager because you will inevitably need one.

Over the past twelve years, I’ve helped hundreds of thousands of people set up Ruby on their Mac. From clean Macs to the most obscure issues, I’ve seen and fixed it all. And the most reliable solution is to use a version manager, specifically chruby and ruby-install.

Install Ruby with a version manager

At a high level, there are a minimum of five steps to a working Ruby environment on macOS with a Ruby manager:

  1. Install Homebrew (which will also install the prerequisite Apple command line tools)
  2. Install a Ruby version manager such as chruby and ruby-install (others include asdf, frum, rbenv, and RVM)
  3. Configure the Ruby version manager
  4. Install a specific version of Ruby
  5. Switch to that version of Ruby

You have two options for performing those steps:

  • Have everything set up for you in 15 minutes or less with a single terminal command
  • Spend an hour or more setting everything up manually

Have everything set up for you in 15 minutes or less with a single command

Ruby on Mac is the easiest, fastest, and most reliable way to set up and maintain a proper Ruby dev environment on a Mac. It can automatically fix your outdated or misconfigured development setup in minutes, saving you from having to wipe your computer and reinstall macOS. It also automatically installs all the other development tools you’ll need for Rails, Jekyll, Flutter, React Native, or any other project that depends on Ruby. It will save you so much time and frustration.

When you buy Ruby on Mac today, you’ll be supporting an independent developer and his family. And if you need it for your job or business, you should be able to expense it.

Read more about what makes Ruby on Mac special and how much people love Ruby on Mac.

Spend an hour or more setting everything up manually

If you prefer to spend an hour or more doing everything manually, you can follow my free step-by-step guide for installing Ruby on a Mac. The guide works best when you start with a clean development environment.

Depending on how messy your setup is, you might need to spend additional time uninstalling some or all dev tools. In the worst case, you’ll need to spend a few more hours backing up your computer, reformatting the hard drive, and reinstalling macOS.