Getting to know the Python installer
Command Line – pip3
As a replacement for pip, pip3 offers a complete solution for binary packages. Here's how to get started with this increasingly popular Python installer.
You can tell the popularity of Linux tools by how often they are used to install packages outside of distro repositories. For years, source packages have been installed by the trinity of commands configure
, make
, and make install
for compiling. More recently, deb packages have become the norm, usually configured for Ubuntu. Both these installation choices are still popular, but, today, the popularity of the Python programming language is reflected in the increasing use of pip3 [1].
Pip3 is the Python installer for Python 3.x releases. It replaces pip, which is used for earlier versions of Python, as well as easy_install
, and can also work with eggs [2] and wheels [3], two other standard Python tools designed to simplify packaging. In basic functionality, it is similar to package managers in other programming languages, such as npm in JavaScript or gem in Ruby. You should note, however that in a command, pip3 is often referenced as pip
.
Like the packages in a distribution's repositories, pip3 automatically handles dependencies, installing them first to avoid the problems of a half-installed package. In addition, pip3 packages are simpler to build than their predecessors and include detailed error and warning messages, as well as a requirement file [4] that makes cloning installations on multiple machines easier. In fact, pip3 is so similar to deb and RPM that it is often talked about in terms of distributions and packages, a habit that causes considerable confusion. Regardless, pip3 has advantages for developers and users alike.
Recent distributions often install Python 3 along with pip3 and pip by default. You can check which you have by running:
python --version pip3 --version
Some applications require a specific earlier version of Python, which you can download from the Python website [5]. If you need to upgrade pip3, run:
python3 -m pip install --upgrade pip
If you need to install or upgrade pip, you can usually install it with
python -m ensurepip --default-pip
or by one of the other means listed on the Python packaging page [6].
You may also want to sandbox the packages you install in a virtual environment. Python has several tools for creating a virtual environment, including virtualenv
, pyvenv
, and venv
. In Python 3.4 and later, the preferred tool is venv
[7], which may need to be installed separately. To create the virtual environment, first install Python and add the directory to your $Path
variable if necessary. Then create the directory for the environment and activate it with:
python3 -m venv DIRECTORY-PATH source DIRECTORY/activate
When you install a package or do any other work in Python, you will be using the specified virtual environment so long as you are in its directory until you type deactivate
.
Using pip3
When a project under development uses pip3 to install, it will probably give the command to install its packages. However, you may also find packages to download in the Python repositories. The main general repository is the Python Package Index (PyPI, aka the Cheese Shop) [8]. However, there are dozens of others. Some are general repositories like Awesome Python. Others are concerned with special interests, such as scikit-learn and Python Design Patterns, or focus on major Python specific applications such as Ansible or Django.
The basic command structure for installing a package using pip3 depends on where the package is in relation to your machine. In each case, the -m
option tells Python to search for the specified package, followed by the pip3 sub-command (install
), the package, and then specifications for the version, if any. Pip for Python 2 uses a similar structure, but as of January 2021 it is no longer supported (Figure 1), although it may still work.
There are several common installation choices. To install the latest version, use
python3 -m pip install "PACKAGE"
Specify the directory for the local package file, the URL for version control, or the Python repository. (The package name alone is needed for PyPI, as in Figure 2).
To install a specific version, use:
python3 -m pip install "PACKAGE==VERSION"
To install a version that is greater than or equal to one version and less than another version, use:
python3 -m pip install "PACKAGE>=VERSION1,<HIGHER-VERSION"
To install a version compatible with a certain version, use:
python3 -m pip install "PACKAGE~=VERSION"
This is usually a point release of the version specified.
Notice that because a connection to PyPI automatically exists, only the package name needs to be specified for packages in PyPI. The same is true for local packages on $PATH
or the current directory, or the directory for storing wheels (usually /usr/share/python-wheels
). With the -requirement FILE
(-r FILE
) option, you can also use a requirement file for the package to install or the URL to a project's version control system (VCS).
Other formats are available to help users deal with the variety of different Python releases.
Besides install
, pip3 supports a number of related commands:
uninstall
: Removes a packagelist
: Displays all installed packages on a system (Figure 3)
show
: Displays information about a package (Figure 4)
search
: Locates a package on PYPI
In addition, pip3 includes several commands for building packages, such as freeze
, which outputs a requirement file (Figure 5) or a list of dependencies that can be used in packaging or troubleshooting, and wheel
, which creates a type of file for streamlining package installation. However, these commands are separate articles in themselves.
You can also use options to refine a pip3 command. The --upgrade
(-u
) option ensures you install the very latest version of a package, although at times it may cause problems with dependencies. Similarly, you may want to use --eggs
, for compatibility with older systems that have eggs installed. You can also install custom options with --install OPTION
, such as:
--install-option ="--install-scripts=/usr/local/bin"
For custom requirement files, you can use --requirement FILE
(-r
).
Still other install options cover the usual range of options such as forcing a reinstall (--force-reinstall
), not installing dependencies (--no-deps
), or specifying the target directory for packages (-t
, --target DIRECTORY
). You can even use --pre
to override pip3's default setting to search for only stable releases and search for pre-release and development builds.
When uninstalling, pip3 offers the option to delete all the packages in a requirement file with --requirement FILE
(-r FILE
). If necessary, multiple requirement files can be specified. With --yes
(-y
), pip3 will not ask for confirmation before deleting files. More general options are available as well, such as --verbose
(-v
), which has three levels of output, or --quiet
(-q
), which gives less output. You can also change the default logfile (~/.pip/pip.log
), positioning a standard log with --log-path PATH
or a verbose log with --log PATH
.
The Next Stage
Pip3 is a complete solution for binary packages. It offers compatibility with many of the other package tools that Python has evolved over the years, and – just as importantly – does so while living up to Python's reputation for clarity and organization. You have only to contrast pip3 to Debian's apt-get
to see what a difference paying attention to structure can make.
Probably, the information given here is more than enough to guide those unfamiliar with pip3. However, to give a complete picture, I should also mention setuptools
[9], a general command for managing packages. While pip3 is more than enough for small-scale operations, such as when trying an application still in development, setuptools
is better suited for large-scale operations. Should you find pip3 straining to meet your packaging needs, have a look at setuptools
. If the popularity of Python continues to grow, I have a hunch that we will be seeing a lot more of both.
Infos
- pip3: https://www.linuxscrew.com/install-pip
- Eggs: http://peak.telecommunity.com/DevCenter/PythonEggs
- Wheels: https://realpython.com/python-wheels/
- Requirement file: https://blog.jcharistech.com/2020/11/02/how-to-create-requirements-txt-file-in-python/
- Downloading Python: https://www.python.org/downloads/
- Python packaging: https://packaging.python.org/tutorials/packaging-projects/
- venv: https://docs.python.org/3/library/venv.html
- PyPI: https://pypi.org/
- setuptools: https://setuptools.readthedocs.io/en/latest/index.html
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.