Install Ruby development tools for gem installation on OSX Catalina

16k views Asked by At

I know there are many instances of this question. I've done everything in those answers and have gotten nowhere after 4 hours.

I am trying to install a gem on Catalina 10.15.7 and getting the ever-popular

/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:467:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.

with this additional context

ERROR:  Error installing ffi:
    ERROR: Failed to build gem native extension.

    current directory: /Users/bmanica/.chefdk/gem/ruby/2.4.0/gems/ffi-1.13.1/ext/ffi_c
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -I /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0 -r ./siteconf20200925-10024-qilctf.rb extconf.rb --with-cflags\=-save-temps\=obj\ -o\ tmp/a.o

when invoking the installation via

gem install ffi  -- --with-cflags="-save-temps=obj -o tmp/a.o"

to get around Catalina's draconian permissions rules.

I've freshly downloaded XCode and the command line tools:

> xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
> xcode-select -p
/Applications/Xcode.app/Contents/Developer
> ls /Applications/Xcode.app/Contents/Developer
Applications    Library     Makefiles   Platforms   Toolchains  Tools       usr

I've accepted the license at least four times via

sudo xcodebuild -license accept

I've tried

sudo xcode-select -switch /

as Gem installation error: You have to install development tools first suggests. I am not on Windows so the rubyinstaller link does not help me.

Can't Find ffi.h When Installing ffi ruby gem references the exact gem I am trying to install, but as indicated xcode-select --install is not the answer.

None of the answers in Developer tools issues when installing Ruby gems are helpful either; I have already brew install openssl and brew install libffi and have exported the variables it told me to:

> set | grep FLAGS
CPPFLAGS=-I/usr/local/opt/libffi/include
LDFLAGS=-L/usr/local/opt/libffi/lib
> set | grep PATH
PKG_CONFIG_PATH=/usr/local/opt/libffi/lib/pkgconfig

I've also tried the suggestions in How to update Xcode from command line - xcode-select -r does nothing, xcode-select -s /Library/Developer/CommandLineTools does nothing, and $ sudo rm -rf /Library/Developer/CommandLineTools prevents xcode-select --install from working, and additionally I moved it anyway and redownloaded the command line dmg package from Apple's developer site. Still same problem.

What do I try next to get this gem to install?

2

There are 2 answers

1
Jörg W Mittag On

Judging by the path names in your error message, you are trying to modify Apple's System Ruby. This is not allowed.

Apple's System Ruby is exclusively for internal use by macOS and/or support for legacy applications. You must not use it, and you most certainly must not modify it. Modifying it would negate the whole "support for legacy applications" thing.

Apple itself has the following to say in the release notes for macOS 10.15 Catalina:

Scripting Language Runtimes

Deprecations

  • Scripting language runtimes such as Python, Ruby, and Perl are included in macOS for compatibility with legacy software. Future versions of macOS won’t include scripting language runtimes by default, and might require you to install additional packages. If your software depends on scripting languages, it’s recommended that you bundle the runtime within the app. (49764202)

There are many, many options for installing Ruby on macOS, including but not limited to compiling it yourself, downloading pre-compiled binaries, Homebrew, ruby-install, ruby-build, RVM. There are many options for managing Ruby installations on macOS, including but not limited to chruby, asdf, rbenv, and RVM.

Most of these have the added advantage that they support newer versions than the one Apple ships, and even more interesting, they support different implementations than just YARV. My personal favorite is TruffleRuby, which, when run on the GraalVM, was up to 1000 times faster for me, depending on the benchmark.

Please, do not use System Ruby.

2
four-eyes On

Finally got it after hours. I followed this instruction, changed it a bit

  1. Make sure brew is installed. If not /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

  2. Set shell to zsh curl -L http://install.ohmyz.sh | sh

or, set PATH

  1. eval "$(rbenv init -)"

  2. Install Ruby Version Manager brew update brew install rbenv ruby-build

  3. Install Ruby rbenv install 2.6 # I used my systems default version number because I was not sure if it messes with my OS X system version. rbenv global 2.6 rbenv rehash

  4. Add it to your zsh echo 'eval "$(rbenv init -)"' >> ~/.zshrc source ~/.zshrc

That did it for me. All the other stuff like installing Xcode, accept the Xcode License, Xcode command line tools did not do it.