Mac环境Ruby 2.6.0部署Heroku时执行rake db:migrate遇psych缺失错误
Hey there, let's work through this psych gem issue you're facing when running heroku run rake db:migrate. That error about missing 2.5/psych.so even though you're using Ruby 2.6.0 is super frustrating—here are the most effective fixes I've used for similar problems:
1. Lock Your Ruby Version in the Gemfile
Heroku might be defaulting to a different Ruby version (like 2.5.x) even if you're using 2.6.0 locally. Force it to match your local environment by adding this line at the top of your Gemfile:
ruby '2.6.0'
Then run:
bundle install git add Gemfile Gemfile.lock git commit -m "Lock Ruby version to 2.6.0" git push heroku main
After redeploying, try running the migrate command again:
heroku run rake db:migrate
2. Purge Heroku's Bundler Cache
Old cached gems (compiled for Ruby 2.5) might be causing conflicts. Clear the cache with:
heroku repo:purge_cache -a your-app-name
Replace your-app-name with your actual Heroku app name. Then redeploy your code and re-run the migrate command.
3. Reinstall Ruby Locally with libyaml (and Update Gems)
The warning about missing libyaml means your local Ruby 2.6.0 wasn't compiled with proper YAML support. Fix this on your Mac:
- First install libyaml via Homebrew:
brew install libyaml - Uninstall and reinstall Ruby 2.6.0 (adjust commands if you use rvm instead of rbenv):
rbenv uninstall 2.6.0 rbenv install 2.6.0 - Reinstall your gems to ensure psych is built for the correct Ruby version:
bundle install --force
Commit the updated Gemfile.lock and redeploy to Heroku.
4. Verify Heroku's Buildpack
Make sure you're using the official Ruby buildpack, which handles gem compilation correctly:
Check your current buildpack:
heroku buildpacks -a your-app-name
If it's not heroku/ruby, set it with:
heroku buildpacks:set heroku/ruby -a your-app-name
Redeploy and test the migrate command again.
Why This Happens
The error occurs because Heroku's gem bundle is trying to load a psych version compiled for Ruby 2.5, but your app is running on Ruby 2.6.0. The mismatched paths (/app/vendor/bundle/ruby/2.5.0/...) confirm that the gem environment isn't aligned with your Ruby version. The fixes above ensure both local and Heroku environments use the same Ruby version and properly compiled gems.
内容的提问来源于stack exchange,提问作者Victoria

