'Gem installation error: You have to install development tools first (Windows)

this question is for Windows

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:\Ruby193\lib\ruby\gems\1.9.1\gems\thin-1.6.3\ext\thin_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:[1]

Also for Mac:

if you do not already have xcode dev tools installed on your system (perhaps you recently upgraded your OS or Xcode), run xcode-select --install

otherwise, a more invasive fix is to set the active directory using the --switch flag

sudo xcode-select --switch /

from the xcode-select manual:

  -s <path>, --switch <path>
          Sets the active developer directory to the given path, for example
          /Applications/Xcode-beta.app. This command must be run with
          superuser permissions (see sudo(8)), and will affect all users on
          the system. To set the path without superuser permissions or only
          for the current shell session, use the DEVELOPER_DIR environment
          variable instead (see ENVIRONMENT).

If you already have XCode installed, you may have a different problem, in which case, see this post Installing puma 5.6.2 with native extensions Gem::Ext::BuildError: ERROR: Failed to build

Solution 2:[2]

For a Mac:

Have you run:

xcode-select --install

To install the Xcode Developer tools?

Solution 3:[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"

Solution 4:[4]

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

Solution 5:[5]

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.

Solution 6:[6]

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

sudo xcode-select --install
sudo xcodebuild -license

Solution 7:[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.

Solution 8:[8]

Tried all previous answers but none worked.

Did this instead:

brew install cocoapods

Solution 9:[9]

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

Solution 10:[10]

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.

Solution 11:[11]

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.

Solution 12:[12]

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

Solution 13:[13]

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

Solution 14:[14]

This solution saved me:

  1. Install homebrew via terminal.

  2. After homebrew is successfully installed, type in the terminal

    '$ brew install cocoapods'

And Finally Done!

Solution 15:[15]

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.

Solution 16:[16]

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

Solution 17:[17]

For Ubuntu 20.04.3 LTS (Focal Fossa) -- WSL

There are some dependencies to solve before, try with following sequence of commands:

# if ERROR: 'Gem installation error: You have to install development tools first'
# BEFORE installing the dev-tools, install dependencies:
# if ERROR: 'GCC fatal error: stdio.h: No such file or directory'
$ sudo apt-get install libc6
# https://stackoverflow.com/a/20150282/5078874
$ sudo apt-get install libc6-dev
$ sudo apt-get install libffi-dev

# Now, go for the development tools
# https://stackoverflow.com/a/63442072/5078874
$ sudo apt-get install ubuntu-dev-tools

# Finally, try again
$ bundle update --bundler
# ... enjoy :)

References:

Solution 18:[18]

Try rvm install ruby --latest

If already installed try rvm reinstall ruby --latest

and $ sudo gem install -V <gem>

Works for me.