You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

macOS Sierra下Rails应用从SQLite转MySQL时mysql gem安装失败

Hey, I’ve dealt with this exact native extension error when installing the mysql gem on macOS Sierra while migrating Rails apps from SQLite to MySQL. Let’s walk through how to fix it step by step.

Fixing "Failed to build gem native extension" for the mysql gem

This error almost always happens because the mysql gem can’t find MySQL’s development libraries (header files and configuration tools) needed to compile its native code. Here’s how to resolve it:

1. Install Xcode Command Line Tools (if missing)

First, make sure you have the basic build tools required to compile native extensions. Run this in your terminal:

xcode-select --install

Follow the on-screen prompts to complete the installation—this gives you the compilers and utilities needed for gem builds.

2. Set up MySQL via Homebrew

macOS doesn’t come with MySQL pre-installed, so we’ll use Homebrew to get the necessary libraries.

  • If you don’t have Homebrew installed, run this official installation command first:
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  • If you previously installed mysql-connector-c, it might conflict with the full MySQL package—uninstall it first:
    brew uninstall mysql-connector-c
    
  • Install the full MySQL package:
    brew install mysql
    

3. Install the mysql gem with explicit configuration path

The gem installer often can’t automatically locate the mysql_config tool from Homebrew’s installation. We’ll point it directly to the correct path:

gem install mysql -- --with-mysql-config=$(brew --prefix mysql)/bin/mysql_config

This command tells the gem exactly where to find the configuration file it needs to build the native extension successfully.

4. Verify the installation

Once the command finishes without errors, confirm the gem is installed:

gem list mysql

You should see mysql (2.9.1) (or your installed version) in the output.

Quick note for RVM users

Make sure your RVM environment is loaded before installing the gem to ensure it’s added to your current Ruby setup. Reload RVM if needed:

source ~/.rvm/scripts/rvm

内容的提问来源于stack exchange,提问作者Amer Bearat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:25:04