Having trouble installing Python packages with pip? Getting that frustrating SSL certificate verification error? You’re not alone. In this post, I’ll explain what’s causing this common issue and show you how to fix it quickly and securely.
The Problem
When trying to install Python packages using pip, you might encounter this error:
SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED]
certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)'))
This error occurs when pip cannot verify the SSL certificate of PyPI (Python Package Index) servers. It’s a security feature meant to protect you from potential man-in-the-middle attacks, but it can be frustrating when you’re just trying to install packages.
Understanding the Cause
This issue typically happens because:
- Your system’s SSL certificates are not up to date
- The SSL certificates are missing entirely
- Your system can’t locate the certificate store
The Solution
There are several ways to resolve this issue. I’ll present them in order of recommended approach:
1. Install System Certificates (Recommended)
The most secure and recommended solution is to install the system certificates:
pip install pip-system-certs
This package ensures pip uses your system’s certificate store, which is typically more secure and up-to-date.
2. Configure Trusted Hosts
If the above solution doesn’t work, you can explicitly tell pip to trust PyPI’s hosts:
pip config --global set global.trusted-host "pypi.org files.pythonhosted.org"
Or use it directly in your pip install command:
pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org <package-name>
3. Update Your Certificates
On some systems, you might need to update your certificate store:
- Windows: Update Windows and Python to the latest version
- macOS: Run the “Install Certificates.command” in your Python folder
- Linux: Update ca-certificates package:
sudo apt-get update
sudo apt-get install ca-certificates
Best Practices and Security Considerations
While the trusted-host approach works, it’s important to understand that it bypasses SSL verification. This should only be used in controlled environments where you’re certain about the security of your network.
Always prefer using proper SSL certificates when possible, as they provide:
- Protection against man-in-the-middle attacks
- Verification of package source authenticity
- Secure encrypted communication
Alternative Solutions
If you’re working in a corporate environment, you might also:
- Configure pip to use your corporate proxy
- Set up a local PyPI mirror
- Use a custom certificate authority
Conclusion
SSL certificate errors can be frustrating, but they exist for a good reason – your security. The recommended approach is to install system certificates using pip-system-certs. If that doesn’t work, configuring trusted hosts is a quick fix, but remember to consider the security implications.
Remember: Security features like SSL verification are there to protect you and your code. While it might be tempting to disable them, it’s always better to fix the underlying certificate issues properly.
Have you encountered other pip-related issues? Let me know in the comments below!