Understanding What pip Actually Is
pip is Python's package installer - the standard tool for installing and managing Python libraries. Before you can install packages like Django, requests, or numpy, you need pip running on your system. Most modern Python installations come with pip pre-installed, but if you're working with an older Python version or a minimal installation, you might need to install it manually.
Checking If pip Is Already Installed
Before going through the installation process, it's worth checking if pip is already on your system. Open your terminal or command prompt and type:
pip --version
If you see a version number like "pip 21.3.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)", you're all set. If you get a "command not found" error, you'll need to install pip. Another way to check is by running python -m pip --version, which works even if pip isn't in your system PATH.
Installing pip Using the Official Bootstrap Script
The most reliable method across all platforms is using the official pip bootstrap script. This approach works whether you're on Windows, macOS, or Linux, and it's the method recommended by the Python Packaging Authority.
Step-by-Step Installation Process
First, you need to download the get-pip.py script. Open your terminal and run:
curl /get-pip.py -o get-pip.py
This downloads the installation script to your current directory. If you don't have curl installed, you can use wget instead:
wget /get-pip.py
Once downloaded, install pip by running:
python get-pip.py
If you have multiple Python versions installed, specify which one to use:
python3 get-pip.py or python3.9 get-pip.py
The script will download and install the latest version of pip along with setuptools and wheel, which are essential for building and installing Python packages.
Dealing with Permission Issues
You might encounter permission errors during installation, especially on Unix-based systems. This happens because the script tries to install pip system-wide. If you see errors like "Permission denied," you have a few options.
You can run the command with sudo:
sudo python get-pip.py
However, this installs pip system-wide, which might not be what you want. A better approach is using the --user flag:
python get-pip.py --user
This installs pip in your user directory, avoiding system-wide changes and permission issues. The downside is that you'll need to add the user-level bin directory to your PATH to use pip.
Alternative Installation Methods
While the bootstrap script is the most universal method, there are other ways to get pip depending on your situation. Let's explore these alternatives.
Using Your System's Package Manager
Most operating systems have pip available through their package managers, which can be more convenient than the bootstrap method.
On Ubuntu or Debian Linux, run:
sudo apt update
sudo apt install python3-pip
For Fedora or RHEL-based systems:
sudo dnf install python3-pip
On macOS with Homebrew:
brew install python
Recent Homebrew Python installations include pip automatically. The advantage of using package managers is that they handle dependencies and integrate with your system's software management. The trade-off is that you might get an older pip version than the latest release.
Using Python's Ensurepip Module
Python 3.4 and later includes ensurepip, a module that can bootstrap pip. This is particularly useful if you have Python installed but missing pip. Run:
python -m ensurepip --upgrade
This command installs pip if it's missing or upgrades it to the latest version bundled with your Python installation. It's a quick solution that doesn't require downloading external scripts, though it might not give you the absolute latest pip version available.
Verifying Your pip Installation
After installation, you should verify that pip is working correctly. This step confirms that the installation succeeded and that pip can find and install packages.
Basic Verification Steps
First, check the version again:
pip --version
You should see output indicating the pip version and which Python installation it's associated with. Next, try installing a simple package to test the functionality:
pip install --upgrade pip
This command upgrades pip to the latest version, which also serves as a good test of the installation. If this works without errors, your pip installation is functional.
Finally, install a test package:
pip install requests
The requests library is a popular HTTP library for Python. If pip can download and install this package successfully, you've confirmed that your pip installation works end-to-end.
Troubleshooting Common Issues
If you encounter problems, here are solutions to common issues. If pip isn't found in your PATH, you might need to add its installation directory. On Linux/macOS, this is often ~/.local/bin for user installations or /usr/local/bin for system installations.
If you get SSL errors when trying to install packages, your system might have outdated SSL certificates. Updating your operating system or installing certifi (a Python SSL certificates package) can resolve this.
For Windows users, if the python command isn't recognized, you might need to reinstall Python and ensure the "Add Python to PATH" option is selected, or manually add Python to your system PATH.
pip vs pip3: Understanding the Difference
When working with Python installations, you might notice both pip and pip3 commands. This distinction exists because of Python 2 vs Python 3 compatibility.
When to Use Each Command
pip typically refers to Python 2's package installer, while pip3 is for Python 3. However, if you only have Python 3 installed, pip might point to Python 3's installer. The safest approach is to use pip3 explicitly when working with Python 3 projects.
You can check which Python version each command uses:
pip --version
pip3 --version
The output will show which Python interpreter each pip version is associated with. When in doubt, use the command that matches your Python executable: if you run python3 scripts, use pip3 for package management.
Managing Multiple Python Versions
If you have both Python 2 and 3 installed, you'll need to be careful about which pip you're using. Each Python installation has its own pip, and packages installed with one won't be available to the other.
You can install packages for specific Python versions using:
python3 -m pip install package-name
This ensures you're installing packages for the correct Python version, regardless of what pip or pip3 point to on your system.
Best Practices for New pip Users
Now that you have pip installed, let's cover some best practices that will save you headaches down the road.
Using Virtual Environments
One of the most important concepts in Python development is virtual environments. These create isolated Python environments where you can install packages without affecting your system-wide Python installation.
To create a virtual environment:
python3 -m venv myproject
Then activate it:
On Windows: myproject\Scripts\activate
On macOS/Linux: source myproject/bin/activate
Once activated, any pip install commands will only affect this isolated environment. This prevents package conflicts between projects and makes it easy to reproduce your development environment elsewhere.
Understanding Requirements Files
When you start working on projects, you'll want to keep track of which packages your project depends on. This is where requirements files come in.
Create a requirements.txt file listing all your project's dependencies:
pip freeze > requirements.txt
This captures the exact versions of all installed packages. To install these packages on another machine or after recreating your virtual environment:
pip install -r requirements.txt
This ensures everyone working on your project has the same package versions, which is crucial for reproducibility.
Security Considerations When Installing pip
Security is an important consideration when installing tools that download and execute code from the internet. Here's what you should know.
Verifying the Installation Script
The get-pip.py script is distributed over HTTPS, which provides some security. However, for maximum security, you can verify the script's integrity using checksums provided on the Python Packaging Authority website.
Before running the script, you might want to inspect its contents:
cat get-pip.py
While you don't need to understand all the code, looking at it can give you confidence that it's not doing anything suspicious. The script is open source and maintained by the Python community.
Running as Non-Root User
Whenever possible, install pip as a non-root user using the --user flag. This limits the potential impact if something goes wrong during installation. System-wide installations with sudo give the installation script elevated privileges over your entire system.
If you need system-wide access to pip-installed packages, consider using a package manager that provides pip as a managed package, rather than running installation scripts with elevated privileges.
Frequently Asked Questions
Why do I get a "ModuleNotFoundError: No module named 'pip._internal'" error?
This error typically occurs with very old pip versions or when there's a conflict between pip's internal modules. The solution is to upgrade pip to the latest version. Run python -m pip install --upgrade pip and the error should resolve. If that doesn't work, try reinstalling pip using the bootstrap method described earlier.
Can I install pip on Python 2?
Yes, but Python 2 reached end-of-life in 2020, and new pip versions no longer support it. If you're still using Python 2 for legacy reasons, you can install an older version of pip compatible with Python 2. However, you should strongly consider migrating to Python 3, as Python 2 no longer receives security updates or bug fixes.
How do I uninstall pip if I need to?
Uninstalling pip depends on how you installed it. If you used the bootstrap script, you can often just delete the pip installation directory. For system-wide installations, use your package manager: on Ubuntu, sudo apt remove python3-pip. For user installations, delete the contents of your user site-packages directory, typically found in ~/.local/lib/pythonX.Y/site-packages.
Verdict: Getting pip Right the First Time
Getting pip installed correctly the first time saves you considerable frustration later. The bootstrap method using get-pip.py is the most reliable cross-platform approach, though using your system's package manager is often more convenient if available. Once installed, pip becomes your gateway to Python's vast ecosystem of libraries and tools.
The key takeaways are to verify your installation, understand which pip command corresponds to your Python installation, and use virtual environments for project isolation. With these fundamentals in place, you're ready to explore thousands of Python packages that can extend your programming capabilities far beyond the standard library. And that's exactly where the real power of Python begins - not in the language itself, but in the rich ecosystem you can access through pip.