Apple Silicon设备通过mysql2 gem配置Rails+MySQL运行环境问题求助
Apple Silicon 设备 Rails 环境 mysql2 gem 安装解决方案
核心报错原因
ld: library not found for -lssl 是编译mysql2时无法自动识别Apple Silicon架构下Homebrew安装的openssl、mysql库路径导致的链接失败。
前置操作
先确认已通过原生arm64终端(执行arch命令输出arm64即为原生环境,不要用Rosetta转译的终端)安装依赖:
brew install mysql openssl # 若使用mysql2 0.4.x及更早版本,需兼容openssl1.1,执行下方命令替代 # brew install mysql openssl@1.1
安装方案
方案1:单独安装指定版本mysql2 gem
将下方命令中的x.x.x替换为你需要的mysql2版本号(Rails 5.2.x通常搭配mysql2 0.5.x版本):
gem install mysql2 -v 'x.x.x' -- --with-opt-dir="$(brew --prefix openssl)" --with-mysql-dir="$(brew --prefix mysql)" # 若安装的是openssl@1.1,替换openssl路径即可: # gem install mysql2 -v 'x.x.x' -- --with-opt-dir="$(brew --prefix openssl@1.1)" --with-mysql-dir="$(brew --prefix mysql)"
方案2:Rails项目全局配置(推荐)
进入Rails项目根目录执行配置,后续bundle install会自动读取参数:
# 项目级配置,仅对当前项目生效 bundle config set --local build.mysql2 "--with-opt-dir=$(brew --prefix openssl) --with-mysql-dir=$(brew --prefix mysql)" # 若用openssl@1.1则替换为对应路径 # bundle config set --local build.mysql2 "--with-opt-dir=$(brew --prefix openssl@1.1) --with-mysql-dir=$(brew --prefix mysql)" # 执行安装 bundle install
版本适配说明
你使用的Ruby 2.5.3、Rails 5.2.3环境可正常运行,若安装mysql2 0.5.3版本时出现openssl兼容性问题,切换为openssl@1.1即可正常编译。
内容的提问来源于stack exchange,提问作者Eskim0
相关产品推荐
相关产品推荐

