Gem Install Hangs Indefinitely: A Quick Fix for RubyGems.org Connection Issues
I’ve encountered a situation where gem install
or bundle install
seems to hang forever. This can be particularly frustrating when setting up a new server or deploying an application. I’ll share my experience with this issue and provide a simple solution that worked for me.
The Problem
When setting up a new Ubuntu 22.04 server with the following stack:
- Ruby 3.3.0 (managed by rbenv)
- Standard Ubuntu 22.04 LTS installation
- Default network configuration
The server would hang indefinitely when trying to communicate with rubygems.org. This issue manifests in several ways:
gem install
commands never completebundle install
gets stuck at “Fetching source index from rubygems.org”- No error messages, just silent hanging
The Root Cause
The core issue stems from IPv6 connectivity problems when communicating with rubygems.org. While IPv6 is the future of internet addressing, not all network configurations handle it properly, leading to timeouts or hanging connections.
The Solution
The fix is surprisingly simple - disable IPv6 on your server. Here’s how to do it:
sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.lo.disable_ipv6=1
To make these changes permanent (survive reboots), add the following lines to /etc/sysctl.conf
:
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
Then apply the changes:
sudo sysctl -p