Error installing cocoapods: Failed to build gem native extension
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:
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
gem install cocoapods --user-install
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
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 eleven 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
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:
- Install Homebrew (which will also install the prerequisite Apple command line tools)
- Install a Ruby version manager such as chruby and ruby-install (others include asdf, frum, rbenv, and RVM)
- Configure the Ruby version manager
- Install a specific version of Ruby
- 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 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. It also automatically installs all the other development tools you’ll need for Rails, Jekyll, Flutter, React Native, or any other project the 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.
Spend an hour or more setting everything up manually
If you haven’t yet tried to install Ruby or other development tools on your Mac, you should be able to get up and running with the basics by following my free step-by-step guide for installing Ruby on a Mac.