安装mechanize gem报错,已装ruby-dev和ruby-make仍失败如何解决?
Hey there, let's work through this issue together—you mentioned you already installed ruby-dev and ruby-make, but still hit that missing header file error when running sudo gem install mechanize. Let's break down actionable fixes:
First, let's recap the error you're seeing for clarity:
sudo gem install mechanize
Building native extensions. This could take a while...
ERROR: Error installing mechanize:
ERROR: Failed to build gem native extension.current directory: /var/lib/gems/2.4.0/gems/unf_ext-0.0.7.5/ext/unf_ext
/usr/bin/ruby2.4 -r ./siteconf20180417-8728-15wdd1s.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h
extconf failed, exit code 1
Common Fixes to Try
Install the exact
ruby-devversion matching your Ruby runtime
The error shows you're using Ruby 2.4.0, but the genericruby-devpackage might be linked to a different Ruby version. Run this to get the correct version:sudo apt-get install ruby2.4-devAfter installing, retry
sudo gem install mechanize.Verify the Ruby header file path
Check where your Ruby expects header files to live with this command:ruby -r rbconfig -e 'puts RbConfig::CONFIG["rubyhdrdir"]'Navigate to that directory and confirm
ruby.hexists. If it doesn't, theruby2.4-devpackage wasn't installed properly—try reinstalling it or checking for package corruption.If using Ruby version managers (rbenv/rvm), use their built-in tools
System packages likeruby-devdon't apply to Ruby versions managed by rbenv or rvm. For rbenv:rbenv install --patch 2.4.0For rvm:
rvm reinstall 2.4.0This ensures the necessary header files are included with your managed Ruby version.
Fix environment variable gaps
A missingPKG_CONFIG_PATHcan break header detection. Export the path for your system architecture (adjust to/usr/lib/i386-linux-gnu/pkgconfigif on 32-bit):export PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig sudo gem install mechanize
内容的提问来源于stack exchange,提问作者Sam

