Windows 10下Ruby安装Gem原生扩展失败求助
Hey there, let's work through this Ruby native extension headache on Windows 10 together! First, I'll recap what you've already tried to avoid repeating steps:
- Using a DevKit compatible with Ruby 2.3.3
- Running
devkitvars.batto set up environment variables - Installing GCC
- Attempting installs via
bundle installand directgem install <gem name>
Here are targeted steps to dig deeper and fix the issue:
1. Double-check DevKit binding to your Ruby installation
Even if you ran devkitvars.bat, sometimes the DevKit doesn't properly attach to your Ruby setup. Let's reset that:
- Close all open command prompts/terminals
- Open a new Administrator command prompt
- Navigate to your DevKit folder (e.g.,
C:\RubyDevKit) - Run
ruby dk.rb initto generate theconfig.ymlfile - Open
config.ymlin a text editor—make sure your Ruby 2.3.3 path is listed with forward slashes (like- C:/Ruby23-x64) - Run
ruby dk.rb installto bind DevKit to your Ruby instance - Re-run
devkitvars.batto reload environment variables
2. Confirm GCC version compatibility
Ruby 2.3.3 plays best with GCC 4.7.2 (the version bundled with the official RubyInstaller DevKit for 2.3.x). Newer GCC versions often cause compatibility issues:
- Run
gcc --versionin your prompt to check the version - If it's not 4.7.2, uninstall the standalone GCC you installed and rely on the one in your DevKit's
mingw\bindirectory—devkitvars.batshould already add this to your PATH
3. Force the gem installer to use DevKit's compiler
Sometimes the gem setup doesn't auto-detect the DevKit's GCC. Explicitly set the compiler paths before installing:
set CC=C:\RubyDevKit\mingw\bin\gcc.exe set CXX=C:\RubyDevKit\mingw\bin\g++.exe gem install <gem name>
(Replace C:\RubyDevKit with your actual DevKit installation path)
4. Add missing system library paths
Some native gems require additional headers/libraries (like OpenSSL, libxml2) that might not be in your default PATH. Add these paths manually:
set INCLUDE=C:\RubyDevKit\mingw\include;%INCLUDE% set LIB=C:\RubyDevKit\mingw\lib;%LIB%
Then retry the gem installation.
5. Update Bundler and get verbose error details
Outdated Bundler versions can mess with native extension builds on Windows. First update it:
gem update bundler
Then run bundle install with verbose output to see exactly where the compilation fails:
bundle install --verbose
The verbose logs will show specific errors (like missing headers or invalid flags) that point to the root problem.
6. Force source compilation with --platform=ruby
Sometimes Windows-specific gem builds are broken or unavailable. Compile the gem from source directly:
gem install <gem name> --platform=ruby
If none of these steps work, sharing the exact error message you see during installation would help narrow things down even further!
内容的提问来源于stack exchange,提问作者Thiago Lipovscek

