macos 安装: rbenv/openssl问题

在 macos 上安装 rbenv
更新于: 2024-10-10 08:45:00

安装

# 安装
brew install rbenv
# 版本
❯ rbenv --version
rbenv 1.2.0

国内快速安装

源地址: https://gitee.com/RubyKids/rbenv-cn

# 安装
bash -c "$(curl -fsSL https://gitee.com/RubyKids/rbenv-cn/raw/main/tools/install.sh)"
# 卸载
bash -c "$(curl -fsSL https://gitee.com/RubyKids/rbenv-cn/raw/main/tools/uninstall.sh)"
# 检测,新开 terminal(mac-settings中已经包含相关的设置)
rbenv --version

列出最近流行版本

rbenv install --list

下载对应版本到缓存

  • 下载
  • 安装
## prepare package
mkdir -p ~/.rbenv/cache
cd ~/.rbenv/cache

## 2.6.x | 3.1.2
wget https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.10.tar.bz2
wget https://cache.ruby-lang.org/pub/ruby/3.1/ruby-3.1.2.tar.gz
wget https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.5.tar.bz2

## install<cwd: ~/.rbenv/cache>
rbenv install 2.6.10

设置全局版本

# 设置
rbenv global 2.6.10
# 可能要新开 terminal tab
ruby --version

更新 rbenv 部分组件

我想安装 2.6.10 的时候(另一台电脑),发现 ruby-build: definition not found: 2.6.10,这个时候就可以用到这个功能了。

尴尬了: 还是没有,todo 吧,等后面必须要解决这个问题再考虑解决办法吧

brew update && brew upgrade ruby-build
❯ rbenv install --list
2.6.9
2.7.5
3.0.3
3.1.0
jruby-9.3.2.0
mruby-3.0.0
rbx-5.0
truffleruby-21.3.0
truffleruby+graalvm-21.3.0

Only latest stable releases for each Ruby implementation are shown.
Use 'rbenv install --list-all / -L' to show all local versions.

openssl@1.1

在安装 2.6.5的时候,会出现无法安装成功的报错,发现默认 openssl@3 的版本

brew link openssl@1.1
# 如果失败,考虑这个命令
brew link --overwrite openssl@1.1

Warning: Refusing to link macOS provided/shadowed software: openssl@1.1
If you need to have openssl@1.1 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.profile

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

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

安装的时候,在当前 terminal 添加这几句

export PATH="/usr/local/opt/openssl@1.1/bin:$PATH";
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib";
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include";
export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig";

一个报错的处理

  • 原因分析
  • 根本问题在这里 ~/.rbenv 里的版本不对应
  • 解决办法:删除 ~/.rbenv 目录,然后重装 rbenv 即可解决问题

/Users/aric.zheng/.rbenv/shims/ruby: line 21: /usr/local/Cellar/rbenv/1.1.2/libexec/rbenv: No such file or directory

vim /Users/aric.zheng/.rbenv/shims/ruby

# ... 发现这一行
exec "/usr/local/Cellar/rbenv/1.1.2/libexec/rbenv" exec "$program" "$@"
# 改成
exec "/usr/local/Cellar/rbenv/1.2.0/libexec/rbenv" exec "$program" "$@"

发现原因是 ~/.rbenv 目录删除,重新安装 rbenv 一切问题都会消失

rm -rf ~/.rbenv
brew install rbenv
rbenv install 2.6.5

还有其它报错

brew install mysql pkg-config

mysql-client 报错

checking whether CFLAGS is valid... yes
checking whether LDFLAGS is valid... no
configure: error: something wrong with LDFLAGS="-L/usr/local/opt/mysql-client/lib"
external command failed with status 1

解决办法,安装 mysql-client 即可

有可能不同的系统,路径不同,可以通过: brew list mysql-client 找到路径。

brew install mysql-client

# ---- 下面的不确定要不要加 ---- 
mysql-client is keg-only, which means it was not symlinked into /usr/local,
because it conflicts with mysql (which contains client libraries).

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

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

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

安装成功的

经过上面的操作,只需要  brew install mysql-client  即可,其它的环境变量,默认已经有了(有可能自己的包处理的,有可能自动 link 掉了)

zlib link 报错: linking shared-object zlib.bundle

2 warnings generated.
linking shared-object -test-/string.bundle
6 warnings generated.
linking shared-object psych.bundle
4 warnings generated.
linking shared-object zlib.bundle
make: *** [build-ext] Error 2
external command failed with status 2

解决方案如下

# 这一行是最关键的(应该是M1上遇到的问题)
export RUBY_CFLAGS="-Wno-error=implicit-function-declaration"
export RUBY_CONFIGURE_OPTS="--with-zlib-dir=$(brew --prefix zlib) --with-openssl-dir=$(brew --prefix openssl@1.1) --with-readline-dir=$(brew --prefix readline) --with-libyaml-dir=$(brew --prefix libyaml) --with-gdbm-dir=$(brew --prefix gdbm)"

# 其它比较有价值的资料
# As I have described in this blog post of mine, you need to specify the path to the version of OpenSSL that your version of Ruby requires:

Ruby 3.1 and newer, use:

RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@3)" rbenv install 3.1.0
Ruby 2.4 to 3.0, use:

RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)" rbenv install 2.4.0
Ruby 2.3 and older, use

RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1)" rbenv install 2.3.0