pip is the standard, de-facto package installer for Python. Created to be a better alternative to easy_install, It enables you to install and manage additional packages from PyPI that are not part of the standard Python library. This tutorial will show you the official, recommended way to install pip for both Python 2 and Python 3 on Ubuntu 18.04 LTS and how they will coexist peacefully.
Why do I need to care about pip for Python2 and pip for Python 3?
As the Python community grows, more and more parts of Ubuntu switched to the language. Because of that, Python 2 is now a part of the standard installation of Ubuntu. Despite the fact that Python 3 has many advantages over Python 2, the latter remains a crucial depency of the system. Removing Python 2.7 from Ubuntu 18.04 can cause a lot of trouble in the long run.
Ubuntu 18.04 ships with both Python 2 and Python 3 preinstalled. One pip installation works with only one Python distribution, so we need to separate both the command to invoke pip and the installation itself. For example,
pip, by default, calls
pip3 is equivalent to
Installing pip3 and pip from Ubuntu Repository
Start off by updating system repository using the following command :
sudo apt update
sudo apt install python3-pip python-pip -y
One the installation is completed, check and verify that
pip3 installs successfully using the following command :
pip number version may vary depending on the current updated repo, but it should look like this :
pip 19.0.3 from /usr/local/lib/python3.6/dist-packages/pip (python 3.6)
pip version can be verified using the command below :
pip 19.0.3 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
Installing pip independently using official install script
While the installation method above is fast and easy, there is another official method using the script provided by
pip development team.
First you need to verify that your system doesn’t have any existing pip installation.
pip --version Command 'pip' not found. Try: sudo apt install <deb name>
Then download the installation script by running
Proceed by using the python version of your choice to run the script. Be cautious as the script doesn’t coordinate well with the preinstalled pip version and can cause conflict if you have
get-pip.py installed over the preinstalled one.
For example, the command for installing pip for Python 3 system-wide would be
sudo python3 get-pip.py
If you want to install pip for Python 3 for the current user, run
python3 get-pip.py --user
Similarly, the command for pip-on-Python2 should be
sudo python get-pip.py OR python get-pip.py --user
Basic pip usage
Installing a package with pip
pip is made to manage the installation of third-party packages, one of the functionality is installing packages via the following command.
pip install <package_name>
Uninstalling a package
If you don’t need the package anymore you can uninstall it easily with the following command
pip uninstall <package_name>
Searching for Libraries
One can fire up the browser and search for a package on PyPI official website, but you can also do that using command line. For our example let’s look for packages related to “
requests“. The search command shows us an extensive list similar to the one below.
pip search requestsrequests-hawk (1.0.0) - requests-hawkrequests-dump (0.1.3) - `requests-dump` provides hook functions for requests.pydantic-requests (0.1.1) - A pydantic integration with requests.requests-foauth (0.1.1) - Requests TransportAdapter for foauth.org!requests-auth (4.0.1) - Easy Authentication for RequestsRequests-OpenTracing (0.0.1) - OpenTracing support for Requestsyamlsettings-requests (1.0.0) - YamlSettings Request Extensionrequests-aws4auth (0.9) - AWS4 authentication for Requestsrequests-middleware (0.1.2) - Composable HTTP middleware for requestsjupyter-requests (0.0.2) - Send requests to a Jupyter server.requests-twisted (0.1.2) - Twisted adapter for the requests library.requests-oauthlib (1.2.0) - OAuthlib authentication support for Requests.requests-ftp (0.3.1) - FTP Transport Adapter for Requests.requests-async (0.5.0) - async-await support for `requests`.requests-cache (0.5.0) - Persistent cache for requests libraryrequests-circuit (0.1.0) - A circuit breaker for Python requestsrequestor-requests (0.1.0) - Requestor Helper to request packagerequests-core (0.0.0) - A minimal HTTP Client, for Requests..........and many more.................
Batch installing depencies
Python codes distributed online often includes
requirements.txt file declaring all the depencies. If you see this file, you can run this command to install all depencies instead of manually run
pip install for each depency.
pip install -r requirements.txt
Show what files are installed and its paths
Sometimes you want to analyze a package’s code, then this command will help you locate its files.
pip show --files OnePackageName: OnePackageVersion: 1.0Location: /my/env/lib/pythonx.x/site-packagesFiles:../OnePackage/__init__.py[...]
Now you should have a basic understanding on how to install pip on your Ubuntu 18.04 and how to manage Python packages using
pip. For more information, visit the pip user guide page.