在Mac OSX Catalina上使用ruby-install安装Ruby 2.2

13

当我尝试安装Ruby 2.2时,我得到了以下结果:

$ ruby-install ruby 2.2.10
# ...
>>> Compiling ruby 2.2.10 ...
    CC = clang
    LD = ld
    LDSHARED = clang -dynamic -bundle
    CFLAGS = -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wshorten-64-to-32 -Wimplicit-function-declaration -Wdivision-by-zero -Wdeprecated-declarations -Wextra-tokens   -pipe
    XCFLAGS = -D_FORTIFY_SOURCE=2 -fstack-protector -fno-strict-overflow -fvisibility=hidden -DRUBY_EXPORT -fPIE
    CPPFLAGS = -I/opt/X11/include -I/usr/local/opt/openssl@1.1/include -I/usr/local/opt/readline/include -I/usr/local/opt/libyaml/include -I/usr/local/opt/gdbm/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT   -I. -I.ext/include/x86_64-darwin19 -I./include -I.
    DLDFLAGS = -Wl,-undefined,dynamic_lookup -Wl,-multiply_defined,suppress -L/usr/local/opt/openssl@1.1/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/libyaml/lib -L/usr/local/opt/gdbm/lib  -fstack-protector -Wl,-u,_objc_msgSend -Wl,-pie -framework CoreFoundation
    SOLIBS = -lgmp
Apple clang version 12.0.0 (clang-1200.0.31.1)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
translating probes probes.d
. ./vm_opts.h
file2lastrev.rb: does not seem to be under a vcs: .
make: [.revision.time] Error 1 (ignored)
./revision.h unchanged
compiling main.c
compiling dmydln.c
compiling miniinit.c
compiling miniprelude.c
compiling array.c
compiling bignum.c
compiling class.c
compiling compar.c
compiling complex.c
compiling dir.c
compiling dln_find.c
compiling encoding.c
encoding.c:825:2: error: implicit declaration of function 'rb_str_change_terminator_length' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
        rb_str_change_terminator_length(obj, oldtermlen, termlen);
        ^
1 error generated.
make: *** [encoding.o] Error 1
!!! Compiling ruby 2.2.10 failed!
  • 操作系统版本: 10.15.6
  • ruby-install 版本: 0.7.1

有什么解决或调试这个问题的想法吗?在升级操作系统之前,我以前能够安装其他版本的2.2。我也可以毫无问题地安装更高版本的Ruby 2.7。


遇到了同样的问题。我使用的是ruby 2.2.4和MacOS 10.15.5。我正在运行rvm pkg install openssl - Townsheriff
实际运行 - rvm reinstall 2.2.10 --with-openssl-dir=$HOME/.rvm/usr - Townsheriff
1
如果你想绕过这个问题,Travis CI有一个Ruby二进制文件列表(https://rubies.travis-ci.org/),其中包括2.2.4。 - max
6个回答

4

使用rbenv/ruby-build,这个方案对我有效,谢谢。 - keyvan
很不幸,当我尝试使用ruby-install安装Ruby 3.0.3时,这并没有起作用:它确实绕过了大部分错误,但是在某个时候,C代码尝试引用由其中一个隐式声明的函数返回的结构指针,而由于函数的返回类型已被隐式假定为int...是的,那行不通。 :( - Ilmari Karonen

4

我翻了很久才发现错误是由于使用 macOS 自带的 GCC 进行编译造成的,因此我决定进行一项测试。

由于旧版 Ruby 需要更新后的 make 中不再存在的指令,因此需要下载较旧版本的 Xcode 命令行工具。

首先,使用以下命令删除当前版本:

sudo rm -rf /Library/Developer/CommandLineTools

忽略有关缺少 git 的错误提示。

前往 https://developer.apple.com/download/more/ 并下载 11.x 版本的 Xcode 命令行工具。

我下载了版本为 11.5 的工具并使用 gcc -v 命令运行成功。

Configured with: --prefix = / Library / Developer / CommandLineTools / usr --with-gxx-include-dir = / Library / Developer / CommandLineTools / SDKs / MacOSX.sdk / usr / include / c ++ / 4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: / Library / Developer / CommandLineTools / usr / bin

与最近的命令行版本12.x相比,您会看到类似以下内容:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.2)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

2
安装11.x版本的命令行工具并没有解决我的问题。gcc -v 仍然显示来自12.x版本的输出。然而,下载并安装完整的Xcode 11.5包确实解决了这个问题。非常感谢您指引我朝着这个方向! - Arjen
奇怪。但是我没有完整的Xcode安装。我会更新我的回答。 - Júlio Campos

1

我能够使用rvm在Mac 10.15.7上安装该版本。

我已经使用homebrew安装了openssl@1.1。如果您运行brew info openssl,会看到什么?我建议先尝试brew upgrade openssl

尝试运行export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig",根据brew返回的信息进行操作。然后尝试rvm install 2.2.10。如果这不起作用,我会尝试rvm install 2.2.10 --with-openssl-dir=/usr/local/opt/openssl --with-openssl-lib=/usr/local/opt/openssl/lib --with-openssl-include=/usr/local/opt/openssl/include,只需确保这些路径对于您的系统是正确的。

您可能还需要事先运行

export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"

export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

这将由brew info提供。


设置 PKG_CONFIG_PATH 对我有用。具有讽刺意味的是,它似乎起作用是因为我的 OpenSSL 版本(3.0.1)太新了,所以 Ruby 构建完全跳过尝试安装 openssl 扩展,说:“需要 OpenSSL >= 1.0.1,< 3.0.0 或 LibreSSL >= 2.5.0。”但无论如何,现在我至少有一个(大部分)工作正常的 Ruby。:D - Ilmari Karonen

0

首先,安装 open-ssl

brew install rbenv/tap/openssl@1.0

那么

CFLAGS="-Wno-error=implicit-function-declaration" rvm reinstall ruby-2.2.5 --with-openssl-dir='/usr/local/opt/openssl@1.0'

0

在这里添加一些可能有用的额外选项:

我们需要正确的SSL版本:

$ brew install rbenv/tap/openssl@1.0

$ rvm install 2.2.10 -C --with-openssl-dir=`brew --prefix openssl@1.0`

0

我最近在Catalina 10.15.7中安装ruby-2.2.0时遇到了同样的问题,但幸运地成功完成了工作。步骤如下:

  1. 安装rvm包openssl:

    $ rvm pkg install openssl
  2. 忽略编译器警告:

    $ export optflags="-Wno-error=implicit-function-declaration"
  3. 安装所需的ruby版本(在我的情况中是2.2.0),同时提供openssl目录参数并忽略rubygems:

    $ rvm install 2.2.0 --with-openssl-dir=$rvm_path/usr --rubygems ignore

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