Ubuntu 20.04下Ruby 2.5.3安装json 1.8.3 gem执行bundle install失败怎么办
这个错误是版本不兼容导致的:json 1.8.3发布时间远早于Ruby 2.4,而Ruby 2.4版本正式将原先的Fixnum、Bignum两个整数类型合并为Integer,移除了rb_cFixnum、rb_cBignum两个底层常量,你当前使用的Ruby 2.5.3不存在这两个常量,因此原生扩展编译失败。
按优先级从高到低排列:
方案1:升级json gem(最推荐)
json 1.8.3存在已知安全漏洞,优先升级到兼容高版本Ruby的稳定版。修改项目Gemfile中json的版本限制为:gem 'json', '~> 2.6'执行以下命令更新依赖即可:
bundle update json方案2:降级Ruby版本(适用于必须使用json 1.8.3的旧项目)
json 1.8.3最高兼容Ruby 2.3系列,可通过RVM切换到兼容的Ruby版本:rvm install 2.3.8 rvm use 2.3.8 bundle install方案3:手动加编译参数临时兼容(临时应急用,不推荐长期使用)
如果既不能升级json也不想切换Ruby版本,可通过编译参数手动将缺失的常量映射为现有Integer常量:
单独安装json时执行:gem install json -v '1.8.3' -- --with-cflags="-fpermissive -Drb_cFixnum=rb_cInteger -Drb_cBignum=rb_cInteger"
如果要让bundle安装时自动生效,先执行全局配置:bundle config build.json --with-cflags="-fpermissive -Drb_cFixnum=rb_cInteger -Drb_cBignum=rb_cInteger"
再执行bundle install即可。
内容的提问来源于stack exchange,提问作者user447141

