Yes, you can fix this. The error happens because the Python package installer (pip) is not in your system’s command path. You can resolve it by installing the Python3 pip package using the Ubuntu terminal. This guide shows how to add pip to your path so you can run commands like pip install requests from anywhere.
What You'll Need
- A computer running Ubuntu Linux (20.04, 22.04, or 24.04 are the most common versions).
- Access to a user account with sudo privileges. You need this to install software system-wide.
- A working internet connection. The installation requires downloading files from the official Ubuntu package repositories.
- A terminal window (often accessed by pressing Ctrl + Alt + T).
Step-by-Step
- Open the terminal. Click the application menu in the top-left corner, type "Terminal" or "Konsole" (depending on your desktop environment), and open it.
- Update your package lists. Before installing new software, it is best practice to ensure your list of available packages is current. Type the following command and press Enter:
sudo apt update
- Install the Python3 pip package. Ubuntu includes pip as part of its standard repositories. To install it, run the command below. The
sudoprefix asks for your password (you won’t see the dots appear as you type).
sudo apt install python3-pip
This command installs the pip executable for Python3. On older Ubuntu versions, the package was named python-pip. On modern versions, it is specifically python3-pip.
- Verify the installation. Once the installation finishes, type the following command to check if pip is now available:
pip3 --version
If the command returns a version number (e.g., pip 23.0.1 from ...), the installation was successful.
- Use pip to install a package. Now that pip is installed, you can use it to install Python libraries. For example, to install the popular
requestslibrary for making HTTP requests, run:
pip3 install requests
You can now import requests in your Python scripts.
Common Mistakes
The most common mistake is trying to run pip without the 3 suffix. If you type pip, the system will look for a program named pip that might not exist or might be an old, incompatible version.
Another common issue arises when python3-pip is not installed on a minimal installation of Ubuntu (like some Docker containers or server setups). In these cases, the apt install python3-pip command will fail with an error like E: Unable to locate package python3-pip.
Tips
When installing packages, you can specify a specific version to avoid breaking changes in your project. For example, to install a specific version of a library, use the == operator:
pip3 install requests==2.28.0
It is also good practice to create a virtual environment for your projects. This keeps your project dependencies isolated from the system-wide Python installation. You can create a virtual environment using the venv module:
python3 -m venv myenv
After creating the environment, you can activate it by running the activate script:
source myenv/bin/activate
Once activated, your command prompt will change to show the environment name.
Troubleshooting
If the command is not found
If you see the error command not found or bash: pip: command not found even after running the steps above, ensure you are using the correct command. Always use pip3 when working with Python3 on Ubuntu. If pip3 is still not found, try reinstalling the package:
sudo apt install --reinstall python3-pip
If the installation fails
If the apt install python3-pip command fails, check your internet connection. The error Unable to locate package means Ubuntu cannot find the software in its list of available packages. This often happens on very minimal systems. You may need to enable the universe repository first:
sudo add-apt-repository universe
Then, run the update and install commands again.
Permission errors
When you install packages globally using pip3 install without the --user flag, it attempts to write to system directories. If you do not have sudo privileges or if the directories are protected, you will get a Permission denied error. To install for your user only, use the --user flag:
pip3 install --user requests
Conflict with apt
On some systems, a package installed via apt (like python3-pip) may conflict with a package installed via the get-pip.py script. If you have previously installed pip using the get-pip.py script, you may need to remove it before using the apt version. You can remove the get-pip.py installed pip by running:
sudo apt remove python3-pip
Then reinstall it using the apt method described in the steps above.
Frequently Asked Questions
Is pip installed by default on Ubuntu?
No, pip is not included in the default installation of Ubuntu. You must install the python3-pip package using the package manager.
What is the difference between pip and pip3?
On systems with both Python 2 and Python 3, pip installs packages for Python 2, while pip3 installs packages for Python 3. On modern Ubuntu systems, which use Python 3 by default, using pip3 is the standard approach.
Why can't I run pip without the 3?
If you type pip and get a command not found error, it is because Ubuntu does not include a pip executable in the system path. The executable is named pip3.
How do I upgrade pip to the latest version?
You can upgrade pip using the built-in self-upgrade command. Run the following command to update pip to the latest version from the Python Package Index:
python3 -m pip install --upgrade pip
Can I install pip on Ubuntu without sudo?
Yes, you can install pip for your user only. This avoids permission errors and keeps your system clean. Use the --user flag during installation:
python3 -m pip install --user pip