162

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?

Jason FB
  • 4,752
  • 3
  • 38
  • 69
Generic_User_ID
  • 1,067
  • 2
  • 13
  • 16
  • The mkmf.log file can be found under the gem's installation directory. – emi Jan 04 '15 at 18:02
  • @siaw23 I've added the contents of the file – Generic_User_ID Jan 04 '15 at 18:45
  • Have you got a C compiler installed? – ian Jan 05 '15 at 11:43
  • @iain How do I check that? And where can I find an installation? – Generic_User_ID Jan 07 '15 at 15:32
  • 1
    The one I've used in the past is [Mingw](http://www.mingw.org/). From [this](http://wiki.openttd.org/Compiling_on_Windows_using_MinGW#Testing_MinGW.2FMSYS_installation) you can try `make -v` and `gcc -v` to check. You might try installing [the Ruby dev kit](http://rubyinstaller.org/add-ons/devkit/) – ian Jan 08 '15 at 00:25
  • 2
    Check this answer out, it's detailed and better than any advice I could give. http://stackoverflow.com/a/11484346/335847 – ian Jan 08 '15 at 00:26
  • 1
    if you are getting this on a Mac when running bundle install--- for example, for puma 5.6.2, I have made a separate SO post because this one is specific to Windows (as well there is no XCode on windows so all these XCode answers are very confusing) https://stackoverflow.com/questions/71383295/installing-puma-5-6-2-with-native-extensions-gemextbuilderror-error-failed – Jason FB Mar 07 '22 at 18:10

18 Answers18

208

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
maheshsgr
  • 2,422
  • 1
  • 14
  • 11
69

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"
neaumusic
  • 10,027
  • 9
  • 55
  • 83
Divyanshu Rawat
  • 4,421
  • 2
  • 37
  • 53
67

For a Mac:

Have you run:

xcode-select --install

To install the Xcode Developer tools?

Jason FB
  • 4,752
  • 3
  • 38
  • 69
Alex Chin
  • 1,642
  • 15
  • 28
  • 43
    I don't think the "development tools" being referenced in the error has anything to do with xcode, especially considering this is on Windows. – Lachlan Arthur Apr 27 '15 at 07:20
  • I moved to a new machine and forgot about xcode command line tools as a separate install to XCode. This helped thanks. I was struggling with `gem install puma` – Josh Peak Sep 20 '15 at 00:31
  • 8
    The original question definitely stated Windows, but regardless - this helped me on a Mac! – Aegix Nov 06 '15 at 22:06
  • 1
    Just what I was missing on OSX El Capitan. – Christophe Geers Mar 13 '17 at 13:24
  • 1
    'xcode-select' is not recognized as an internal or external command, operable program or batch file. – Mugen May 30 '18 at 09:27
  • You might need to set the Command Line Tools in Xcode. 1. Open Xcode 2. Navigate to Xcode > Preferences > Locations 3. Set the Command Line Tools to the version of Xcode you have installed – Matthew Glenn Jun 12 '18 at 19:20
  • 5
    (On Mac OSx Mojave) After running `xcode-select --install`, close and re-open the terminal. Then, run `sudo xcodebuild -license`, follow the instructions and finally accept the license agreement. In my experiment, this was the solution to solve the problem. – Esmaeil MIRZAEE Nov 16 '18 at 20:32
  • 1
    Hilariously, this answer is STILL the top search result, even for OSX. It's _accidentally_ the best answer. – TheGerm Jan 31 '19 at 21:24
55

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
Doug Couvillion
  • 1,071
  • 10
  • 13
27

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.

Cereal
  • 3,699
  • 2
  • 23
  • 36
25

Tried all previous answers but none worked.

Did this instead:

brew install cocoapods
lvndry
  • 421
  • 6
  • 13
18

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

sudo xcode-select --install
sudo xcodebuild -license
Gowtham
  • 11,853
  • 12
  • 43
  • 64
  • 2
    I was able to execute your steps without error on Mojave, but they didn't help me with the error "You have to install development tools first". – Ryan Apr 17 '20 at 01:31
18

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.

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
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

tw airball
  • 1,359
  • 11
  • 12
  • 1
    I was able to execute your steps without error on Mojave, but they didn't help me with the error "You have to install development tools first". – Ryan Apr 17 '20 at 01:30
7

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!

update: You can install homebrew by this command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

from here

M Karimi
  • 1,991
  • 1
  • 17
  • 34
5

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.

xuzepei
  • 1,119
  • 11
  • 9
  • 1
    I tryed anything on the earch, more that two days, I am in MacOS 10.15.7 catalina, xcode 12.3, trying to develop nativescript apps, and finallay this solution worked for me and cocoapods installed successfully. thanks man. – mesut Jan 14 '21 at 20:49
3

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.

Behlül
  • 3,412
  • 2
  • 29
  • 46
3

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
13m5
  • 139
  • 2
2

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

Shreehari
  • 334
  • 4
  • 10
2

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:

0

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.

0

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

ramene
  • 61
  • 5
0

In Mac, for me worked by doing a:

sudo xcode-select --reset

I had xcode-select previously installed, but it was not working anyway. Doing a --reset fixed this for me.