Error installing compass: Failed to build gem native extension

Published by Moncef Belyamani on
Updated on

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

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

This is a common issue people face when installing the Compass gem on a Mac that hasn’t yet been properly set up for Ruby development. The “Failed to build gem native extension” error is usually due to one or more of these issues:

  • You’re using the system Ruby (the version of Ruby that came preinstalled on your Mac)
  • You have missing or broken development tools
  • You’re trying to install an old version of Compass on an Apple Silicon Mac.

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 that. Keep reading to understand why you shouldn’t use it, and what to do instead.

Failed to build gem native extension error installing Compass when using the system Ruby

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

gem install compass --user-install

or with sudo:

sudo gem install compass

However, you should never use sudo to install gems.

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

Failed to build gem native extension error when installing Compass using a newer Ruby version

If you’re not using the system Ruby, and you know you have a proper Ruby dev setup with all the necessary tools, then the most likely cause of the “failed to build gem native extension” error when installing Compass is one or more outdated gems. If you’re seeing this issue when running bundle install in an existing Ruby project (such as a Rails app or Jekyll site), then I recommend updating any gems that are mentioned in the error. For example:

bundle update compass

Here are more resources that might be helpful:

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