285

I am trying to use pip behind a proxy at work.

One of the answers from this post suggested using CNTLM. I installed and configured it per this other post, but running cntlm.exe -c cntlm.ini -I -M http://google.com gave the error Connection to proxy failed, bailing out.

I also tried pip install -–proxy=user:pass@localhost:3128 (the default CNTLM port) but that raised Cannot fetch index base URL http://pypi.python.org/simple/. Clearly something's up with the proxy.

Does anyone know how to check more definitively whether CNTLM is set up right, or if there's another way around this altogether? I know you can also set the http_proxy environment variable as described here but I'm not sure what credentials to put in. The ones from cntlm.ini?

Rob
  • 9,433
  • 12
  • 52
  • 87
  • 2
    You need to know the IP address of your proxy and port number and set it accordingly in your cntlm.ini (Also your username and password for the proxy if you have one) – number5 Jan 04 '13 at 01:58
  • 2
    I wrote a [blog](http://annelagang.blogspot.com/2012/11/installing-gems-in-ubuntu-1204-using.html) on how to setup and use CNTLM. The post is for Ubuntu and for installing Rails, but I think the process might be the same. Edit: Read this [article](http://stormpoopersmith.com/2012/03/20/using-applications-behind-a-corporate-proxy/) on how to setup CNTLM for Windows, basically same with my Ubuntu version. HTH. – Annie Lagang Jan 04 '13 at 02:16

32 Answers32

275

With Ubuntu I could not get the proxy option to work as advertised – so following command did not work:

sudo pip --proxy http://web-proxy.mydomain.com install somepackage

But exporting the https_proxy environment variable (note its https_proxy not http_proxy) did the trick:

export https_proxy=http://web-proxy.mydomain.com

then

sudo -E pip install somepackage
Martin Tournoij
  • 23,567
  • 24
  • 90
  • 127
Sameer Vaidya
  • 2,851
  • 2
  • 9
  • 3
  • 1
    setting https_proxy does the job in Mac OS X too. – Bharadwaj Srigiriraju Nov 19 '13 at 11:10
  • 4
    same here, --proxy did not work, I had to specify BOTH http_proxy AND https_proxy (Debian behind a corp firewall) – marc.guenther Dec 12 '13 at 10:18
  • 35
    Thank you for including ```-E``` in the ```sudo``` command, that constantly throws me when I wonder why programs aren't seeing my ENV. – AndyC May 26 '14 at 08:44
  • 1
    Works under windows too! – AngelM1981 May 27 '14 at 12:27
  • 11
    -E added to sudo did the trick for me. It preserves the environment variable "http_proxy" that I set up earlier. Thanks! – imagineerThat Sep 23 '14 at 22:47
  • I am behind a proxy and the following worked to get me updated `sudo pip --proxy http://my.proxy:port install -U pip` – Eric Fossum Jan 28 '15 at 16:49
  • 1
    It only worked for me after updating to pip==6.0.8 on Ubuntu 14.04, with the pip suited within python-pip apt package it does NOT work nor with environment variables nor with --proxy when using -r option – danius Feb 21 '15 at 23:15
  • 7
    pip --proxy= someproxy-url:port install some-package worked for me in Win8.1 with pip 6.1.1 – zenpoy Apr 29 '15 at 07:15
  • I think this is the best answer. I've been setting up a bunch of things all day at work, and for the most part, setting `http_proxy`, `https_proxy`, `HTTP_PROXY`, and `HTTPS_PROXY` covered all sins. For some reason, I just couldn't get `get_pip.py` to work behind a proxy though. – Jacqlyn Jul 13 '15 at 17:24
  • Depending on your distribution and proxy settings, the above will not work. The general case will be: `export http_proxy="http://proxy-server:port"` (no authentication required) `export http_proxy="http://user:password@proxy-server:port"` (authentication required). Similar for https protocol. See http://www.shellhacks.com/en/HowTo-Use-a-Proxy-on-the-Linux-Command-Line – nikosd Jul 15 '15 at 17:38
  • this should be the right answer because it solves without need to install additional software/packages. – bagustris Apr 01 '16 at 14:50
  • sudo pip --proxy http://web-proxy.mydomain.com install somepackage worked for windows 7 – hugomau Dec 19 '17 at 11:22
  • Adding `-H` for `sudo` may also be needed, so as *not* to inherit HOME directory of originating user but that of superuser. – Anton Samsonov Mar 21 '18 at 09:10
  • It works well on Mac pro. sudo pip --proxy http://web-proxy.mydomain.com install hvac. – Hua Zhang May 02 '19 at 18:56
96

Under Windows dont forget to set

SET HTTPS_PROXY=<proxyHost>:<proxyPort>

what I needed to set for

pip install pep8
k7sleeper
  • 2,359
  • 1
  • 14
  • 11
  • Works on fedora also with 'export http_proxy=:' – Damien Feb 25 '14 at 16:29
  • 5
    This worked for me, but only in the form SET HTTPS_PROXY=ht tps://user:pass@addr:port [minus the space I had to add to keep stackoverflow from garbling it] and I had to replace special characters in my password with hex in the form %nn – Seth Nov 04 '16 at 16:05
  • this worked for me. before, I was doing: `SET HTTPS_PROXY=username:password@:` and it was not working. – Zahra May 02 '17 at 17:21
  • Does somebody have an explanation why the environment variable is needed and how its used by pip? For conda its enough to configure the proxy in the .condarc file. – maggie May 16 '17 at 07:56
  • 1
    An example call would look like: `SET HTTPS_PROXY = https://512893:Pass%23h98@proxy.example.com:6050` for username `512893` & password `pass#h98`. Remember to url encode special characters in password or username (`#` in this case). Maybe also try setting the `HTTP_PROXY` flag as well. – Nuhman Nov 09 '18 at 09:46
  • this worked for me. nothing worked with the "--proxy" parameter from pip command itself. – Robert Apr 11 '19 at 14:02
76

To setup CNTLM for windows, follow this article. For Ubuntu, read my blog post.

Edit:

Basically, to use CNTLM in any platform, you need to setup your username and hashed password, before using http://127.0.0.1:3128 as a proxy to your parent proxy.

  1. Edit the config and add important information like domain, username, password and parent proxy.

  2. Generate hashed password.

    Windows cntlm –c cntlm.ini –H

    Ubuntu/Linux cntlm -v -H -c /etc/cntlm.conf

  3. Remove plain text password from the config and replace them with the generated passwords.

To check if working:

Windows cntlm –M http://www.google.com

Ubuntu/Linux sudo cntlm -M http://www.google.com/

For more detailed instructions, see links above.

Update:

Just for completeness sake, I was able to configure and use CNTLM in Windows recently. I encountered a problem during the syncing process of Kindle for PC because of our proxy and installing and configuring CNTLM for Windows fixed that issue for me. Refer to my article for more details.

Annie Lagang
  • 3,135
  • 1
  • 25
  • 36
  • If this guide does not solve your issue, try running pip with commandline option `--trusted-host pypi.python.org` which did the trick for me. – Petr Jan 04 '19 at 08:23
  • 1
    I'd just like to add to this, that I couldn't actually get cntlm working on Windows or Mac within our corporate environment, but installing Fiddler on a Windows machine and using it as the proxy solved the issue. – Dean Meehan Jul 31 '19 at 13:30
66

It was not working for me. I had to use https at work:

pip install --proxy=https://user@mydomain:port somepackage

In order to update, add -U.

Olivier
  • 117
  • 1
  • 10
Hugo Salvador
  • 1,036
  • 10
  • 11
47

You can continue to use pip over HTTPS by adding your corporation's root certificate to the cacert.pem file in your site-packages/pip folder. Then configure pip to use your proxy by adding the following lines to ~/pip/pip.conf (or ~\pip\pip.ini if you're on Windows):

[global]
proxy = [user:passwd@]proxy.server:port

That's it. No need to use third party packages or give up HTTPS (of course, your network admin can still see what you're doing).

djmoch
  • 700
  • 7
  • 11
  • This worked for me, but only after manually installing pip, which means you can't use get-pip.py initially – Luke Dunstan Nov 10 '14 at 08:13
  • 2
    For the location of pip.ini see https://pip.pypa.io/en/latest/user_guide.html#configuration In windows I found cacert in "C:\Python34\Lib\site-packages\pip\_vendor\requests\cacert.pem" – lib May 18 '15 at 09:00
  • 2
    This worked for me but I didnt have a pip.config file so I had to create one in `~/.pip/pip.conf` then `export PIP_CONFIG_FILE=/root/.pip/pip.conf` and my installation worked! Thanks! – Rhyuk Sep 22 '16 at 14:48
  • On windows7 this did not work. After installing python35 on windows into c:\python35-32 I find pip in the c:\python35-32\Scripts folder. No pip.ini. Manually creating one in %APPDATA%\pip as suggested by @lib did not do anything. – 576i Nov 09 '16 at 10:39
  • On Windows 7, *creating* pip.ini in the **~\pip** directory worked. – asachet Apr 11 '17 at 12:42
  • are those square brackets around user required? why is symbol @ used after password? – user13107 May 21 '18 at 06:17
  • 2
    Thanks, you set me on the right path! FYI, for Python 3.7 on Windows 7 pip's certificates file is apparently located at `[Python37]\Lib\site-packages\pip\_vendor\certifi\cacert.pem`. Export your corp's SSL interception certificate in Base64 .cer format, paste the public key into that file, and voilà, full https support behind the corporate proxy. – Mike Strobel Dec 04 '18 at 14:31
  • 2
    Your pip ini location on windows may be `AppData/Roaming/pip/pip.ini` .... – Erik Aronesty Dec 22 '18 at 10:10
  • `pip config set global.proxy [user:passwd@]proxy.server:port` command auto create file – Voyager Mar 03 '19 at 14:50
  • I had to put it here: C:\Users\MYUSERNAME\AppData\Roaming\pip\pip.ini This one alone didn't work: C:\Users\MYUSERNAME\AppData\Local\pip\pip.ini – PM0087 Nov 07 '19 at 16:59
27

for windows; set your proxy in command prompt as
set HTTP_PROXY=domain\username:password@myproxy:myproxyport

example:
set http_proxy=IND\namit.kewat:xl123456@192.168.180.150:8880

namit
  • 6,042
  • 3
  • 30
  • 39
  • 3
    What if the password has special chars such as ! and @. – Ravi Kumar Jul 18 '13 at 14:36
  • 1
    escape special chars with the bash escape char: \ – Adam Nov 04 '13 at 18:41
  • Escaping did not do the trick... I've got the characters ! and @ in my proxy username and password... Neither using the environment variables nor the --proxy parameter did the trick (both escaped and unescaped) – MOnsDaR Jun 19 '15 at 05:15
  • 1
    You can't escape '@' in your password with escape character (\), you have to url encode it to %40 in the proxy string. – Deep-B Oct 15 '15 at 13:26
  • 3
    I needed set HTTP_PROXY=http://username:password@myproxy:myproxyport set HTTPS_PROXY=https://username:password@myproxy:myproxyport – Seth Oct 04 '16 at 21:34
  • If you have pac file, you can download it, and look for the proxy server to setup the windows environment. – Umar Jul 19 '18 at 12:33
  • 1
    Setting this environment variable seems to have no effect on pip on my machine – Jack M Aug 17 '18 at 11:00
26

This worked for me (on Windows via CMD):

pip install --proxy proxyserver:port requests
bastelflp
  • 6,818
  • 5
  • 27
  • 61
damian1baran
  • 1,159
  • 1
  • 12
  • 13
21
$ pip --proxy http://proxy-host:proxy-port install packagename

This is what worked for me on

slm
  • 12,534
  • 12
  • 87
  • 106
Anand
  • 228
  • 2
  • 4
  • 4
    This is really useful when only temporarily behind a proxy. I also had to add my username and password to the syntax: `pip --proxy http://username:password@proxy-host:proxy-port install ` – Terrabits Jun 20 '18 at 21:12
14

Under our security policy I may not use https with pypi, SSL-inspection rewrites certificates, it breaks the built-in security of pip for www.python.org. The man in the middle is the network-admin.

So I need to use plain http. To do so I need to override the system proxy as well as the default pypi:

bin/pip install --proxy=squidproxy:3128 -i http://www.python.org/pypi --upgrade "SQLAlchemy>=0.7.10"
bbaassssiiee
  • 4,879
  • 1
  • 34
  • 46
  • 2
    This doesn't work anymore. PyPi now automatically forwards you to https. – Martin M. Nov 27 '14 at 11:22
  • SSL inspection does not seem to work; could it be because key pinning, or forward secrecy? – bbaassssiiee Apr 15 '17 at 12:35
  • @SamuelHarmer . `Security` has four syllables. Too many for Trump. – StephenBoesch Jun 27 '17 at 19:17
  • 2
    I think @bbaassssiiee may be saying that his pip broke (in 2013) with HTTPS because his corporate proxy rewrites all certs to use their own root cert (so they can decrypt all SSL traffic) and pip fails the cert verification. Of course in 2017, pip automatically avoids certificate checking when --proxy is used. Many corporate proxies already effectively change all of your traffic to non-SSL anyway because even when navigating to SSL sites, you are proxying via non-SSL HTTP proxy. – cowbert Jul 19 '17 at 19:36
13

Open the Windows command prompt.

Set proxy environment variables.

set http_proxy=http://user:password@proxy_ip:port
set https_proxy=https://user:password@proxy_ip:port

Install Python packages using proxy in the same Windows command prompt.

pip install --proxy="user:password@proxy_ip:port" package_name
glennsl
  • 23,127
  • 11
  • 49
  • 65
Jyoti Yadav
  • 263
  • 2
  • 9
12

In Windows 7:

pip install --proxy DOMAIN\user:password@proxyaddress:port package

i.e.:

pip install --proxy BR\neo:p4ssw0rd@myproxyrocks.com.br:8080 virtualenv
Daniel
  • 624
  • 6
  • 21
11

Phone as mobile hotspot/USB tethering

If I have much trouble finding a way through the corporate proxy, I connect to the web through my phone (wireless hotspot if I have wifi, USB tether if not) and do a quick pip install.

Might not work for all setups, but should get most people by in a pinch.

Tommy O'Dell
  • 6,553
  • 13
  • 51
  • 67
8

In Ubuntu 14.04 LTS

   sudo pip --proxy http://PROXYDOM:PROXYPORT install package

Cheers

NooBskie
  • 3,487
  • 20
  • 46
7

I had the same issue : behind a corporate proxy with auth at work, I couldn't have pip work, as well as Sublime Text 2 (well, it worked with custom setup of my proxy settings). For pip (and I'll try that on git), I solved it installing cntlm proxy. It was very simple to configure :

  1. Edit cntlm.ini
  2. Edit "Username", "Domain", "Password" fields
  3. Add a "Proxy" line, with your proxy settings : server:port
  4. Make sure the line "NoProxy" integrates "localhost" (like that by default)
  5. Note the default port : 3128
  6. Save and that's it.

To test that works, just launch a new command line tool, and try :

pip install django --proxy=localhost:3128

That worked for me. Hope this will help you.

Johan Chouquet
  • 417
  • 6
  • 13
6

Set up invironment variable in Advanced System Settings. In Command prompt it should behave like this :

C:\Windows\system32>echo %http_proxy%

http://username:passowrd@proxy:port

C:\Windows\system32>echo %https_proxy%

http://username:password@proxy:port

Later , Simply pip install whatEver should work.

mkkhedawat
  • 1,467
  • 2
  • 13
  • 30
6

I could achieve this by running:

pip install --proxy=http://user:pass@your.proxy.com:3128 package==version

I'm using Python 3.7.3 inside a corporative proxy.

Nicolas Castro
  • 1,044
  • 9
  • 10
5

if you want to upgrade pip by proxy, can use (for example in Windows):

python -m pip --proxy http://proxy_user:proxy_password@proxy_hostname:proxy_port insta
ll --upgrade pip
Nikolay Baranenko
  • 1,096
  • 3
  • 21
  • 48
4

For windows users: if you want to install Flask-MongoAlchemy then use the following code

pip install Flask-MongoAlchemy --proxy="http://example.com:port"**
skuntsel
  • 11,346
  • 11
  • 41
  • 64
Nitin Panwar
  • 121
  • 1
  • 4
3

If you are connecting to the internet behind a proxy, there might be problem in running the some commands.

Set the environment variables for proxy configuration in the command prompt as follows:

set http_proxy=http://username:password@proxyserver:proxyport
set https_proxy=https://username:password@proxyserver:proxyport
laplace
  • 169
  • 8
2

How about just doing it locally? Most likely you are able to download from https source through your browser

  1. Download your module file (mysql-connector-python-2.0.3.zip /gz... etc).
  2. Extract it and go the extracted dir where setup.py is located and call:

    C:\mysql-connector-python-2.0.3>python.exe setup.py install
    
Tshilidzi Mudau
  • 5,472
  • 6
  • 32
  • 44
Sendi_t
  • 572
  • 3
  • 13
2

At CentOS (actually I think all linux distros are similar) run

env|grep http_proxy

and

env|grep https_proxy

check what is the output of those commands (they should contain your proxy addresses).

If the outputs are empty or have incorrect values, modify them, for ex:

export http_proxy=http://10.1.1.1:8080
export https_proxy=http://10.1.1.1:8080

Now try to fetch and install some packages by using pip:

pip --proxy http://10.1.1.1:8080 install robotframework

and actually I have never met the case when it didn't work. For some systems you need to be a root (sudo is not enough).

Paweł Wojtal
  • 230
  • 5
  • 11
2

Warning, there is something very bad with the "pip search" command. The search command do not use the proxy setting regardless of the way it's being passed.

I was trying to figure out the problem only trying the "search" command, and found this post with detailed explanation about that bug: https://github.com/pypa/pip/issues/1104

I can confirm the bug remains with pip 1.5.6 on Debian 8 with python 2.7.9. The "pip install" command works like a charm.

Pivert
  • 701
  • 5
  • 15
2

I got the error:

chris@green:~$ sudo http_proxy=http://localhost:3128 pip install django==1.8.8 
Downloading/unpacking django==1.8.8
  Cannot fetch index base URL http://pypi.python.org/simple/
  Could not find any downloads that satisfy the requirement django==1.8.8
No distributions at all found for django==1.8.8
Storing complete log in /home/chris/.pip/pip.log

(The proxy server's port is ssh port forwarded to localhost:3128).

I had to set both http and https proxies to make it work:

chris@green:~$ sudo http_proxy=http://localhost:3128 https_proxy=http://localhost:3128 pip install django==1.8.8
Downloading/unpacking django==1.8.8
  Downloading Django-1.8.8.tar.gz (7.3Mb): 7.3Mb downloaded
  Running setup.py egg_info for package django

    warning: no previously-included files matching '__pycache__' found under directory '*'
    warning: no previously-included files matching '*.py[co]' found under directory '*'
Installing collected packages: django
  Running setup.py install for django

    warning: no previously-included files matching '__pycache__' found under directory '*'
    warning: no previously-included files matching '*.py[co]' found under directory '*'
    changing mode of build/scripts-2.7/django-admin.py from 644 to 755
    changing mode of /usr/local/bin/django-admin.py to 755
    Installing django-admin script to /usr/local/bin
Successfully installed django
Cleaning up...

as http://pypi.python.org/simple/ redirects to https://pypi.python.org/simple but pip's error does not tell you.

fadedbee
  • 37,386
  • 39
  • 142
  • 236
2

I am also no expert in this but I made it work by setting the all_proxy variable in the ~/.bashrc file. To open ~/.bashrc file and edit it from a terminal run following commands,

gedit ~/.bashrc &

Add following at the end of file,

export all_proxy="http://x.y.z.w:port"

Then either open a new terminal or run following in the same terminal,

source ~/.bashrc

Just setting http_proxy and https_proxy variables aren't enough for simple usage pip install somepackage. Though somehow sudo -E pip install somepackage works, but this have given me some problem in case I am using a local installation of Anaconda in my users' folder.

P.S. - I am using Ubuntu 16.04.

bytestorm
  • 1,201
  • 3
  • 17
  • 35
2

Using pip behind a work proxy with authentification, note that quotation is required for some OSes when specifing the proxy url with user and password:

pip install <module> --proxy 'http://<proxy_user>:<proxy_password>@<proxy_ip>:<proxy_port>'

Documentation: https://pip.pypa.io/en/stable/user_guide/#using-a-proxy-server

Example:

pip3 install -r requirements.txt --proxy 'http://user:password@192.168.0.1:1234'

Example:

pip install flask --proxy 'http://user:password@192.168.0.1:1234'

Proxy can also be configured manually in pip.ini. Example:

[global]
proxy = http://user:password@192.168.0.1:1234 

Documentation: https://pip.pypa.io/en/stable/user_guide/#config-file

  • Hello Mihai, you should be providing an answer, but instead you're asking a question. You may instead want to review the previous answers and add a comment to the relevant one(s). I'd suggest looking at whether the environment variable is properly exported (`export http_proxy`). Consider solving the problem and updating your answer so that it provides a solution. – Thomas Mar 20 '20 at 13:15
  • Thank you for reviewing this answer, great job! – Thomas Nov 17 '20 at 14:50
1

This is what works for me:

pip --proxy proxy url:port command package
Tshilidzi Mudau
  • 5,472
  • 6
  • 32
  • 44
Brock Vond
  • 216
  • 2
  • 7
1

Set the following environment variable: export PIP_PROXY=http://web-proxy.mydomain.com

FTM
  • 1,324
  • 10
  • 29
1

If you are using Linux, as root:

env https_proxy=http://$web_proxy_ip:$web_proxy_port pip install something

When you use env it exports the variable https_proxy for the current execution of the command pip install.

$web_proxy_ip is the hostname or IP of your Proxy $web_proxy_port is the Port

Adail Junior
  • 431
  • 4
  • 6
0

I solved the problem with PIP in Windows using "Fiddler" (https://www.telerik.com/download/fiddler). After downloading and installing, do the following:

"Rules" => click "Automatically Authenticate"

Example: pip install virtualenv -proxy 127.0.0.1:8888

Just open your prompt and use.

https://github.com/pypa/pip/issues/1182 Search for "voltagex" (commented on 22 May 2015)

0

for windows go to C:/ProgramData/pip/pip.ini, and set

[global]

proxy = http://YouKnowTheRest

with your proxy details. This permanently configures the proxy for pip.

0

A simpler approach might be:

  1. Create a folder named "pip" in your $HOME directory.
  2. Create a file named "pip.ini" (Windows) or "pip.conf" (Linux) in the directory created in step 1
  3. Copy & paste the following lines under the pip.ini/pip.conf:

    [global]
    trusted-host = pypi.python.org
                   pypi.org
                   files.pythonhosted.org 
    
Alex
  • 7,405
  • 5
  • 46
  • 72
0
  1. For PIP Installation:
ex:PORT = 9090
ex:PROXY_SERVER = stackoverflow
USERNAME:your user id
PASSWORD: your password

sudo pip2 install PACKAGENAME --proxy https://USERNAME:PASSWORD@PROXY_SERVER:PORT/ for Python2.7

sudo pip3 install PACKAGENAME --proxy https://USERNAME:PASSWORD@PROXY_SERVER:PORT/ for Python3.5

Example:

sudo pip2 install pandas --proxy https://USERNAME:PASSWORD@PROXY_SERVER:PORT/
  1. For apt-get Installation
sudo http_proxy=http://USERNAME:PASSWORD@PROXY_SERVER:PORT/ apt-get install PACKAGENAME

Example:

sudo http_proxy=http://USERNAME:YOURPASSWORD@PROXY_SERVER:PORT/ apt-get install tensorrt

sudo http_proxy=http://USERNAME:YOURPASSWORD@PROXY_SERVER:PORT/ apt-get update
biruk1230
  • 2,508
  • 4
  • 12
  • 26