172

I am trying to install a Python library using pip, getting an SSL error:

~/projects/base  pre-master±  pip install xdict

Collecting xdict
  Could not fetch URL https://pypi.python.org/simple/xdict/: There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) - skipping
  Could not find a version that satisfies the requirement xdict (from versions: )
No matching distribution found for xdict

pip version: pip 9.0.1

How do I fix this error?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Nishant Nawarkhede
  • 6,960
  • 9
  • 49
  • 67

17 Answers17

448

Upgrade pip as follows:

curl https://bootstrap.pypa.io/get-pip.py | python

Note: You may need to use sudo python above if not in a virtual environment.

(Note that upgrading pip using pip i.e pip install --upgrade pip will also not upgrade it correctly. It's just a chicken-and-egg issue. pip won't work unless using TLS >= 1.2.)

As mentioned in this detailed answer, this is due to the recent TLS deprecation for pip. Python.org sites have stopped support for TLS versions 1.0 and 1.1.

From the Python status page:

Completed - The rolling brownouts are finished, and TLSv1.0 and TLSv1.1 have been disabled. Apr 11, 15:37 UTC


For PyCharm (virtualenv) users:

  1. Run virtual environment with shell. (replace "./venv/bin/activate" to your own path)

    source ./venv/bin/activate
    
  2. Run upgrade

    curl https://bootstrap.pypa.io/get-pip.py | python
    
  3. Restart your PyCharm instance, and check your Python interpreter in Preference.

Anupam
  • 12,420
  • 12
  • 51
  • 82
  • 2
    This worked for me! I had to add `sudo` at the front because of permission denied errors. Thanks! – Simar Apr 12 '18 at 21:34
  • 34
    Requires sudo to work: `curl https://bootstrap.pypa.io/get-pip.py | sudo python` – Wallace Apr 13 '18 at 01:59
  • 2
    @Simar @Wallace yeah, may require `sudo` if not in a virtual environment. Added that bit as well in the answer now. – Anupam Apr 13 '18 at 03:33
  • 1
    If you're not in a virtual environment you may need to specify the path to your python binary, as Ilya mentions below – Chris Apr 18 '18 at 12:05
  • 4
    On OSX this worked better for me `curl https://bootstrap.pypa.io/get-pip.py | sudo -H python3` – nick fox Sep 21 '18 at 09:41
  • `curl: (35) error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version` – Andre Sep 27 '18 at 14:35
  • This Solution dint work for me. On OSX, I use @nickfox solution, I started getting some other errors ERROR: Compiling fastdtw because it depends on /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Cython/Includes/numpy/math.pxd. saw this error down the way, Download error on https://pypi.org/simple/pytest-runner/. I installed pytest-runner separately, I was able to install and solve my probelm – driven_spider May 23 '19 at 17:30
  • I needed to run: sudo -H pip3.5 install requests[security] afterwards, too. – eruciform Oct 28 '19 at 14:50
  • This curl command seems to do nothing for eternity. – 2540625 Jun 30 '20 at 21:55
  • @2540625 try running https://bootstrap.pypa.io/get-pip.py in your browser - does it return the py file? – Anupam Jul 07 '20 at 05:16
29

But if the curl command itself fails with error, or "tlsv1 alert protocol version" persists even after upgrading pip, it means your operating system's underlying OpenSSL library version<1.0.1 or Python version<2.7.9 (or <3.4 in Python 3) do not support the newer TLS 1.2 protocol that pip needs to connect to PyPI since about a year ago. You can easily check it in Python interpreter:

>>> import ssl
>>> ssl.OPENSSL_VERSION
'OpenSSL 0.9.8o 01 Jun 2010'
>>> ssl.PROTOCOL_TLSv1_2
 AttributeError: 'module' object has no attribute 'PROTOCOL_TLSv1_2'

The AttributeError (instead of expected '5') means your Python stdlib ssl module, compiled against old openssl lib, is lacking support for the TLSv1.2 protocol (even if the openssl library can or could be updated later).

Fortunately, it can be solved without upgrading Python (and the whole system), by manually installing extra Python packages -- the detailed step-by-step guide is available here on Stackoverflow.

Note, curl and pip and wget all depend on the same OpenSSL lib for establishing SSL connections (use $ openssl version command). libcurl supports TLS 1.2 since curl version 7.34, but older curl versions should be able to connect if you had OpenSSL version 1.0.2 (or later).


P.S.
For Python 3, please use python3 and pip3 everywhere (unless you are in a venv/virtualenv), including the curl command from above:
$ curl https://bootstrap.pypa.io/get-pip.py | python3 --user

Alex C.
  • 3,173
  • 13
  • 26
  • 5
    I also realized I had to run `pip3`, not just `pip` — check version with command `-V` to make sure you're running the right pip!! – Jan Aug 17 '18 at 07:22
9

Following @Anupam's answer on OS X resulted in the following error for me, regardless of permissions I ran it with:

Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: ...

What eventually worked was to download a newer pip package (9.0.3) from PyPI directly from my browser - https://pypi.org/simple/pip/, extract the contents, and then pip install the package locally:

pip install ./pip-9.0.3/

This fixed my [SSL: TLSV1_ALERT_PROTOCOL_VERSION] errors.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
T Dub
  • 91
  • 2
7

@Anupam's solution worked for me. However, I had to use sudo and specify the exact location of my virtual Python environment:

curl https://bootstrap.pypa.io/get-pip.py | sudo /Users/{your user name}/{path to python}/bin/python
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Ilya Gazman
  • 27,805
  • 19
  • 119
  • 190
6

To upgrade the local version I used a slight variant:

curl https://bootstrap.pypa.io/get-pip.py | python - --user

This problem arises if you keep your pip and packages under your home directory as described in this gist.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Hbar
  • 403
  • 3
  • 6
5

The following solution worked for me:

brew install python2

It also upgraded pip to version 1.10.1

Dov Benyomin Sohacheski
  • 5,155
  • 7
  • 31
  • 54
  • ^ this one for those of us not using the default python on OSX. I guess the question wasn't OSX specific, but anyway, if you're not running Linux, this works --- unless you're content writing over the system installed Python. – jar May 14 '18 at 14:18
  • This one worked for me, I use the python on OSX. My situation I spent a week trying to install any pip package and it failed. I uninstalled pip and from this time I can not do anything till I got this command which installed pip for me. Thank you! – amm Jul 01 '18 at 17:24
4

Check your TLS version:

python2 -c "import urllib2,json; print(json.loads(urllib2.urlopen('https://www.howsmyssl.com/a/check').read())['tls_version'])"

If your TLS version is less than 1.2 you have to upgrade it since the PyPI repository is on a brownout period of deprecating early TLS.

Source - Time To Upgrade Your Python: TLS v1.2 Will Soon Be Mandatory

You can upgrade the TLS version using the following command:

sudo apt-get update && sudo apt-get install openssl libssl-dev

This should fix your problem. Good luck!

EDIT: You can download packages using your own private python package repository regardless of TLS version. Private Python Package Repository

Vithulan
  • 170
  • 9
2

For Python2 WIN10 Users:

1.Uninstall python thoroughly ,include all folders.

2.Fetch and install the lastest python-2.7.msi (ver 2.7.15)

3.After step 2,you may find pip had been installed too.

4.Now ,if your system'env haven't been changed,you can use pip to install packages now.The "tlsv1 alert protocol version" will not appear.

tripleee
  • 139,311
  • 24
  • 207
  • 268
2

This worked for me. Add sudo before python

curl https://bootstrap.pypa.io/get-pip.py |sudo python
Sugoi Reed
  • 357
  • 3
  • 3
2

I tried all existing fixes and not working for me

I re-install python 2.7 (will also install pip) by downloading .pkg at https://www.python.org/downloads/mac-osx/

works for me after installation downloaded pkg

keypoint
  • 2,013
  • 2
  • 25
  • 53
2

I ran into this problem as well. The underlying problem is that the ssl library in Python 2.7 versions < 2.7.9 is no longer compatible with the pip mechanism.

If you are running on Windows, and you (like us) can't easily upgrade from an incompatible version of 2.7, FWIW, I found that if you copy the following files from another install of the latest version of Python (e.g. Python 2.7.15) on another machine to your installation:

    Lib\ssl.py
    libs\_ssl.lib
    DLLs\_ssl.dll

it will effectively "upgrade" your SSL layer to one which is supported; we were then be able to use pip again, even to upgrade pip.

W. Sadkin
  • 101
  • 1
  • 6
2

For all the python3 and pip3 users out there:

curl https://bootstrap.pypa.io/get-pip.py | sudo python3

and then assume you want to install pandas

pip3 install pandas --user
HoKy22
  • 3,337
  • 6
  • 30
  • 50
2

The answers of installing pip via:

  1. curl https://bootstrap.pypa.io/get-pip.py |sudo python or
  2. curl https://bootstrap.pypa.io/get-pip.py | python

did not work for me as I kept on getting the error:

Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)) - skipping
ERROR: Could not find a version that satisfies the requirement pip (from versions: none)
ERROR: No matching distribution found for pip

I had to install pip manually via:

  1. Go the pip distribution website
  2. Download the tar.gz version
  3. Unpack the file locally and cd into the directory
  4. run python setup.py install
pokkie
  • 519
  • 1
  • 8
  • 22
1

This worked for me, I installed latest version of pip and then installed the library (ciscoconfparse).

Upgrading pip:

curl https://bootstrap.pypa.io/get-pip.py | sudo /Users/{your user name}/{path to python}/bin/python
Jason Roman
  • 7,503
  • 10
  • 32
  • 35
1

I also hit this problem on my windows10 and tried all the answers but didn't solve my problem.

C:\python367\Scripts>pip install Flask

Collecting Flask Could not find a version that satisfies the requirement Flask (from versions: ) No matching distribution found for Flask

After that, I find the pip configuration file had been modified. So, I set the pip.ini as the original default configuration, re-run the pip command and it works for me!

In summary of the solution of mine:

  1. Check the pip.ini (usually under the path C:\ProgramData\pip) had been modified;

  2. If yes in step1, try to reset it to a default configuration.

lyn
  • 53
  • 6
1

myenv:

python 2.7.14

pip 9.0.1

mac osx 10.9.4


mysolution:

  1. download get-pip.py manually from https://packaging.python.org/tutorials/installing-packages/

  2. run python get-pip.py


refs:

https://github.com/pypa/warehouse/issues/3293#issuecomment-378468534

https://packaging.python.org/tutorials/installing-packages/

Securely Download get-pip.py [1]

Run python get-pip.py. [2] This will install or upgrade pip. Additionally, it will install setuptools and wheel if they’re not installed already.

Ensure pip, setuptools, and wheel are up to date

While pip alone is sufficient to install from pre-built binary archives, up to date copies of the setuptools and wheel projects are useful to ensure you can also install from source archives:

python -m pip install --upgrade pip setuptools wheel

Nick Dong
  • 3,111
  • 6
  • 39
  • 68
1

Or simply the required library just isn't in the repo. I'm Python newbie and all advices about upgrading pip finally shown as misleading. I had just to look into https://pypi.org/ , finding the library (airflow in my case) stopped at some old version, after which it was renamed. Yes, also that silly solution is also possible :-).

Tomáš Záluský
  • 6,234
  • 1
  • 26
  • 49