Linux Basics #4 - Packages

Last Edited: 11/14/2024

This blog post introduces about packages in Linux.

DevOps

Now that we understand the basic bash commands and Linux filesystem, we can move on to installing packages to do more than the default configurations.

What Are Packages?

A package is a distribution of software comprising multiple files, like Google Chrome or VS Code, bundled into one archive file. This archive file includes metadata containing the software's name, purpose, version, dependencies, and more. The upstream providers compile the code and create installation instructions, while package maintainers review, manage, and distribute these as packages.

Packages

Package managers often work closely with software repositories where packages are stored. We can specify the location of these repositories or source links so the system can download packages from them. Linux distributions come with pre-approved sources to get packages, which can be seen at /etc/apt/sources.list.d/ubuntu.sources on our system. You can visit the link in the file from your browser to see the list of available packages and their versions.

Archive Files

When you visit the repository link, you will see many .deb files and some .tar.gz files. The .tar.gz files are archived by tar and compressed by gzip. gzip (or GNU zip) is a compression tool similar to zip, which compresses and decompresses files in Linux. You can use commands like gzip file and gunzip file.gz to compress and decompress files, but gzip cannot bundle multiple files into one archive. This is where tar comes into play, allowing you to create and unpack archives.

To create an archive, you can use tar cvf new.tar file1 file2, where new.tar is created from file1 and file2, and cvf stands for create, verbose, and filename. To unpack new.tar, you can use tar xvf new.tar, where xvf stands for extract, verbose, and filename. Since .tar archives are often compressed with gzip to create .tar.gz files, there are commands like tar czf new.tar.gz to compress and archive, and tar xzf new.tar.gz to decompress and unpack.

The .deb files are ar archive files containing a Debian binary (version), a control archive (metadata in .tar.gz), and a data archive (actual files in .tar.gz). This format is processed by the package management command dpkg, which is set up for Debian-based Linux systems. Installation and removal of .deb packages can be done with dpkg -i package.deb and dpkg -r package.deb, respectively, and you can check the list of installed packages using dpkg -l.

Advanced Package Tool

While dpkg allows you to install and remove packages from pre-approved sources, it doesn’t resolve dependencies. If a package has 10 dependencies, you would need to install each of these packages and their dependencies manually. To simplify this, Debian-based Linux systems use the Advanced Package Tool, or apt, which automatically retrieves, configures, and installs packages and their dependencies.

To install and remove a package, simply run apt install package_name and apt remove package_name. You can also check information about a package with apt show package_name and update the repositories using apt update. As practice, try installing the vim package and checking its information using apt (we will cover how to install and use vim in the next article). While we often work primarily with apt and not with dpkg, tar, and gzip, it’s important to understand what they are to troubleshoot issues effectively instead of treating apt and packages as magic.

Sources

  • Linux Journey. n.d. Packages. Linux Journey.