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

使用RVM时bundle install调用错误Ruby,原生扩展构建失败如何解决?

解决Bundle调用系统Ruby而非RVM Ruby导致的gem编译失败问题

我之前也碰到过一模一样的问题!你的情况很明确:RVM管理的Ruby路径是对的,但Bundle偏偏调用了系统的Ruby,导致编译unf_ext这类native扩展时找不到正确的头文件。试试下面这几个步骤,应该能解决:

1. 确保RVM环境在当前shell中正确加载

有时候终端启动时没加载RVM的脚本,导致环境变量没生效。先手动加载一次:

source ~/.rvm/scripts/rvm

加载完成后,重新运行bundle install试试。

2. 明确指定RVM的Ruby环境和gemset

直接在终端里切换到你要用的环境:

rvm use ruby-2.3.4@junk-drawer

这一步会确保当前会话的Ruby、Gem等工具都来自RVM的指定版本,之后再执行bundle install应该就会用正确的Ruby了。

3. 检查并重新安装RVM环境下的Bundler

如果你的bundle命令还是指向系统路径(可以用which bundle查看),说明之前是用系统Ruby安装的Bundler。需要在RVM环境下重新安装:

gem install bundler

安装完成后再运行which bundle,应该会显示类似/home/roger/.rvm/gems/ruby-2.3.4@junk-drawer/bin/bundle的路径,这就对了。

4. 给项目添加RVM环境配置(可选但推荐)

在你的项目根目录下创建一个.rvmrc文件,内容如下:

rvm use ruby-2.3.4@junk-drawer

下次进入这个项目目录时,RVM会自动切换到指定的Ruby环境,避免每次都手动切换。第一次进入时终端会提示你信任这个文件,确认即可。

你的报错信息回顾

你遇到的具体报错是因为Bundle调用了系统的/usr/bin/ruby2.3,而不是RVM的Ruby:

$ bundle
Fetching gem metadata from https://rubygems.org/..........
Fetching version metadata from https://rubygems.org/..
Resolving dependencies...
Installing unf_ext 0.0.7.4 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /home/roger/.rvm/gems/ruby-2.3.4@junk-drawer/gems/unf_ext-0.0.7.4/ext/unf_ext
/usr/bin/ruby2.3 -r ./siteconf20180115-9722-1iow75n.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h
extconf failed, exit code 1
Gem files will remain installed in /home/roger/.rvm/gems/ruby-2.3.4@junk-drawer/gems/unf_ext-0.0.7.4 for inspection.
Results logged to /home/roger/.rvm/gems/ruby-2.3.4@junk-drawer/extensions/x86_64-linux/2.3.0/unf_ext-0.0.7.4/gem_make.out
Installing mime-types-data 3.2016.0521
Installing netrc 0.11.0
Using bundler 1.11.2
An error occurred while installing unf_ext (0.0.7.4), and Bundler cannot continue.
Make sure that `gem install unf_ext -v '0.0.7.4'` succeeds before bundling.

内容的提问来源于stack exchange,提问作者Roger Lipscombe

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:13:36