安装 ruby 2.3.3/2.4.1

小于2.4的版本都按此方法进行
更新于: 2025-01-17 10:57:14

第一步: 安装  openssl@1.0

brew install rbenv/tap/openssl@1.0
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl@1.0/bin/c_rehash

openssl@1.0 is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl@1.0 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openssl@1.0/bin:$PATH"' >> ~/.profile

For compilers to find openssl@1.0 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.0/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.0/include"

For pkg-config to find openssl@1.0 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.0/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/openssl@1.0/1.0.2t: 1,787 files, 12.4MB, built in 7 minutes 40 seconds

以下是在 2025 年安装得到的信息 (Mac M2 Air)

A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /opt/homebrew/etc/openssl/certs

and run
  /opt/homebrew/opt/openssl@1.0/bin/c_rehash

openssl@1.0 is keg-only, which means it was not symlinked into /opt/homebrew,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl@1.0 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/openssl@1.0/bin:$PATH"' >> ~/.profile

For compilers to find openssl@1.0 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/openssl@1.0/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.0/include"

For pkg-config to find openssl@1.0 you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@1.0/lib/pkgconfig"
==> Summary
🍺  /opt/homebrew/Cellar/openssl@1.0/1.0.2u: 1,796 files, 12MB
==> Running `brew cleanup openssl@1.0`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

第二步: 安装 rbenv

brew install rbenv

第三步: 放一些临时的环境变量

确认路径正确
# 在此之前需要确认 /usr/local/opt/ 或者 /opt/homebrew/opt/ 这个路径下真实存在这个openssl@1.0
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.0)"
export PATH="$(brew --prefix openssl@1.0)/bin:$PATH"
export LDFLAGS="-L$(brew --prefix openssl@1.0)/lib"
export CPPFLAGS="-I$(brew --prefix openssl@1.0)/include"
export PKG_CONFIG_PATH="$(brew --prefix openssl@1.0)/lib/pkgconfig"

# 确认路径生效
echo $PATH
echo $LDFLAGS
echo $CPPFLAGS
echo $PKG_CONFIG_PATH

报错

解决方式是,使用较低的 gcc 编译器

  • 目前 MacAir m2 卡在这里,过不去。 可以跳过这一步,直接下一步。
../.././include/ruby/ruby.h:401:33: note: expanded from macro 'RB_ID2SYM'
  401 | #define RB_ID2SYM(x) (rb_id2sym(x))
      |                                 ^
date_parse.c:3096:2: note: ')' token is here
 3096 |         set_hash("offset", date_zone_to_diff(s[9]));
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
date_parse.c:40:49: note: expanded from macro 'set_hash'
   40 | #define set_hash(k,v) rb_hash_aset(hash, ID2SYM(rb_intern(k)), v)
      |                                                 ^~~~~~~~~~~~
../.././include/ruby/ruby.h:1737:51: note: expanded from macro 'rb_intern'
 1737 |         __extension__ (CONST_ID_CACHE((ID), (str))) : \
      |                                                   ^
../.././include/ruby/ruby.h:406:29: note: expanded from macro 'ID2SYM'
  406 | #define ID2SYM(x) RB_ID2SYM(x)
      |                             ^
../.././include/ruby/ruby.h:401:33: note: expanded from macro 'RB_ID2SYM'
  401 | #define RB_ID2SYM(x) (rb_id2sym(x))
      |                                 ^
494 warnings generated.
454 warnings generated.
linking shared-object date_core.bundle
make: *** [build-ext] Error 2
external command failed with status 2

1. 确认依赖环境

确保使用的依赖版本与 Ruby 2.3.3 兼容:

  • 确保安装了 openssl@1.0,并使用了正确的路径(已在之前说明)。
  • 检查是否使用了较新的编译器(如 Clang 或 GCC)。老旧的 Ruby 代码可能需要特定版本的编译器。

检查编译器版本

clang --version   # 或者 gcc --version

对于 Ruby 2.3.3,建议使用较老版本的编译器,如:

  • Clang 9 或更低版本
  • GCC 7 或更低版本

可以通过 Homebrew 安装旧版本编译器:

brew install gcc@7
export CC=$(brew --prefix gcc@7)/bin/gcc-7
# 我实际上尝试安装的是 gcc@11(因为7-10均无效)
export CC=$(brew --prefix gcc@11)/bin/gcc-11

2. 添加兼容性标志

尝试为编译器添加一些兼容性标志,避免过于严格的警告或错误。

设置以下环境变量后重试安装:

export CFLAGS="-Wno-error=implicit-function-declaration"
export CPPFLAGS="-I$(brew --prefix openssl@1.0)/include"
export LDFLAGS="-L$(brew --prefix openssl@1.0)/lib"
export PKG_CONFIG_PATH="$(brew --prefix openssl@1.0)/lib/pkgconfig"
rbenv install 2.3.3

Mac m1 安装发现有问题(gcc问题)

❯ gcc --version
Apple clang version 16.0.0 (clang-1600.0.26.4)
Target: arm64-apple-darwin23.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

~
$ p brew install gcc@7 --force
Warning: gcc@7 has been disabled because it is deprecated upstream! It was disabled on 2024-02-22.
gcc@7: This formula either does not compile or function as expected on macOS
versions newer than Monterey due to an upstream incompatibility.
gcc@7: The x86_64 architecture is required for this software.
Error: gcc@7: Unsatisfied requirements failed this build.

第四步: 安装 ruby2.3.3

RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.0)" rbenv install 2.3.3

看到下面就表示安装成功了

Installing ruby-2.3.3...

WARNING: ruby-2.3.3 is past its end of life and is now unsupported.
It no longer receives bug fixes or critical security updates.

ruby-build: using readline from homebrew
Installed ruby-2.3.3 to /Users/alo7.aric/.rbenv/versions/2.3.3

Mac m2(MacBook air)

# 使用这个(之前某些教程里使用的是 mysql-client)
brew install libffi

export LDFLAGS="-L/opt/homebrew/opt/libffi/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libffi/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/libffi/lib/pkgconfig"

# 确认安装过 openssl@1.0
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.0)"

rbenv install 2.3.3