ERROR: Error installing rails: ERROR: Failed to build gem native extension

Updated

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 gems on a Mac that doesn’t have a proper Ruby development environment. Most people run into this issue after trying to install Rails with sudo:

sudo gem install rails

However, you should never use sudo to install gems.

Perhaps you initially tried to install without sudo, using gem install rails, but then you got this error:

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

That’s because macOS comes with Ruby preinstalled, but Apple doesn’t want you to make changes to the system Ruby, by installing gems for example.

Learn why you shouldn’t use the system Ruby to install gems on a Mac.

So what’s the correct way to install gems on a Mac?

While there are various ways to install gems on a Mac, there’s really only one method that should be recommended: using a version manager.

I highly recommend using a Ruby manager because it allows you to have multiple versions of Ruby installed at the same time, and makes it easy to switch between them. You will likely come across instructions online to install Ruby directly with Homebrew, but that will eventually confuse you because you can only use one version at a time.

Over the past ten years, I’ve helped 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.

To help people like you, and to make it as easy and painless as possible, I built Ruby on Mac, an automated script that will get you up and running with Rails in minutes.

Read what people say about the script.

Not only will Ruby on Mac set up a proper Ruby environment, it will also automatically install Rails, and all the other tools you’ll need for Rails, such as Bundler, Git, Postgres, Node, Yarn, and Redis. All with a single command. It will save you so much time and frustration.

It doesn’t just have a one-time use. You can run it over and over to keep your system up to date and secure. And the next time you get a new Mac, it will save you half a day because it can also automatically install all your Mac apps, in addition to a complete development environment. You get all of these time savings now and in the future for a one-time cost.

When you buy Ruby on Mac today, you’ll be supporting an independent developer, and you get it at a very reasonable price given how much time it saves.

If you’re trying to set up Rails on a work computer, you should be able to expense the one-time cost of Ruby on Mac.