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

在fly.io部署ROR应用时修复`failed to load command: bootsnap`错误

修复fly.io部署Rails应用时的bootsnap加载失败问题

执行fly deploy时出现failed to load command: bootsnap (/rails/vendor/bundle/ruby/2.7.0/bin/bootsnap)错误,可尝试以下修复步骤:

1. 检查Gemfile中bootsnap的分组配置

如果你的Gemfile将bootsnap仅放在development或test组中(如下示例),生产环境部署时不会安装该gem,但如果config/boot.rb中仍尝试加载它,就会触发错误:

# 错误示例
gem 'bootsnap', require: false, group: [:development, :test]

修复方式:

  • 若生产环境不需要bootsnap,就在config/boot.rb中添加环境判断,仅在开发/测试环境加载;
  • 若生产环境需要bootsnap,移除group限制,或把它加入:production组:
gem 'bootsnap', require: false, group: [:development, :test, :production]

2. 修正Dockerfile中的bundle安装配置

检查Dockerfile中是否设置了BUNDLE_WITHOUT环境变量,排除了bootsnap所在的组:

# 错误示例:排除了development/test组,若bootsnap在这些组则不会安装
ENV BUNDLE_WITHOUT development:test

修复方式:

  • 若生产环境需要bootsnap,调整BUNDLE_WITHOUT,比如只排除test组:ENV BUNDLE_WITHOUT test;
  • 确保bundle install命令没有添加--without development test这类会跳过bootsnap安装的参数。

3. 调整config/boot.rb的加载逻辑

如果生产环境不需要bootsnap,修改config/boot.rb,仅在非生产环境加载bootsnap:

# 修改前
require 'bootsnap'
Bootsnap.setup(
  cache_dir: 'tmp/cache',
  development_mode: Rails.env.development?,
  load_path_cache: true,
  autoload_cache: true,
  compile_cache_iseq: true,
  compile_cache_yaml: true
)

# 修改后
unless Rails.env.production?
  require 'bootsnap'
  Bootsnap.setup(
    cache_dir: 'tmp/cache',
    development_mode: Rails.env.development?,
    load_path_cache: true,
    autoload_cache: true,
    compile_cache_iseq: true,
    compile_cache_yaml: true
  )
end

4. 清理Docker构建缓存后重新部署

旧的构建缓存可能导致依赖未正确更新,执行以下命令强制重新构建:

fly deploy --no-cache

5. 确认Ruby版本一致性

确保本地Ruby版本与Dockerfile中使用的Ruby版本完全一致(比如都是2.7.6),版本不匹配会导致bundle安装的gem路径出现差异,触发加载错误。

内容的提问来源于stack exchange,提问作者Aitzaz Ahmad

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 19:25:08