I’m writing this guide right after smashing my head on the desk for an hour and finally getting Blunder (the gem, not the feeling) to install on Windows. If you just typed
gem install blunder
and saw RubyGems explode with an ECONNREFUSED
message, walk with me. I’ll show you exactly what happened on my machine, the quick proof-and-fix steps, and a little helper script I now keep in every project so this never bites me again.
The One Line Command That Broke
The minute I ran gem install blunder
I got this:
: Could not find a valid gem 'bundler' (>= 0), here is why:
Unable to download data from https://rubygems.org/ -
Errno::ECONNREFUSED: Failed to open TCP connection
to 127.0.0.1:1080 (No connection could be made because the
target machine actively refused it.)
What the Error
Piece of the message | What it means in plain English |
---|---|
127.0.0.1:1080 | RubyGems thinks it must talk through a proxy running on my own PC on port 1080. |
ECONNREFUSED | Nothing is listening there, so Windows rejects the call before it even leaves the machine. |
/specs.4.8.gz | That’s the master index RubyGems grabs first; if the download fails everything else fails. |
Why Did Ruby Gems Look at 127.0.0.1:1080?
RubyGems hunts for proxy settings in four spots, in this order:
- Environment variables:
HTTP_PROXY
,HTTPS_PROXY
,http_proxy
,https_proxy
~/.gemrc
(or%USERPROFILE%\.gemrc
on Windows)- Bundler settings (if you run inside
bundle …
) - Windows “Internet Options” proxy settings
Months ago I tested a Shadowsocks client that shoved
HTTP_PROXY=http://127.0.0.1:1080
into my user variables. The client is long gone, but the variable stayed behind. When RubyGems saw it, it dutifully tried that socket, found nothing, and threw ECONNREFUSED
.
Fix The Error
# | Command / Action (Windows CMD) | What you should see |
---|---|---|
1 | set HTTP_PROXY | If the variable exists, the line shows up. |
2 | setx HTTP_PROXY "" then reopen CMD | The variable disappears from new shells. |
3 | gem sources --clear-all gem sources -a https://rubygems.org | Source list resets to the default secure endpoint. |
4 | gem install blunder | RubyGems reaches the real server and starts downloading. |
Tip: If you really need a proxy later, add it to a PowerShell profile or a batch script you run manually instead of keeping it global.
The Tiny Helper Correct: gemdoctor.rb
I don’t like surprises, so I wrote a 40-line Ruby script that checks my proxy, pings RubyGems, and prints a friendly hint instead of a stack trace.
#!/usr/bin/env ruby
require "net/http"
require "uri"
RUBYGEMS_HOST = "https://rubygems.org"
def current_proxy
ENV.values_at("https_proxy", "HTTPS_PROXY", "http_proxy", "HTTP_PROXY").compact.first
end
def rubygems_reachable?(proxy)
uri = URI.join(RUBYGEMS_HOST, "/specs.4.8.gz")
host = proxy ? URI(proxy).host : nil
port = proxy ? URI(proxy).port : nil
http = Net::HTTP.new(uri.host, uri.port, host, port)
http.use_ssl = true
http.open_timeout = 5
http.read_timeout = 5
http.start { |h| h.head(uri.request_uri).is_a?(Net::HTTPOK) }
rescue
false
end
proxy = current_proxy
puts "GemDoctor: proxy = #{proxy || 'none'}"
if rubygems_reachable?(proxy)
puts "✓ RubyGems reachable. You’re good: gem install blunder"
else
puts "✗ Can’t reach RubyGems."
if proxy
puts " Proxy #{proxy} looks dead."
puts " Run: setx HTTP_PROXY \"\" (Windows) or unset HTTP_PROXY (bash) and retry."
else
puts " Check your firewall or VPN."
end
end
How I Use It
ruby gemdoctor.rb
If the script prints a red “✗” I fix the proxy, rerun, and only then bother with gem install
.
Explain it
- Auto-clear bad proxies
Makegemdoctor.rb
prompt “Clear proxy for me? (y/n)” and, on y, runENV.delete
plussetx HTTP_PROXY ""
. - Pick the fastest mirror
Add a loop that pingshttps://gems.ruby-china.com
andhttps://mirrors.tuna.tsinghua.edu.cn/rubygems/
, measures response time, and suggests the quickest source. - Write a tiny log
Append each run (timestamp, proxy, reachable
) togemdoctor.log
so you can track flaky hotel Wi-Fi later.
You’ll practice Net::HTTP
, environment tinkering, and Ruby’s simplest file I/O skills that pop up in real projects all the time.
Final Thought
Nine out of ten “gem can’t download” headaches on Windows trace back to a stale proxy entry, not a broken gem or a buggy Ruby install. Keep the checklist and gemdoctor.rb
close, clear the variable, reset the source list, and watch installs fly. Your future self and every teammate who inherits your machine will thank you.