Introduction
Bundler throws "Could not find gem X in any of the sources" when it cannot resolve a gem from the configured sources (rubygems.org, private gem servers, or git repositories). This can happen due to network issues, platform incompatibility, Ruby version requirements, or the gem being yanked from the registry.
This error blocks application deployment and is particularly common when setting up new development environments or deploying to production servers.
Symptoms
- bundle install fails with "Could not find gem 'pg (~> 1.4)' in any of the gem sources"
- Error occurs on a specific gem while others install fine
- Same Gemfile works on another machine or Ruby version
Common Causes
- Gem has been yanked (removed) from RubyGems
- Ruby version does not meet the gem's requirements
- Platform-specific gem (native extension) is not available for the current OS/architecture
- Network issues or gem source is unreachable
Step-by-Step Fix
- 1.Verify the gem exists and is compatible: Check the gem on RubyGems.
- 2.```bash
- 3.# Check if the gem exists on RubyGems:
- 4.gem search pg --remote
# Check available versions: gem search pg --remote --all
# Check Ruby version requirement: gem specification pg required_ruby_version ```
- 1.Fix platform-specific gem issues: Ensure native extensions can build.
- 2.```bash
- 3.# For pg gem (PostgreSQL), install development headers:
- 4.# Ubuntu/Debian:
- 5.sudo apt-get install -y libpq-dev
# macOS: brew install postgresql
# Then retry: bundle install
# For platform-specific gems, check your platform: ruby -e "puts Gem::Platform.local" ```
- 1.Clear Bundler cache and retry: Remove cached data that may be stale.
- 2.```bash
- 3.# Clear Bundler cache:
- 4.bundle clean --force
# Remove the lock file and reinstall (last resort): rm Gemfile.lock bundle install
# Or use a fresh cache: rm -rf ~/.bundle/cache bundle install ```
Prevention
- Pin gem versions explicitly in Gemfile to avoid surprise yanks
- Keep Gemfile.lock committed for reproducible builds
- Use platform-independent gems when possible
- Test bundle install on the target deployment platform before deploying