部署Rails应用到Heroku时遇Bundler安装Gem失败问题求助
Rails部署Heroku时Bundler安装Gem失败问题解决
问题描述
尝试将Rails应用部署到Heroku,执行git push heroku main命令时收到错误:
Failed to install gems via Bundler.
Gemfile内容
source "https://rubygems.org" git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby "3.1.2" gem 'will_paginate', '~> 3.3.1' # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" gem "rails", "~> 7.0.3", ">= 7.0.3.1" # The original asset pipeline for Rails gem "sprockets-rails" # Use the Puma web server gem "puma", "~> 5.0" # Use JavaScript with ESM import maps gem "importmap-rails" # Hotwire's SPA-like page accelerator gem "turbo-rails" # Hotwire's modest JavaScript framework gem "stimulus-rails" # Build JSON APIs with ease gem "jbuilder" # Use Redis adapter to run Action Cable in production gem "redis", "~> 4.0" # Use Active Model has_secure_password gem "bcrypt", "~> 3.1.7" # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] # Reduces boot times through caching; required in config/boot.rb gem "bootsnap", require: false # Use Sass to process CSS # gem "sassc-rails" # Use Active Storage variants # gem "image_processing", "~> 1.2" group :development, :test do # Use sqlite3 as the database for Active Record gem "sqlite3", "~> 1.4" # Debugging gem gem "debug", platforms: %i[ mri mingw x64_mingw ] end group :development do # Use console on exceptions pages gem "web-console" # Add speed badges # gem "rack-mini-profiler" # Speed up commands on slow machines / big apps # gem "spring" end group :test do # Use system testing gem "capybara" gem "selenium-webdriver" gem "webdrivers" end group :production do gem "pg" end
完整部署日志
git push heroku main Enumerating objects: 662, done. Counting objects: 100% (662/662), done. Delta compression using up to 8 threads Compressing objects: 100% (628/628), done. Writing objects: 100% (662/662), 80.33 KiB | 6.18 MiB/s, done. Total 662 (delta 376), reused 0 (delta 0), pack-reused 0 remote: Compressing source files... done. remote: Building source: remote: remote: -----> Building on the Heroku-22 stack remote: -----> Determining which buildpack to use for this app remote: -----> Ruby app detected remote: -----> Installing bundler 2.3.10 remote: -----> Removing BUNDLED WITH version in the Gemfile.lock remote: -----> Compiling Ruby/Rails remote: -----> Using Ruby version: ruby-3.1.2 remote: -----> Installing dependencies using bundler 2.3.10 remote: Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4 remote: Your bundle only supports platforms ["arm64-darwin-21"] but your local platform remote: is x86_64-linux. Add the current platform to the lockfile with remote: `bundle lock --add-platform x86_64-linux` and try again. remote: Bundler Output: Your bundle only supports platforms ["arm64-darwin-21"] but your local platform remote: is x86_64-linux. Add the current platform to the lockfile with remote: `bundle lock --add-platform x86_64-linux` and try again. remote: remote: ! remote: ! Failed to install gems via Bundler. remote: ! remote: ! Push rejected, failed to compile Ruby app. remote: remote: ! Push failed remote: Verifying deploy... remote: remote: ! Push rejected to arcane-island-23619. remote: To https://git.heroku.com/arcane-island-23619.git ! [remote rejected] main -> main (pre-receive hook declined)
解决方案
日志明确显示问题:你的Gemfile.lock仅包含本地开发环境的平台(arm64-darwin-21,即M系列Mac系统),但Heroku使用x86_64-linux平台,Bundler找不到对应平台的gem导致安装失败。按以下步骤修复:
- 在本地项目根目录运行命令,添加Heroku所需平台到lock文件:
bundle lock --add-platform x86_64-linux - 提交更新后的
Gemfile.lock到Git:git add Gemfile.lock git commit -m "Add x86_64-linux platform to Gemfile.lock" - 重新推送到Heroku:
git push heroku main
如果后续有跨平台部署需求,可同时添加通用Ruby平台提升兼容性:
bundle lock --add-platform ruby
内容的提问来源于stack exchange,提问作者Zachary C.
相关产品推荐
相关产品推荐

