Pip Install Fails With "Connection Error: [Ssl: Certificate_verify_failed] Certificate Verify Failed (_ssl.c:598)"

Better Stack Team
Updated on October 7, 2024

The error "ConnectionError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:598)" occurs when pip is unable to verify the SSL certificate of the repository from which it is trying to download a package. This can happen for several reasons, including an outdated or misconfigured CA certificates bundle or network issues. Below are several ways to resolve this issue.

Solution 1: Upgrade pip

Ensure that you have the latest version of pip, as older versions may have issues with SSL certificates.

 
python -m pip install --upgrade pip

Solution 2: Add the Trusted Host Option

If you're certain that the repository you are connecting to is trustworthy, you can bypass SSL certificate verification by adding the --trusted-host option:

 
pip install <package_name> --trusted-host pypi.org --trusted-host files.pythonhosted.org

For example:

 
pip install requests --trusted-host pypi.org --trusted-host files.pythonhosted.org

Solution 3: Install the certifi Package and Use it with pip

The certifi package provides Mozilla's CA Bundle in Python. You can use this to ensure that pip uses the correct CA certificates.

  1. Install certifi:

     
    pip install certifi
    
  2. Locate the path to certifi’s certificate:

     
    python -m certifi
    
  3. Export the SSL_CERT_FILE environment variable to point to the certifi certificate bundle:

    • On Windows:

       
      set SSL_CERT_FILE=C:\\path\\to\\cacert.pem
      
 
- On **Linux/macOS**:

    ```bash
    export SSL_CERT_FILE=/path/to/cacert.pem
    ```
  1. Run pip again:

     
    pip install <package_name>
    

Solution 4: Manually Update certifi Certificate

If the issue persists, you can manually update the certificates used by pip.

  1. Download the latest cacert.pem file from the certifi GitHub repository.
  2. Replace the existing cacert.pem file in your Python installation's certifi directory with the newly downloaded file. The directory can typically be found here:
    • Windows: C:\\PythonXX\\Lib\\site-packages\\certifi
    • macOS/Linux: /usr/local/lib/pythonX.X/site-packages/certifi

Solution 5: Disable SSL Verification (Temporary Solution)

As a last resort, you can disable SSL verification entirely. This is generally not recommended due to the security implications.

  1. Create or edit the pip.conf file:
    • Windows: %APPDATA%\\pip\\pip.ini
    • Linux/macOS: ~/.pip/pip.conf or ~/.config/pip/pip.conf
  2. Add the following lines:

     
    [global]
    trusted-host = pypi.org
    disable-pip-version-check = true
    
  3. Try running pip install again.

Solution 6: Ensure System Time is Correct

Sometimes SSL errors occur if your system's date and time are incorrect. Make sure your system time is accurate and synchronized with a time server.

Summary

The SSL verification error with pip is typically due to issues with certificate verification. You can resolve it by updating pip, configuring the trusted-host option, using the certifi package, or as a last resort, disabling SSL verification. Each method has different security implications, so it's important to choose the one that best fits your situation.

Got an article suggestion? Let us know
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github