Pip is a package management system that allows you to install, remove and manage packages written in Python. It can be used to install packages from the Python Package Index (PyPI) and other indexes.
In this tutorial, we will explain how to install pip for Python 2 and 3 on CentOS 8, and cover the basics of how to use pip to manage Python packages.
Installing pip on CentOS 8
As you know, there are two versions of Python under active development, Python 2 and Python 3. By default, RHEL/CentOS 8 does not have a full system version of the python
command to avoid locking users into a specific version of Python. Instead, it gives users the option to install, configure, and run specific versions of Python.
When installing python modules globally, you should prefer to use dnf
or yum
to install python modules from the distribution repository, as they have been tested to work properly on CentOS 8. Use pip to install python modules globally only if there are no rpm packages available to install python modules.
Python 2 packages are named with the prefix “python2”, while Python 3 modules are prefixed with “python3”. For example, to install the paramiko module for Python 3, you would run:
|
|
Installing pip for Python 3 (pip3)
To install pip for Python 3 on CentOS 8, run the following command in a terminal as the root or sudo user.
|
|
This command will install Python 3.6 and pip. to run Python 3, you need to explicitly type python3
and to run pip type pip3
.
Verify that pip is correctly installed by running the following command, which will display the pip version.
|
|
The version number may vary, but it should look like this.
|
|
To be able to install and build Python modules via pip, you need to install the development tools.
Installing pip for Python 2 (pip2)
To install Python 2 and pip, enter the following command.
|
|
Verify the installation by typing the following.
|
|
The output should look like the following.
|
|
To execute Python 2, type python2
and then run the pip type pip2
.
Install the development tools.
Managing Python packages with pip
In general, you should only use pip in virtual environments. Python Virtual Environments
virtual environments allow you to install Python modules in isolated locations in a given project, without having to install them globally. This way, you don’t have to worry about affecting other Python projects.
In this section, we will cover some basic pip commands.
To install python modules using pip, run pip install
and enter the package name. For example, to install a package named twisted
, you would run the following command.
|
|
twisted is an asynchronous networking framework written in Python.
To install a specific version of the package, use the following format.
|
|
To uninstall a package, use pip uninstall
followed by the package name.
|
|
To search for packages from PyPI.
|
|
List the installed packages.
|
|
List outdated packages.
|
|
To upgrade an installed package to the latest version, use the following command.
|
|