Install Ruby on Rails on Windows with ‘gem’ behind a corporate proxy

Sometimes on a corporate network, you’ll be stuck behind a proxy and working away on a Windows box. Trying to install or update gems like Ruby on Rails with commands like

gem install rails

can give you strange errors. You’ll see various things at various points of bashing your head against the wall. Things like

ERROR: While executing gem ... (Gem::RemoteFetcher::FetchError)
       bad response Forbidden 403 (http://rubygems.org/latest_specs.4.8.gz)

or

ERROR: Could not find a valid gem 'rails' () = 0) in any repository
ERROR: While exectung gem ... (Gem::RemoteFetcher::FetchError)
       bad response Forbidden 403 (http://rubygems.org/latest_specs.4.8.gz)

O hell. How do I fix this? You think.

Personally, I tried calling the inhouse IT department to get whatever was being blocked unblocked (I know very little about networking so don’t ask me specifics here). They couldn’t really help me out for some reason. So instead I grabbed a copy of ntlmaps which basically allows you to create and run a proxy on your local machine that will grab all requests and re-package them as if they were coming from IE, thus allowing the communication to go ahead.

ntlmaps is a neat little python utility so you’ll need to get that on your ‘puter. Then pop the ntlmaps into a nice easy to find directory and read the Install.txt and readme.txt to get a little background and basic info. I went ahead and changed my server.cfg file filling in the following bits:

NT_DOMAIN:superCorp
USER:calvin
PASSWORD:hobbes
LM_PART:1
NT_PART:0

Now I’ve got a nice little local proxy ready to run – give it the old runserver.bat at the command line – and wait for it to fire up. You should get something like

NTLM authorization Proxy Server v...blahblahblah
Now listening at COMPUTERNAME on port 5865

obviously, COMPUTERNAME is going to be your computer name.
Now, fire up a new command shell and type in

SET HTTP_PROXY=http://COMPUTERNAME:5865

You’ll get no response from that command but don’t worry. If you want to verify the setting of the HTTP_PROXY variable, simply type

SET HTTP_PROXY

At last, you can try updating the gem again:

gem update rails

And you should be good to go