Error installing rails: Failed to build gem native extension
Have you been trying to install Rails on your Mac and getting this 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. This particular error is usually due to missing or broken command line tools.
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
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
Apple preinstalls Ruby on all Macs, and the default location where gems would get installed is in the
However, that directory is not meant to be modified, which is why Apple doesn’t give you permissions to write to it. On top of that, the version that comes with your Mac is super old, and you can’t upgrade it. Ruby 2.6 reached end of life in March 2022.
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. If you’re really curious about the other ways, read my definitive guide to installing gems on a Mac.
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. 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 thousands of people set up Ruby on their Mac. It’s my specialty. From clean Macs, to OpenSSL errors, 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 rbenv, asdf, 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 minutes with a single command
- Spend an hour or more setting everything up manually
Have everything set up for you in minutes with a single command
Ruby on Mac will automatically install Ruby with chruby, ruby-install, and all the other development tools you’ll need for Rails or Jekyll. 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 by simply typing “rom” to keep your system up to date and secure. And the next time you get a new Mac, the Ultimate version will save you an entire day because it can also automatically install all your Mac apps, fonts, macOS preferences, and GitHub repos, 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 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.
With Ruby on Mac, you can also choose to automatically install Rails, and all the other development tools you’ll need for a Rails app. And if you’ve already tried to install Rails using another method, and you’re running into issues, Ruby on Mac will fix those as well!