宝石安装错误:您首先需要安装开发工具(Windows)

166

此问题针对Windows操作系统

我尝试安装宝石 'sinatra-websocket',但当我运行 gem install sinatra-websocket 时,出现了以下错误...

ERROR: Failed to build gem native extension

... 以及其他内容...

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>'

以下是 C:\Ruby193\lib\ruby\gems\1.9.1\gems\thin-1.6.3\ext\thin_parser 文件夹中的 mkmf 文件:

"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 */

我正在使用Windows 7。

为什么会出现这个错误?有一行写着“无法创建Makefile...”,但那是否相关呢?


mkmf.log文件可以在gem的安装目录下找到。 - emi
你是否安装了 C 编译器? - ian
1
我过去使用的是Mingw。从这里你可以尝试make -vgcc -v来检查。你可以尝试安装Ruby开发工具包 - ian
2
看看这个答案,它非常详细,比我能给出的任何建议都要好。https://dev59.com/qGbWa4cB1Zd3GeqPTRom#11484346 - ian
1
如果你在Mac上运行bundle install时遇到了这个问题——例如,对于puma 5.6.2,我已经发布了一个单独的SO帖子,因为这个问题是特定于Windows的(在Windows上没有XCode,所以所有这些XCode答案都非常令人困惑)https://dev59.com/C3ENtIcB2Jgan1zn72j6 - Jason FB
显示剩余2条评论
20个回答

210

同样适用于Mac:

如果您的系统上没有安装Xcode开发工具(可能是因为您最近升级了操作系统或Xcode),请运行xcode-select --install

否则,一种更加侵入性的修复方法是使用--switch标志来设置活动目录。

sudo xcode-select --switch /

来自Xcode-select手册:

  -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).
如果您已经安装了Xcode,则可能存在其他问题。在这种情况下,请参阅此文章Installing puma 5.6.2 with native extensions Gem::Ext::BuildError: ERROR: Failed to build

7
我已经安装了命令行工具,这对我也起作用了。谢谢! - basicallydan
21
没能帮到我。 - Dave Nottage
8
我能理解您遇到了这个错误:xcode-select: error: invalid developer directory '/'。它的意思是Xcode选择了一个无效的开发者目录'/',导致出现了问题。 - Lê Vũ Huy
1
哇,这对我起作用了。谢谢!(安装 CocoaPods 插件 - https://github.com/joncardasis/cocoapods-user-defined-build-types)。同时很好奇为什么这样可以解决问题! - sinewave440hz
10
这个答案需要更多的解释。事实上,它需要A)使用sudo权限,B)我不知道xcode是什么,C)看起来要对根目录进行操作,这三个因素对于新手来说都是巨大的警告信号。 - Excel Help
显示剩余6条评论

70
我在MacOS Mojave(10.14.1)上遇到了这个问题。通过Homebrew安装最新版本的Ruby并设置一些环境变量,问题得到了解决:
(从 brew info ruby 说明中复制)
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"

6
如果您是通过brew安装ruby的,可以通过brew info ruby获取这些环境变量。 - Dara Kong
1
这个答案解决了我在最近的OSX更新后尝试使用Sass编译器时遇到的问题。 - Jonathan Eltgroth
1
适用于Catalina 10.15.7,非常感谢。 - someone
1
已在 Big Sur 11.6.5 上工作,谢谢。 - Andrew Kingdom
1
是的,它可以在M1操作系统12.6.x上运行(我已经安装了用于工作的Xcode工具)。 - antislice
显示剩余2条评论

67

Mac电脑:

您是否运行了:

xcode-select --install
安装Xcode开发工具需要做什么?

43
我认为错误中提到的“开发工具”与Xcode无关,特别是考虑到这是在Windows上。 - Lachlan Arthur
8
原问题肯定是提到了Windows,但无论如何——这个回答对我在Mac上也有帮助! - Aegix
1
在OSX El Capitan上我正缺少的东西。 - Christophe Geers
1
'xcode-select' 不被识别为内部或外部命令、可执行程序或批处理文件。 - Mugen
6
(在Mac OSx Mojave上)运行 xcode-select --install 后,关闭并重新打开终端。然后运行 sudo xcodebuild -license,按照说明进行,最后接受许可协议即可。在我的实验中,这是解决问题的解决方案。 - Esmaeil MIRZAEE
显示剩余3条评论

55

我在 Ubuntu 18.04 遇到了同样的问题。在 Ubuntu 上解决的方法是安装 ubuntu-dev-tools 包:

$ sudo apt-get install ubuntu-dev-tools

6
谢谢,对我有用!在Ubuntu 20.04上有效。 - kintaro
谢谢。我在20.04上也为此苦苦挣扎了一段时间。 - guero64
我正在使用Docker,主机机器是Ubuntu,作为容器。这对我非常有效,谢谢伙计。 - uzay95
我搜索了很多不同的解决方案,最终发现是缺少了make命令。 - Vort3x

27

编辑:rubyinstaller现在提供了包含devkit的安装程序。下载页面上标有这些信息。

听起来您缺少用于Ruby的开发工具(devtools)。

您可以从http://rubyinstaller.org/downloads/ 下载它们,只需向下滚动到“Development Kit”即可。

下载并将其解压缩到任何位置。在命令提示符中打开文件夹,然后运行ruby dk.rb init。这将创建一个详细说明您Ruby安装的配置文件。在继续之前,请检查此配置文件以确认Ruby安装的路径是否正确。

现在运行ruby dk.rb install。这将安装开发工具。

重新启动命令提示符,然后再次尝试安装gem。


25

尝试了所有之前的答案,但都没有起作用。

于是尝试了以下方法:

brew install cocoapods

我也遇到了同样的问题。我花了一些时间来解决编译器无法生成可执行文件。(RuntimeError)您必须先安装开发工具。的问题。 - kosiara - Bartosz Kosarzycki
2
我是一名Mac用户,已经安装并运行了Xcode。这对我来说很有效。 - KunYu Tsai
这为我安装了Ruby,还有libyaml。可能其中一个是最初的罪魁祸首。(我原本有Ruby 2.x.x,它安装了Ruby 3.1.2) - nycynik

18

如果您使用的是Mac,并且最近升级了Xcode工具,请运行以下命令:

sudo xcode-select --install
sudo xcodebuild -license

2
我在Mojave上成功地执行了您的步骤,但它们并没有帮助我解决“您必须先安装开发工具”的错误。 - Ryan

18

我的问题是我升级到了macOS Big Sur,但是命令行工具过时了。

我知道这一点是因为,当我试图运行brew install rbenv时,我收到了以下错误信息:

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/.

所以我运行了

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

并且在其完成后,宝石包已成功安装。

2
这个工作正常,没有删除CommandLineTools目录。谢谢! - ddavison
1
谢谢,这帮助我在 M1 Mac mini 上安装 CocoaPods。 - fullmoon

8
如果你使用的是Mac OS 10.14(莫哈韦),建议尝试以下方法:
cd  /Library/Developer/CommandLineTools/Packages/;
open macOS_SDK_headers_for_macOS_10.14.pkg

致谢Roy Huang

参考:https://dev59.com/hlsW5IYBdhLWcg3wwZcP#55732339

1
我在Mojave上成功执行了你的步骤,但它们并没有帮助我解决“您必须首先安装开发工具”的错误。 - Ryan

7

这个解决方案救了我:

  1. 通过终端安装homebrew

  2. 安装成功后,在终端中键入以下命令:

    '$ brew install cocoapods'

最后完成!

更新:您可以使用此命令安装homebrew

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

从这里开始(from here

请详细说明如何通过终端安装Homebrew!请逐步操作。 - Karolina Hagegård
1
无论如何,它做到了!(我使用了命令:/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 来自:https://brew.sh/。随意将其放入您的答案中!) - Karolina Hagegård
1
@KarolinaHagegård 谢谢,我已经将它添加到答案中了。 - M Karimi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接