Not able to install Python packages [SSL: TLSV1_ALERT_PROTOCOL_VERSION]

162,508

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

@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

Solution 5

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.

Share:
162,508

Related videos on Youtube

Nishant Nawarkhede
Author by

Nishant Nawarkhede

Working on chatbot developement.

Updated on March 18, 2020

Comments

  • Nishant Nawarkhede
    Nishant Nawarkhede about 4 years

    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?

  • Simar
    Simar about 6 years
    This worked for me! I had to add sudo at the front because of permission denied errors. Thanks!
  • Wallace
    Wallace about 6 years
    Requires sudo to work: curl https://bootstrap.pypa.io/get-pip.py | sudo python
  • Anupam
    Anupam about 6 years
    @Simar @Wallace yeah, may require sudo if not in a virtual environment. Added that bit as well in the answer now.
  • Chris
    Chris about 6 years
    If you're not in a virtual environment you may need to specify the path to your python binary, as Ilya mentions below
  • jar
    jar almost 6 years
    ^ 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.
  • amm
    amm almost 6 years
    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!
  • Jan
    Jan over 5 years
    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!!
  • nick fox
    nick fox over 5 years
    On OSX this worked better for me curl https://bootstrap.pypa.io/get-pip.py | sudo -H python3
  • Andre
    Andre over 5 years
    curl: (35) error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version
  • driven_spider
    driven_spider almost 5 years
    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/python‌​3.5/site-packages/Cy‌​thon/Includes/numpy/‌​math.pxd. saw this error down the way, Download error on pypi.org/simple/pytest-runner. I installed pytest-runner separately, I was able to install and solve my probelm
  • eruciform
    eruciform over 4 years
    I needed to run: sudo -H pip3.5 install requests[security] afterwards, too.
  • 2540625
    2540625 almost 4 years
    This curl command seems to do nothing for eternity.
  • Anupam
    Anupam almost 4 years
    @2540625 try running bootstrap.pypa.io/get-pip.py in your browser - does it return the py file?
  • Kevin
    Kevin almost 3 years
    I'm stuck with Python 2.6.6 - the script doesn't work with anything older than Python 3.6.