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

创建Rails应用时出现cannot load such file --json (LoadError)错误求助

Rails新建应用时出现cannot load such file -- json (LoadError)的解决方案

问题场景

在Linux系统执行rails new app_name命令创建Rails应用,bundle依赖安装完成后,执行importmap:install和turbo:install stimulus:install时触发如下错误:

Fetching gem metadata from https://rubygems.org/...........
Resolving dependencies...
Bundle complete! 14 Gemfile dependencies, 81 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
         run  bundle lock --add-platform=x86_64-linux
Writing lockfile to /home/isaac/git/frameworks/ruby/rails_blog/test_app/Gemfile.lock
         run  bundle binstubs bundler
       rails  importmap:install
/usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in `require': cannot load such file -- json (LoadError)
        from /usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in `require'
        from /usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap/compile_cache/json.rb:49:in `init!'
        from /usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap/compile_cache/json.rb:42:in `install!'
        from /usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap/compile_cache.rb:34:in `setup'
        from /usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap.rb:68:in `setup'
        from /usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap.rb:112:in `default_setup'
        from /usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap/setup.rb:5:in `<top (required)>'
        from /home/isaac/git/frameworks/ruby/rails_blog/test_app/config/boot.rb:4:in `require'
        from /home/isaac/git/frameworks/ruby/rails_blog/test_app/config/boot.rb:4:in `<top (required)>'
        from bin/rails:3:in `require_relative'
        from bin/rails:3:in `<main>'
       rails  turbo:install stimulus:install
/usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in `require': cannot load such file -- json (LoadError)
        from /usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in `require'
        from /usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap/compile_cache/json.rb:49:in `init!'
        from /usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap/compile_cache/json.rb:42:in `install!'
        from /usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap/compile_cache.rb:34:in `setup'
        from /usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap.rb:68:in `setup'
        from /usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap.rb:112:in `default_setup'
        from /usr/lib/ruby/gems/3.0.0/gems/bootsnap-1.18.3/lib/bootsnap/setup.rb:5:in `<top (required)>'
        from /home/isaac/git/frameworks/ruby/rails_blog/test_app/config/boot.rb:4:in `require'
        from /home/isaac/git/frameworks/ruby/rails_blog/test_app/config/boot.rb:4:in `<top (required)>'
        from bin/rails:3:in `require_relative'
        from bin/rails:3:in `<main>'

解决步骤

1. 安装系统级Ruby开发依赖

该错误通常是缺少编译Ruby原生扩展的依赖导致,json gem需要这些依赖才能正常安装。根据你的Linux发行版执行对应命令:

  • Debian/Ubuntu系:
    sudo apt-get update && sudo apt-get install ruby-dev
    
  • RHEL/CentOS系:
    sudo yum install ruby-devel
    

2. 重新安装json gem

执行命令手动安装json gem:

gem install json

3. 重新安装项目依赖

进入新建的应用目录,重新执行bundle安装:

cd app_name
bundle install

4. 尝试跳过默认组件创建应用

如果上述方法无效,可以创建应用时跳过importmap、turbo和stimulus,先搭建基础应用再按需添加:

rails new app_name --skip-importmap --skip-turbo --skip-stimulus

5. 临时禁用Bootsnap测试

Bootsnap的缓存机制可能引发该问题,可临时禁用它:

  • 打开项目根目录下的config/boot.rb文件
  • 注释掉require 'bootsnap/setup'这一行
  • 再次尝试执行rails命令

内容的提问来源于stack exchange,提问作者isaac reyes

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 12:05:55