1. Introduction
In the development world, knowing how to manage packages is essential to maintaining a smooth workflow and the proper functioning of applications.
Working with Python in the Linux environment, two package management tools stand out: apt-get and pip. Understanding the nuances of these tools will help us properly manage an effective development environment.
In this tutorial, we’re going to discuss the differences between these two tools, their applications, and their usage.
2. apt-get install
apt-get is a command-line tool used to handle packages in Debian-based Linux distributions like Ubuntu via the Advanced Package Tool (APT) library. We use apt-get install to install packages, Python packages included, from repositories hosted by Canonical, which is the company behind Ubuntu.
This tool simplifies how we install and manage packages, as we’re able to install the package and its dependencies with just a command.
2.1. Installation
Let’s look at the basic syntax for installing a package using apt-get:
$ sudo apt-get [options] install [package(s)]
Naturally, we have to be a root user to run apt-get, so we use sudo to grant such privileges. We use the options tag to specify flags, like -y, that automatically grant permission for the installation.
Generally, we can install a specific package or multiple packages separated by white space using this command.
2.2. Upgrading Packages
apt-get installs packages from the package lists stored in the /etc/apt/sources.list file. When we run the apt-get install command, it fetches these packages and installs them. Consequently, to upgrade these packages, we fetch them from their source repositories and update the information stored in our system repository. We do this using the apt-get update command:
$ sudo apt-get update
[sudo] password for user:
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Hit:2 http://ng.archive.ubuntu.com/ubuntu focal InRelease
Hit:3 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu focal InRelease
...
Ign:15 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 InRelease
...
Err:26 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 Release.gpg
Fetched 16.5 MB in 16s (1,012 kB/s)
Reading package lists... Done
Let’s inspect the output:
- Get: retrieve a new version of the package
- Hit: there’s no change in the package version
- Ign: the program ignores the package
- Err: shows an error occurred
Next, we upgrade them:
$ sudo apt-get -y upgrade
[sudo] password for user:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
accountsservice amd64-microcode apache2 apache2-bin
...
431 upgraded, 0 newly installed, 0 to remove and 6 not upgraded.
Need to get 809 MB of archives.
After this operation, 72.7 MB of additional disk space will be used.
Get:1 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4/multiverse amd64 mongodb-org-mongos amd64 4.4.28 [16.1 MB]
...
Ign:293 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu focal/main amd64 r-cran-ggplot2 all 3.4.4-1cran1.2004.0
...
Err:313 http://ng.archive.ubuntu.com/ubuntu focal-updates/main amd64 libjavascriptcoregtk-4.0-18 amd64 2.38.6-0ubuntu0.20.04.1
...
Here, the -y flag provides the validation apt-get needs to upgrade and install the updated packages in our environment.
Additionally, we can use apt-get to uninstall, reinstall, remove, and clean packages from our system.
3. pip install
Conversely, we use pip to download and install Python packages from the Python Package Index (PyPi). PyPi, hosted by the Python Software Foundation, is a specialized package manager that only deals with Python packages.
We use pip to install a broader range of Python packages we wouldn’t usually find using apt-get.
3.1. Installation
Unlike apt-get, we don’t necessarily need admin access to run pip commands. The basic syntax for installing a Python package is similar to that of apt-get:
$ pip install [package(s)]
Also, we can install multiple packages by separating each package with a white space.
What’s more, we can install packages peculiar to a user using the –user flag:
$ pip install --user [package(s)]
This will install the package to the Python user install directory on our platform. This is the default if we aren’t operating as root, nor are we in a virtual-env.
3.2. Upgrading Packages
We see a slight difference in the command to upgrade packages using pip. Unlike apt-get, we use the –upgrade flag to upgrade the version of packages in our system:
$ pip install --upgrade flask
Collecting flask
Downloading flask-3.0.2-py3-none-any.whl (101 kB)
|████████████████████████████████| 101 kB 246 kB/s
Requirement already satisfied, skipping upgrade: importlib-metadata>=3.6.0; python_version < "3.10" in /usr/local/lib/python3.8/dist-packages (from flask) (4.12.0)
Collecting Werkzeug>=3.0.0
Downloading werkzeug-3.0.1-py3-none-any.whl (226 kB)
|████████████████████████████████| 226 kB 151 kB/s
Requirement already satisfied, skipping upgrade: Jinja2>=3.1.2 in /usr/local/lib/python3.8/dist-packages (from flask) (3.1.2)
Requirement already satisfied, skipping upgrade: blinker>=1.6.2 in /usr/local/lib/python3.8/dist-packages (from flask) (1.6.2)
...
ERROR: flask-babel 3.0.1 has requirement Flask<3.0.0,>=2.0.0, but you'll have flask 3.0.2 which is incompatible.
Installing collected packages: Werkzeug, flask
Successfully installed Werkzeug-3.0.1 flask-3.0.2
Evidently, we see that this will download and install the newer version of the package, skipping the ones that are up to date.
Similarly, we can also use pip to uninstall a package.
4. Key Differences
We’ve looked at both package management tools and their applications. Therefore, let’s identify their key differences.
We can split their differences into five main categories:
Categories
apt-get install
pip-install
Access control
requires root/admin access
allows flexibility of user mode installation using –user flag
Scope
installs packages in a system-wide location
can install packages in a virtual-env
Package range
focuses primarily on system-wide packages, hence the limited Python packages
specializes in Python packages, leading to more available Python packages
Version control
Canonical can only host the latest version or the most recent update of any package, so we can only install the most recent version of the package.
We can decide the package version we want by specifying the version during installation
Dependency management
automatically resolves and manages package dependencies, ensuring system-wide compatibility
manages python package dependencies within the project’s environment
5. Usage
Although we can use both commands to install our Python packages effortlessly, there are use cases in which we can favor one over the other.
Particularly if we’re working in a virtual-env, or we desire to install an older version of the package, or we desire a package hosted by PyPi, then we favor using pip install. Otherwise, we can use any of the two commands to get the job done.
6. Conclusion
In this article, we’ve seen and discussed the two popular package management tool commands: apt-get install and pip install, identifying their applications and usage. We also identified the key differences between both, showing the variance in areas of version control, dependency management, scope, and access privileges.