Run gem install behind a firewall in Windows
Posted by Marcus Wyatt on 18 March 2008
I’m contracting at a client running a Novell network and wanted to create some small little ruby scripts to automate some tasks for me. But I ran into problems using gem install behind their firewall. I’m getting the following error:
ERROR: While executing gem … (Gem::RemoteSourceException) HTTP Response 407
Here is the steps I followed to get gem install working on my windows xp machine in painstaking detail:
Firstly we need to set the HTTP_PROXY environment variable. I’ve played around with different options, using uid and pwd and other suggestions, but all I needed was the following:
- On the desktop right click the ‘My Computer’ icon and select properties.

- Now you can either add the HTTP_PROXY variable to the ‘User’ or the ‘System’ variables by clicking the ‘New’ button.

- Within the new system variable dialog, specify ’HTTP_PROXY’ as the variable name and in the value area a url in the following format – http://%5Bproxy_ip%5D:%5Bproxy_port%5D – i.e. http://127.168.1.3:5865

Next we install the Ruby Win32 SSPI which gives ruby the ability to authenticate with ISA natively. We need this library because as far as I understand, it patches the Open-uri library because of some incompatibilities with windows. Anyways, follow the following steps to get it working:
- Download rubysspi from the Ruby Win32 SSPI project page(also available as gem install rubysspi but that doesn’t help much, does it?)
- Install the gem locally using the following command: gem install [local_path_to_gem]\rubysspi-1.2.3.gem i.e. C:\gems\rubysspi-1.2.3.gem
- Now copy the spa.rb file from the gem install directory and paste it in the site-ruby directory. i.e. If ruby is installed in C:\ruby, then the paths should be:
- origin path – C:\ruby\lib\ruby\gems\1.8\gems\rubysspi-1.2.3\spa.rb
- destination path – C:\ruby\lib\ruby\site_ruby\spa.rb
- Find gem.bat in your ruby bin directory (C:\ruby\bin) and replace the last line (i.e. "%~d0%~p0ruby" -x "%~f0" %*) with the following line – @"ruby" -rspa "c:\ruby\bin\gem" %1 %2 %3 %4 %5 %6 %7 %8 %9
- Test you can access the remote ruby gems by executing the following command: gem list rails -r

- You should see similar output as shown above.
Now we can work again without any issues when trying to install or update gems.
Enjoy!!!






Run gem install behind a firewall in Windows said
[...] portalhispano wrote an interesting post today onHere’s a quick excerptI’m contracting at a client running a Novell network and wanted to create some small little ruby scripts to automate some tasks for me. But I ran into problems using gem install behind their firewall. I’m getting the following error: ERROR: While executing gem … (Gem::RemoteSourceException) HTTP Response 407 [...]
Jason said
I followed the instructions above and got this when I ran the ‘gem list rails -r’ test:
>gem list rails -r
‘örubyö’ is not recognized as an internal or external command,
operable program or batch file.
Any ideas?
Jared said
You need to edit the batch file in text mode, and re-type the quote characters. The quotes you have copied and pasted are unicode left and right double quote characters that you might get from Word, instead of the plain ascii double quote character. This should probably be fixed in the blog post.
Maruis Marais said
Check to see if the ruby executable is set in your Environment PATH. If you look at step 3 you’ll see the PATH which contains the C:\ruby\bin, you need to set this path with where your ruby lives. Now, after you’ve set this, reboot. Then test it in your cmd window by running ruby -v which should report the version of ruby you’re running. Hope that helps…
Skyward said
Somehow i missed the point. Probably lost in translation
Anyway … nice blog to visit.
cheers, Skyward.
Travis Winters said
Nice one. It’s certainly tricky making this stuff work. I was having similar problems 18 months ago, when the tools that make this easier were not quite as mature. It wasn’t any fun.
Victor Musiwa said
Thanks man, this has been a project staller for while!
Ben Cameorn said
I followed the instructions above and got this when I ran the ‘gem list rails -r’ test:
>gem list rails -r
‘örubyö’ is not recognized as an internal or external command,
operable program or batch file.
To fix this error, remove the funny Microsoft Word style double quotes and replace with the standard ones above the apostrophe on your keyboard.
jadjay said
You’re a f*cking genius !!!!
Thank you so much !
Truly!
Roy Pardee said
I’m behind an NLTM-authentication-requiring proxy server at my work and for whatever it’s worth, this has worked for me:
set http_proxy=http://win_username:win_password_here@proxy.ghc.org:8080
Where win_username and win_password were substituted w/my current windows network creds. Since that’s obviously sensitive information, I just execute that set command at a DOS prompt (which means it survives only as long as the DOS prompt session) right before calling the ‘gem’ command.
Note that this is *without* using the win32 SSPI thing. I was not able to get that to work for me…
Cheers.
Charl Fourie said
Contrary to Roy I do need Rubysspi. I am playing around with Ruby 1.9 at the moment, but Rubysspi does not seem to be compattible with that yet. If anybody has a work-around, suggestions would be welcome.
Charl Fourie said
A correction to my previous comment — I see that Justin Bailey’s sspi library has been build into the Ruby 1.9 base code. (ruby/lib/ruby/1.9.0/win32/sspi.rb)
Gems through the proxy (or the «undefined method `[]=’» error) « ELIÆ said
[...] Gems Proxy under windows Possibly related posts: (automatically generated)Run gem install behind a firewall in WindowsRuby: [...]
Tio Zé said
Thanx,
you saved my life.
Att.
Tio_Zé
(Uncle_Joseph)
Fahim Ahmed said
Thanx,
This article was a great help for me.
Don said
Ron,
Worked like a charm. Thanks!
Aaron said
Great article! Your instructions, in combination with Roy Pardee’s comment, works perfect for a windows authenticated proxy.
Thanks to both of you.
Romain said
Thanks for the article!
I only had to change the following:
set http_proxy=http://domain\username:password@server:port
When setting the HTTP_PROXY environment variable, I had to escape all non alphanumeric characters. For example, the \ between the domain and username becomes %5C. I also had a # in my password which had to become %23.
Darryl said
Thank you!
srinivasan m said
Hi,
I cannot follow the second step which is “Install the gem locally using the following command: gem install [local_path_to_gem]\rubysspi-1.2.3.gem i.e. C:\gems\rubysspi-1.2.3.gem”
I have downloaded the rubysspi-1.2.3.gem.tar from the website. But while trying the above step in installing, I could not install it properly.. Can somebody explain more on this.
The error which I am getting is, ERROR: http://gems.rubyforge.org/ does not appear to be a repository. ERROR: could not find gem C:\gems\rubysspi-1.3.1.gem locally or in a repository.
Thank you.
Marcus Wyatt said
Firstly, you have extracted the .gem file from the archive? If you have, you will have the rubysspi-1.2.3.gem file somewhere on the file system. Now change your working directory to where the .gem file is located and then executing the gem install command.
In the above code the .gem file was located at ‘C:\gems\rubysspi-1.2.3.gem’ so I would cd into the ‘C:\gems\’ directory and then execute the command ‘gem install rubysspi-1.2.3.gem’.
You can probably also try the latest version of the gem (version 1.3.1).
Marcos said
Tried all this with no success.
I’m not behind a proxy, only behind a router. After all the teaks suggested here I still get:
ERROR: While executing gem … (Gem::RemoteSourceException)
HTTP Response 403
What should I use as http_proxy? Tried router IP, my current IP provided by ISP. Any lights?
Marcus Wyatt said
As far as I know you shouldn’t have any issues at all with just a std router. It sounds like you have a specific port or range of ports blocked, which the gem command will use when connecting to the gem hosts. Also(going far back into the memory banks), check your windows firewall settings, you might have blocked some previous request that is now causing your headaches.
If all else fails, you could always just get a Mac. I’ve stopped using Windows 2 years ago and now just run os x and ubuntu.
Topper Heddon said
“If all else fails, you could always just get a Mac. I’ve stopped using Windows 2 years ago and now just run os x and ubuntu.” – This is in no way helpful. You smug Apple users are so annoying!
Marcus Wyatt said
I felt the same way before I bought my first Mac. It’s to expensive. You can’t tweak the hardware and it feels restrictive. But here we are a couple of years later and I still feel the same about Mac. The best decision I’ve ever made in my 15 year IT career.
Huy said
Can someone show me how to get the step 4 work?
abhishek said
Hi
step 4, It says to change last line.
But last line in my gem.bat is different @”ruby.exe” “%~dpn0″ %*
what should i do?
Marcus Wyatt said
Hi Abhishek,
I am no longer working in an environment where I have the firewall and windooze operating system to check for you. But what I would suggest, is to make a backup of your gem.bat file. (If I remember correctly, run “cp gem.bat gem.backup”). Now change the line as per the instruction and then test the changes. If it works, well you good to go. If not, then just run the copy command in reverse. (‘cp gem.backup gem.bat’).
Hope it helps…
Horace Ho said
Great help! Thank you~
Stephanie Meyer said
Nice, thanks for sharing this info. Looks great on my iPhone, but on the Blackberry Pearl’s browser your site comes out a little irregular.
Praj said
Hi,
i followed all the steps in my home pc where it doesnot ask any authentication or does not have any proxy,
Within the new system variable dialog, specify ’HTTP_PROXY’ as the variable name and in the value area a url in the following format – http://%5Bproxy_ip%5D:%5Bproxy_port%5D – i.e. http://192.168.1.1:80
1. Download rubysspi from the Ruby Win32 SSPI project page(also available as gem install rubysspi but that doesn’t help much, does it?)
2. Install the gem locally using the following command:
gem install “D:\GEMS\rubysspi-1.2.3.gem”
D:\Ruby191\bin>gem install “d:\GEMS\rubysspi-1.3.1.gem”
Successfully installed rubysspi-1.3.1
1 gem installed
Installing ri documentation for rubysspi-1.3.1…
Updating class cache with 11 classes…
Installing RDoc documentation for rubysspi-1.3.1…
3. Now copy the spa.rb file from the gem install directory and paste it in the site-ruby directory. i.e. If ruby is installed in D:\Ruby191, then the paths should be:
origin path –D:\Ruby191\lib\ruby\gems\1.8\gems\rubysspi-1.2.3\spa.rb
destination path – D:\Ruby191\lib\ruby\site_ruby\spa.rb
4.Find gem.bat in your ruby bin directory (D:\Ruby191\bin) and replace the last line (i.e. “%~d0%~p0ruby” -x “%~f0″ %*) with the following line – @”ruby” -rspa “c:\ruby\bin\gem” %1 %2 %3 %4 %5 %6 %7 %8 %9
5. Test you can access the remote ruby gems by executing the following command: gem list rails -r
D:\Ruby191\bin>gem list rails -r
*** REMOTE GEMS ***
ERROR: http://rubygems.org/ does not appear to be a repository
ERROR: While executing gem … (Gem::RemoteFetcher::FetchError)
Errno::EACCES: Permission denied – connect(2) (http://rubygems.org/yaml)
Chris Prakoso said
This is awesome! I was a little bit sceptical at first
But since I was completely stuck and didn’t know what to do more, I tried it your way!
Lo and Behold!!! It’s bloody work!!! Hell yeah!
Thanks so much! I am going to link to this article from my website.
Chris Prakoso said
Wow! I’ve searched high and low in the Internet, and yours is the only one that works!!!
Thanks a lot! You are a star!!
Cheers,
Chris
MJS said
Thanks for this. Worked a treat.
✇ google proxies | proxy server | proxy for free | webserver proxy| proxy facebook | proxy twitter | proxy free | server proxy gratis | said
✇ google proxies | proxy server | proxy for free | webserver proxy| proxy facebook | proxy twitter | proxy free | server proxy gratis |…
[...]Run gem install behind a firewall in Windows « exceptionz[...]…
Pauli said
Worked, thks! I can link ur site? Thks o/