What is a Package Manager? Why Do We Need It, and How Does It Work?
A package manager is a tool that helps you manage applications, libraries, and dependencies. It allows you to install, update, and remove packages. These packages can be standalone applications or libraries used in your code. In simple terms, a package manager handles packaged software.
The primary purpose of a package manager is to manage installed packages, track dependencies, and resolve conflicts. It is not necessarily meant for downloading packages, but some package managers include their own repositories, allowing you to download packages directly. However, downloading is an optional feature.
For example, in Debian-based Linux distributions, the actual package manager is dpkg
, not apt
or apt-get
. apt
simply retrieves packages from a remote repository and hands them over to dpkg
for installation. On the other hand, in Arch Linux, the package manager is pacman
, which can both download and install packages. Other examples include npm
, pip
, gem
, and cargo
. A package manager doesn’t have to be an operating system’s application manager—it can also manage libraries.
What Does “Installed” Mean?
For a software application to run, it requires its dependencies. When all dependencies are provided, the software can execute successfully. When you launch an application, it looks for the required files and directories, and if found, it runs. Many programs can function as long as they have their dependencies, regardless of where they are stored—whether on your desktop, home directory, a USB drive, or even a CD/DVD. However, managing software this way is impractical, as you might forget where you stored them or even lose track of installed applications.
A package manager keeps a record of all installed packages, their locations, associated files, and permissions. Most Unix-like systems include an install
command, which allows package managers to install and track packages. Most package managers use a specific package format that acts as a recipe for installing software. These formats often rely on the install
command to place files correctly. A common practice is to use a fake root directory during installation; once the installation is completed, the package manager moves the files to their correct locations and updates its database.
Here’s an example of a PKGBUILD
file for my old game, “Sudo Mice”:
bash# Complete PKGBUILD can be found here: https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=sudo-mice-bin
# Maintainer: Furkan Baytekin (Elagoht) <[email protected]>
# Maintainer: Enes Baytekin <[email protected]>
...
install -d "$pkgdir/usr/share/applications"
install -Dm644 "SudoMice.desktop" -t "$pkgdir/usr/share/applications"
install -d "$pkgdir/usr/share/sudomice"
...
The install
command uses specific options:
-
-d
: Creates a directory. -
-D
: Creates all necessary parent directories before copying the file. -
-t
: Specifies the target directory for copied files.
For example, install -d "$pkgdir/usr/share/applications"
creates the applications
directory inside usr/share
, and install -Dm644 "SudoMice.desktop" -t "$pkgdir/usr/share/applications"
copies the SudoMice.desktop
file into it. Once this fake installation is completed, the package manager moves files to the correct locations and updates its database, making the Sudo Mice application accessible in the application menu.
How Do Package Managers Work?
Let’s use rpm
as an example. rpm
requires an .rpm
package file to install software. This package format is used in Red Hat-based Linux distributions. Installing an .rpm
package looks like this:
bashrpm -i /path/to/package.rpm
However, rpm
does not download packages—it only installs them. You must first obtain the package file manually, for example:
bashwget https://example.com/package.rpm
This approach requires knowing the exact URL of each package, which is impractical. This is where package repositories come into play.
Package Repositories
A package repository is a storage location for packages, which can be public or private, local or remote. Public remote repositories are typically maintained by package manager vendors, ensuring safe and centralized access to packages.
For example, Fedora’s official mirrors include this domain:
http://linus.iyte.edu.tr/linux/fedora/… (Thanks to IYTE for hosting a mirror in Turkey!)
Repositories provide a structured way to distribute software, so users don’t need to manually find package URLs. However, downloading packages manually is still inconvenient, which is why we use helper tools.
Helper Tools
Helper tools simplify package management by handling downloads and installations automatically. They use a mirror list to determine where packages should be fetched from. The order of mirrors in this list matters—if a package exists in multiple repositories, the first available one is used.
Example repository file for Fedora:
bash# /etc/yum.repos.d/fedora.repo
[fedora]
name=Fedora $releasever - $basearch
...
metalink=https://mirrors.fedoraproject.org/metalink?repo=fedora-$releasever&arch=$basearch
Helper tools don’t just download packages—they also resolve dependency trees and install dependencies in the correct order. For example:
bashdnf install package-name
This command ensures that all dependencies are downloaded and installed correctly.
How Are Packages Created?
Packages are created using packaging tools. For rpm
, tools like rpm-build
and rpmdevtools
are used. While we won’t cover package creation in detail here, it’s important to note that package creation includes defining dependencies to ensure proper installation.
Before the Internet Was Everywhere
Before widespread internet access, people obtained software from physical media like CDs, DVDs, and floppy disks. This often led to dependency nightmares—installing a package required tracking down multiple other packages from different sources. With the internet and modern package managers, these issues are largely eliminated, making installations seamless. Helper tools resolve dependency trees and install dependencies in the correct order.
Software Library Managers
Library managers function similarly to package managers but are focused on programming languages. Examples include:
-
pip
for Python -
cargo
for Rust -
gem
for Ruby -
npm
for Node.js
For instance, installing a package with npm
:
bashnpm install package-name
This installs the package and stores it in the node_modules
directory. Running npm install
without arguments installs all dependencies listed in package.json
, resolving the dependency tree automatically.
Windows and macOS Package Managers
Package managers aren’t exclusive to Linux. Windows and macOS also have their own:
-
Windows:
winget
(built-in but with limited repositories) andchocolatey
(popular but requires manual installation). -
macOS:
brew
(widely used but not built-in; must be installed manually).
These package managers work similarly to their Linux counterparts, simplifying software management.
Conclusion
Thanks to package managers, helper tools, and repositories, installing, updating, and removing software has become an effortless process. We owe a lot to the engineers who built these systems, making software management more efficient and user-friendly.
Album of the day: