Search code examples
ruby-on-railsrubymacosmacos-monterey

Gem::Ext::BuildError: ERROR: Failed to build gem native extension on macOS Monterey


When I run bundle install on my rails 5 project, I am getting a number of errors on gems that appear to have native extensions. Here is the top of the output for one of those gems:

Installing nio4r 1.2.1 with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

checking for unistd.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_thread_call_without_gvl()... yes
checking for sys/select.h... yes
checking for poll.h... yes
checking for sys/epoll.h... no
checking for sys/event.h... yes
checking for sys/queue.h... yes
checking for port.h... no
checking for sys/resource.h... yes
creating Makefile

current directory: /Users/<my-username>/.rvm/gems/ruby-2.7.4/gems/nio4r-1.2.1/ext/nio4r
make "DESTDIR=" clean

current directory: /Users/<my-username>/.rvm/gems/ruby-2.7.4/gems/nio4r-1.2.1/ext/nio4r
make "DESTDIR="
compiling monitor.c
monitor.c:91:24: warning: '(' and '{' tokens introducing statement expression appear in different macro expansion contexts [-Wcompound-token-split-by-macro]
if(interests_id == rb_intern("r")) {
                   ^~~~~~~~~~~~~~
/Users/<my-username>/.rvm/rubies/ruby-2.7.4/include/ruby-2.7.0/ruby/ruby.h:1847:23: note: expanded from macro 'rb_intern'
    __extension__ (RUBY_CONST_ID_CACHE((ID), (str))) : \
                  ^
monitor.c:91:24: note: '{' token is here
    if(interests_id == rb_intern("r")) {
                       ^~~~~~~~~~~~~~

I have tried a number of things.

Per this post I tried the following suggestions:

# didn't fix it
bundle config build.nio4r --with-cflags="-std=c99"
bundle

# didn't fix it
xcode-select --install
sudo xcodebuild -license accept
bundle

I also referenced this Go Rails guide, specifically the "Final Steps" section that deals with gems that require C extensions, but that command seems to not apply because I didn't migrate my macOS from Mojave.

At this point I'm not sure what to try next. Ultimately: it appears the issue revolves around being unable to install gems that require C extensions.

Environment:

  • macOS Monterey version 12.2
  • Using rvm with ruby version 2.7.4
  • Project is using rails version 5.0.1

Solution

  • It turns out the problem was that in the Gemfile.lock file, it was locked to using an older version of bundler. We simply deleted the Gemfile.lock, and re-ran bundle to regenerate Gemfile.lock. That solved it. It now says this at the bottom of the Gemfile.lock file, so it liked this version of bundler:

    BUNDLED WITH
       2.3.9