When using Ubuntu, I found that Python's pip and pip3 were not pre-installed. I unexpectedly ran into trouble during the installation, so here's the record of how to solve it.
- Introduction
- Installing pip/pip3 on Ubuntu
- Conclusion: Update Before Installation
- Final Thoughts
- References
Introduction
The pip
command is convenient for installing Python packages.
However, Ubuntu does not have pip pre-installed by default.
When trying to install pandas using pip on a fresh Ubuntu system, the following error occurs:
$ pip install pandas Command 'pip' not found, but can be installed with: sudo apt install python-pip
The error displays, suggesting a pip installation method. However, even this recommended method fails initially.
After trial and error, I successfully installed pip / pip3 and will document the process.
# Environment in this post Ubuntu 18.04.1 LTS
Note: This article was translated from my original post.
Installing pip/pip3 on Ubuntu
Premise: pip vs pip3
First, let me clarify the difference between pip and pip3.
pip3
specifically invokes pip for Python 3, while pip
can be set to either Python 2 or Python 3.
Initially, I assumed all pip commands were simply pip
, with pip3
as an optional command.
However, in environments like Ubuntu with both Python 2 and 3 installed, this difference is significant.
By default, pip
points to Python 2, while pip3
points to Python 3.
Conversely, if only Python 2 or Python 3 is installed, simply typing pip
will default to the respective Python version.
This behavior matches the python
and python3
commands.
Executing Python 3 scripts requires the python3
command, whereas using python
defaults to Python 2.
In this article, I'll install pip and pip3 separately.
Failed attempt with apt install
: pip✕ / pip3✕
Let's attempt to install pip and pip3.
As mentioned above, running pip suggests the following:
For pip: sudo apt install python-pip
$ pip install pandas Command 'pip' not found, but can be installed with: sudo apt install python-pip
For pip3: sudo apt install python3-pip
$ pip3 install pandas Command 'pip3' not found, but can be installed with: sudo apt install python3-pip
However, both methods result in errors and fail to install. We need an alternative method.
Installing pip with "get-pip.py": pip○ / pip3✕
Searching for another solution, I found a method using "get-pip.py". This method requires downloading and executing "get-pip.py".
First, install the curl
command since it is not available by default:
$ sudo apt install curl
Then download "get-pip.py":
$ curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
Execute "get-pip.py" with the python
command:
$ sudo python get-pip.py
This successfully installed pip. Confirm the installation:
$ pip --version pip 19.0.3 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
Indeed, pip is successfully installed.
Next, I'll try installing pip3.
After briefly checking the code inside "get-pip.py", I found that it supports both Python2 and Python3 environments, so I'll run "get-pip.py" with python3
just like I did with pip.
$ sudo python3 get-pip.py ~ ModuleNotFoundError: No module named 'distutils.util'
Unfortunately, this method resulted in an error that I could not resolve easily.
Solution: Run apt update
before apt install
: pip○ / pip3○
After searching extensively, a surprisingly simple solution worked. Running updates before installation fixed the problem.
Update using the following commands:
$ sudo apt update $ sudo apt upgrade
Then retry the pip3 installation:
$ sudo apt install python3-pip
The installation succeeded this time without error.
Check pip3 installation:
$ pip3 --version pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
pip3 installed successfully.
Conclusion: Update Before Installation
Always update first:
sudo apt update sudo apt upgrade
Then, install pip and pip3:
sudo apt install python-pip sudo apt install python3-pip
If these fail, use "get-pip.py" to install pip/pip3:
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" sudo python get-pip.py sudo python3 get-pip.py
Final Thoughts
I documented my struggles with installing pip/pip3 on Ubuntu.
Ultimately, the solution was simple—update before installing. This is a valuable lesson applicable to many other package installations as well.
I hope this article helps someone!