Error installing mysql2 Failed to build gem native extension
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
- RedHat/CentOS:
- macOS: Use Homebrew to install the MySQL client libraries:
Make sure that the Homebrew installed paths are correctly configured:
Step 2: Ensure Version Compatibility
Check the installed Ruby and mysql2 gem versions to ensure compatibility. Upgrade the gem or Ruby as needed:
- Ensure that you have an appropriate version of the
mysql2gem specified in yourGemfile:
Step 3: Verify Compiler Toolchain
Make sure you have a fully functioning compiler toolchain. Ruby is usually compiled using GCC, so ensure that it is installed:
- For macOS: Install Xcode Command Line Tools:
- For Linux: Check for GCC:
Step 4: Address OpenSSL Conflicts
If OpenSSL mismatches are the cause, consider recompiling Ruby to use the correct OpenSSL version:
This command assumes the use of rbenv for Ruby installation and ensures it uses the OpenSSL version installed via Homebrew.
Additional Debugging Techniques
If issues persist, explore the following:
Consult Log Files
When you run into errors during gem install, consult the detailed log output. Adding verbosity can often help narrow down the issue:
Use Docker for Isolation
If multiple dependencies become cumbersome, consider using Docker to create an isolated environment. Docker images can provide a consistent system configuration, sparing you the hassle of managing multiple dependencies.
Summary of Key Points
Below is a summary of solutions to resolve the "Failed to build gem native extension" error:
| Issue | Solution |
| Missing MySQL Development Files | Install libmysqlclient-dev or equivalent |
| Version Incompatibility | Use compatible Ruby and gem versions |
| Incompatible Compiler Tools | Ensure GCC and build tools are installed |
| OpenSSL Version Mismatch | Recompile Ruby with correct OpenSSL version |
| Debugging | Use logs and Docker for problem isolation |
By addressing these factors, you can resolve the common issues encountered when installing the mysql2 gem and ensure a smooth development experience.
Related reading
- Error related to only_full_group_by when executing a query in MySql
- Error renaming a column in MySQL
- Error starting yugabyte as per shown in docs
- Error Tablespace for table xxx exists. Please DISCARD the tablespace before IMPORT
- ERROR update or delete on table tablename violates foreign key constraint
- Error while trying to configure ArangoDB replication
- Escaping single quote in PHP when inserting into MySQL
- Event Sourcing With an Event Store and an ORM

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.