使用rbenv+Bundler启动Unicorn时无法加载指定Gem的问题
I’ve run into this exact frustration before with rbenv and Bundler—let’s break down the most likely causes and fixes for why your gems aren’t loading despite bundle show confirming they’re installed:
1. Unicorn might be using the wrong Ruby version (not your rbenv 1.9.3-p484)
Even if you’ve set global/local rbenv versions, sometimes the unicorn binary being executed isn’t the one from your rbenv shims. Here’s how to verify and fix this:
- Run
which unicorn—it should point to something like~/.rbenv/shims/unicorn. If it shows/usr/bin/unicornor another system-level path, that’s the issue. - Fix: Ensure
~/.rbenv/shimsis at the top of yourPATHvariable. Check withecho $PATH—if rbenv’s shims aren’t first, update your shell config (.bashrc,.zshrc) to addexport PATH="$HOME/.rbenv/shims:$PATH"and restart your terminal. Also, double-check you’re running commands from your project root (rbenv’s local version only applies to that directory).
2. Your GEM_HOME override is conflicting with Bundler’s isolated paths
Bundler creates a project-specific gem set (usually in vendor/bundle or a rbenv-managed directory) to keep dependencies isolated. Manually setting GEM_HOME tells Ruby to look for gems in a global path instead of Bundler’s dedicated folder.
- Check if
GEM_HOMEis set: Runecho $GEM_HOME. If it returns a path, that’s almost certainly interfering. - Fix: Temporarily unset it for the current session with
unset GEM_HOME, then trybundle exec unicornagain. If this works, remove theexport GEM_HOME=...line from your shell config—rbenv + Bundler handle gem paths automatically, so you don’t need this manual setup.
3. Your Gemfile.lock is out of sync or from a different environment
If you copied Gemfile.lock from another machine or environment, it might reference gem versions or paths that don’t match your local 1.9.3-p484 setup.
- Fix: Delete
Gemfile.lock, then runbundle installto generate a fresh lock file tailored to your current environment. Also, double-check thathaml-contribandsinatra-activerecordare explicitly listed in yourGemfile—Bundler only loads gems defined there, even if they’re installed locally.
4. rbenv’s shims haven’t been updated for new gems
Sometimes rbenv doesn’t refresh its shims after installing gems via Bundler, leading to outdated references.
- Fix: Run
rbenv rehashin your project directory, then try starting Unicorn again.
5. Permissions issues with Bundler’s gem directory
If your user doesn’t have read access to the gem files (e.g., they’re owned by root or another user), Bundler will fail to load them without clear feedback.
- Check permissions: Run
ls -la $(bundle show haml-contrib)to confirm your user has access. - Fix: If permissions are incorrect, run
sudo chown -R $USER:$USER ~/.rbenv(or the path to your Bundler gem directory) to take ownership of the files.
内容的提问来源于stack exchange,提问作者Danila Ladner

