How to use bundler behind a proxy?

2019-01-12 22:04发布

问题:

I get the following output from the sudo bundle install command:

Fetching source index for `http://rubygems.org/`  
Could not reach rubygems repository `http://rubygems.org/`  
Could not find gem 'rspec-rails (>= 2.0.0.beta.22, runtime)' in any of the gem sources.

I have $http_proxy set correctly and I've added gem: --http-proxy=my proxy to ~/.gemrc. These settings are what allow my gem commands to work, and I was hoping they would translate to bundler, but no such luck.

Thinking sudo might not inherit my all of my environment, I also added those settings to my root user, but nada.

At this point bundler is preventing me from deploying my application, and I can find very few others running into this. If no one has an answer I will be forced to rip bundler out of my Rails app (which I wouldn't mind doing...)

回答1:

OSX & Linux

export http_proxy=http://user:password@host:port
export HTTP_PROXY=$http_proxy

If it's using HTTPS, set it as well

export https_proxy=http://user:password@host:port
export HTTPS_PROXY=$https_proxy

If you use sudo, by default sudo does not preserves http proxy variable. Use -E flag to preserve it

$ sudo -E bundle install

to make sudo preserves environment variables by default:

https://memset.wordpress.com/2010/10/14/bash-http_proxy-from-a-user-environment-to-sudo-one/

Windows

As pointed by answers below, you can use SET instead

SET HTTP_PROXY=http://user:password@host:port
SET HTTPS_PROXY=%HTTP_PROXY%


回答2:

I figured out that also setting HTTP_PROXY (in addition to http_proxy) made a positive difference, i.e. it worked for me. So assuming that you have set up http_proxy environment variable correct, try (if you are using bash)

export HTTP_PROXY=$http_proxy

and then also use the -E option to sudo (to preserve environment variables), so

sudo -E bundle install

Jarl



回答3:

to get bundler behind a proxy on win XP/7 I needed to do the following:

I added http_proxy to the Environment Variables

  • My Computer
  • Advanced system settings
  • Advanced Tab Environment
  • Variables
  • New
  • Variable name = http_proxy
  • Variable value = MY_PROXY
  • Click Ok

Change MY_PROXY to whatever yours is.

this worked for bundler. The .gemrc proxy setting only worked for gems.

thanks Jamie



回答4:

You can download the required gems locally with gem install and then bundle install. Not exactly neat, I know, but it does work.



回答5:

If you don't want to set a global variable in the system you can edit ~/.gemrc and write it like that

---
:benchmark: false
:verbose: true
:sources:
- http://rubygems.org/
- http://gems.rubyforge.org
:backtrace: false
:bulk_threshold: 1000
:update_sources: true
gem: --http-proxy=http://USERNAME:PASSWORD@ADDRESS:PORT


回答6:

probably more flexible and securable use batch file:

SET /P login="Enter proxy login: "
SET /P password="Enter proxy password: "
SET HTTP_PROXY=http://%login%:%password%@proxy.com:8080
SET HTTPS_PROXY=%HTTP_PROXY%

CLS

bundle install


回答7:

Windows OS, run following command before execute bundle install

SET http_proxy=http://user:password@host:port


回答8:

Make sure your OS default http_proxy is already set up. If you're using Linux try the following command to know which proxy it's pointing to.

echo $http_proxy

In my Ubuntu OS, I set my http_proxy environment variable to my proxy server in ~/.bashrc



回答9:

$ export http_proxy="http://username:password@host:port"
$ export ftp_proxy="http://username:password@host:port"
$ sudo visudo

Add this line in the file:

Defaults env_keep = "http_proxy ftp_proxy"

Above this line:

Defaults        env_reset

then run your command as sudo it will work.

ref :https://memset.wordpress.com/2010/10/14/bash-http_proxy-from-a-user-environment-to-sudo-one/



回答10:

I am running Ubuntu. The $http_proxy variable is set, but it doesn't work with a couple items. One of those items being gem.

If you put the following in your ~/.gemrc it will work.

http_proxy: proxy-url:port

Replace the proxy-url:port with your proxy address and port. After I added that, I ran "bundle install" and everything ran as expected.



回答11:

To have command bundle install work with proxy on windows do the following:

  1. Edit file .gemrc. Open windows command line and type: notepad %userprofile%\.gemrc .
  2. The file .gemrc is open in notepad. Type on a new line http_proxy: http://username:passwordEncodedWithUrlencode@proxyaddress:proxyport . Password should be encoded with urlencode .
  3. Close the file .gemrc with saving it.