A cross-platform script that builds Ruby gems with C extensions using Docker, creating precompiled versions compatible with modern systems.
Many legacy Ruby gems with C extensions fail to compile on modern macOS due to:
- Compiler compatibility issues
- Missing dependencies
- Architecture mismatches (ARM64 vs x86_64)
- Outdated build tools
This script uses Docker with Ruby 2.5 to:
- Compile gems in a controlled Linux environment
- Create precompiled versions that bypass compilation
- Make gems work with both
gem install
andbundle install
- Support any Ruby gem with C extensions
- Docker (with ARM64 support on Apple Silicon)
- Ruby installed locally
- Bash shell
# Make executable
chmod +x build_ruby_gem.sh
# Build any gem with C extensions
./build_ruby_gem.sh <gem_name> <gem_version>
# Legacy gems that often fail on modern macOS
./build_ruby_gem.sh semacode-ruby19 0.7.4
./build_ruby_gem.sh nokogiri 1.13.10
./build_ruby_gem.sh nio4r 2.3.1
./build_ruby_gem.sh mysql2 0.5.4
./build_ruby_gem.sh pg 1.4.5
./build_ruby_gem.sh sqlite3 1.6.0
- Downloads the gem source in Docker
- Compiles C extensions with compatible toolchain
- Removes extension source files (prevents recompilation)
- Packages precompiled gem with
.so
files - Installs locally for immediate use
- Gem installed and ready to
require
- Works with Bundler (no compilation attempts)
- Precompiled gem saved in
./precompiled/
- Legacy Rails applications with old gem dependencies
- macOS development with problematic C extensions
- CI/CD pipelines needing reliable gem builds
- Apple Silicon compatibility for x86_64 gems
Note: This creates architecture-specific builds. Run on the target platform or use multi-arch Docker builds for cross-platform compatibility.