Error installing rails: Failed to build gem native extension

Published by Moncef Belyamani on
Updated on

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

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

This is a common issue people face when trying to install Rails 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
  • You’re trying to install an outdated version of Rails

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 Rails using the system Ruby, either with the --user-install flag:

gem install rails --user-install

or with sudo:

sudo gem install rails

However, you should never use sudo to install gems.

Perhaps you initially tried to install Rails without sudo, using gem install rails, 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 Rails 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.

Other reasons for getting the failed to build gem native extension error when installing Rails

Even if you have a proper Ruby dev setup with all the necessary tools and a newer version of Ruby, it’s still possible to get the “failed to build gem native extension” error when installing an outdated version of Rails. For example, if you try to install Rails 5.2.1, you might get an error related to “Could not find MIME type database in the following locations”:

Could not find MIME type database in the following locations: [
"/usr/local/share/mime/packages/freedesktop.org.xml", 
"/opt/homebrew/share/mime/packages/freedesktop.org.xml", 
"/opt/local/share/mime/packages/freedesktop.org.xml", 
"/usr/share/mime/packages/freedesktop.org.xml"
]

One way to fix this is by installing the missing tool with Homebrew. The command below assumes you already have Homebrew installed:

brew install shared-mime-info

However, a better solution is to install a newer version of Rails, or update the Rails version in your existing project to at least 5.2.8.1, which is the latest version in the 5.2.x series.

So what’s the proper way to install Rails 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 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.

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 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.