[Solved] Gem installation error: You have to install development tools first

I tried installing the gem ‘sinatra-websocket‘, but when I ran gem install sinatra-websocket, I got this error…

ERROR: Failed to build gem native extension

… along with …

C:/Ruby193/bin/ruby.exe extconf.rb
checking for main() in -lc... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=C:/Ruby193/bin/ruby
    --with-thin_parser-dir
    --without-thin_parser-dir
    --with-thin_parser-include
    --without-thin_parser-include=${thin_parser-dir}/include
    --with-thin_parser-lib
    --without-thin_parser-lib=${thin_parser-dir}/lib
    --with-clib
    --without-clib
C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:381:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:461:in `try_link0'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:476:in `try_link'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:619:in `try_func'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:845:in `block in have_library'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:790:in `block in checking_for'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:284:in `block (2 levels) in postpone'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:254:in `open'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:284:in `block in postpone'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:254:in `open'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:280:in `postpone'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:789:in `checking_for'
    from C:/Ruby193/lib/ruby/1.9.1/mkmf.rb:840:in `have_library'
    from extconf.rb:4:in `<main>'

Here’s what the mkmf file in C:Ruby193lib
ubygems1.9.1gems hin-1.6.3ext hin_parser
looks like:

"gcc -o conftest -IC:/Ruby193/include/ruby-1.9.1/i386-mingw32 -IC:/Ruby193/include/ruby-1.9.1/ruby/backward -IC:/Ruby193/include/ruby-1.9.1 -I. -DFD_SETSIZE=2048  -DFD_SETSIZE=2048  -O3 -fno-omit-frame-pointer -g -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration conftest.c  -L. -LC:/Ruby193/lib -L.   -march=i486   -lmsvcrt-ruby191  -lshell32 -lws2_32 -limagehlp -lshlwapi  "
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: #include <winsock2.h>
4: #include <windows.h>
5: int main(int argc, char **argv)
6: {
7:   return 0;
8: }
/* end */

I’m using Windows 7.

Why am I getting the error? There’s a line stating “could not create Makefile…”, but is that relevant?

Solution #1:

This worked for me:

sudo xcode-select -switch /

Respondent: Generic_User_ID

Solution #2:

Have you run:

xcode-select --install

To install the Xcode Developer tools?

Respondent: maheshsgr

Solution #3:

I was having this issue on MacOS Mojave(10.14.1). Installing latest ruby via Homebrew and setting some env variables solved it for me:

(copied from brew info ruby instructions)

export PATH="/usr/local/opt/ruby/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"
Respondent: Alex Chin

Solution #4:

edit: rubyinstaller now provides installs with the devkit included. These are labelled on the download page.

Sounds like you’re missing the devtools for ruby.

You can get them here: http://rubyinstaller.org/downloads/ just scroll down to “Development Kit”.

Download and extract that to anywhere you want. Open up the folder in command prompt and run ruby dk.rb init. This will create a config file detailing your ruby installations. Check this config file before continuing to confirm the paths to your ruby installation are correct.

Now run ruby dk.rb install. This will install the development kit.

Restart command prompt, and try to install the gem again.

Respondent: Divyanshu Rawat

Solution #5:

I ran into this same problem on Ubuntu 18.04. The fix on Ubuntu was to install the ubuntu-dev-tools package:

$ sudo apt-get install ubuntu-dev-tools
Respondent: Cereal

Solution #6:

If you’re on Mac and recently upgraded your Xcode tools, run the following commands:

sudo xcode-select --install
sudo xcodebuild -license
Respondent: Doug Couvillion

Solution #7:

My issue was that I updated to macOS Big Sur, and somehow, the command line tools were outdated.

I know this because, when I tried to run brew install rbenv, I received this error:

Error: Your Command Line Tools (CLT) does not support macOS 11.
It is either outdated or was modified.
Please update your Command Line Tools (CLT) or delete it if no updates are available.
Update them from Software Update in System Preferences or run:
  softwareupdate --all --install --force

If that doesn't show you any updates, run:
  sudo rm -rf /Library/Developer/CommandLineTools
  sudo xcode-select --install

Alternatively, manually download them from:
  https://developer.apple.com/download/more/.

So I ran

sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select --install

and after it finished, the gem installed successfully.

Respondent: Gowtham

Solution #8:

If you’re on Mac OS 10.14 (Mojave) try

cd  /Library/Developer/CommandLineTools/Packages/;
open macOS_SDK_headers_for_macOS_10.14.pkg

Credit to Roy Huang
Reference: https://stackoverflow.com/a/55732339/1598551

Respondent: Iulian Onofrei

Solution #9:

Tried all previous answers but none worked.

Did this instead:

brew install cocoapods
Respondent: tw airball

Solution #10:

This means you don’t have compiler tools installed. For mac xcode-select --install installs them. For linux or other systems you can use package managers, for example apt install cmake should install the necessary build tools for systems having apt package manager.

Respondent: lvndry

Solution #11:

Here is my solution for mac OSX:

In the Terminal App:

Step 1: Make sure you have installed the Xcode developer tool by the following script:

xcode-select --install

Step 2: Upgrade your ruby by the script:

brew install ruby

Step 3: Try again.

Respondent: Behlül

Solution #12:

I had recently upgraded to MAC OS big sur, and started getting the error when running gem install…

Solution is to remove the CLT completely and run the installation again.

Optional :

brew upgrade

Run :

sudo rm -rf /Library/Developer/CommandLineTools

sudo xcode-select –install

Respondent: xuzepei

Solution #13:

If you have multiple versions of Xcode installed, make sure you select the command line tools from the most recent one, otherwise gem will not be able to find them.

Respondent: Shreehari

Solution #14:

On macOS Catalina; After running $ brew install coreutils (credit) –– I followed an older article by Evan Hoffman, installing chef ( which embeds latest stable release of Ruby )

$ curl -L https://www.getchef.com/chef/install.sh | sudo bash

Then I can install any gems needed, with all the requisite development tools now installed;

$ sudo gem install -V <gem>

Your mileage may vary on Big Sur

Respondent: Alessandro Martin

Solution #15:

Actually, the real problem is when one upgrades from a major version of macOS to another and installs a newer Xcode.

cd /Library/Developer/CommandLineTools
ls -l SDKs/
total 0
lrwxr-xr-x  1 root  wheel   14 Feb  4 21:41 MacOSX.sdk -> MacOSX11.1.sdk
drwxr-xr-x  8 root  wheel  256 Feb  4 21:43 MacOSX10.15.sdk
drwxr-xr-x  7 root  wheel  224 Feb  4 21:43 MacOSX11.1.sdk

After going to Apple’s developer site and downloading the CLI tools by hand, this is what the folder contains:

$> ls -l SDKs/
lrwxr-xr-x  1 root  wheel   14 May  4 18:10 MacOSX.sdk -> MacOSX11.3.sdk
drwxr-xr-x  8 root  wheel  256 May  4 18:10 MacOSX10.15.sdk
drwxr-xr-x  7 root  wheel  224 Feb  4 21:43 MacOSX11.1.sdk
drwxr-xr-x  7 root  wheel  224 Mar 16 10:03 MacOSX11.3.sdk
lrwxr-xr-x  1 root  wheel   14 May  4 18:09 MacOSX11.sdk -> MacOSX11.3.sdk

You might still need to reset your Xcode with:

$> sudo xcode-select --reset
$> sudo xcodebuild -license

Verify with:

$> sudo xcode-select --print-path
/Applications/Xcode.app/Contents/Developer

Lastly, if you still see errors compiling something, you will need to pay close attention to what the compiler error is and address it. For example, in my case, it was simply that in macOS Big Sur clang needs special arguments to add declarations (struct). It was fixed with:

export CFLAGS="-fdeclspec $CFLAGS"

This was the error if you’re curious (or anybody looking for an answer for Ruby 3.0.x online):

/usr/local/Cellar/ruby/3.0.1/include/ruby-3.0.0/ruby/internal/fl_type.h:285:1: error: '__declspec' attributes are not enabled; use '-fdeclspec' or '-fms-extensions' to enable support for __declspec attributes
Respondent: ramene

Solution #16:

Try rvm install ruby --latest

If already installed try rvm reinstall ruby --latest

and $ sudo gem install -V <gem>

Works for me.

Respondent: 13m5

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.