Working on the Command Line
With so much software available for installation, it is no surprise that Debian-based distros have many ways to manage software installation. At their root, however, they all use Debian’s world-renowned Advanced Package Tool (APT). A person posting on Slashdot.com once said, “Welcome to Slashdot. If you can’t think of anything original, just say how much APT rocks and you’ll fit right in.” You see, even though many other distros have tried to equal the power of APT, nothing else even comes close.
Why is APT so cool? Well, it was the first system to properly handle dependencies in software. Other distros, such as Red Hat, used RPM files that had dependencies. For example, an RPM for Gimp would have a dependency on Gtk, the graphical toolkit on which Gimp is based. As a result, if you tried to install your Gimp RPM without having the Gtk RPM, your install would fail. So, you grab the Gtk RPM and try again. Aha: Gtk has a dependency on three other things that you need to download. And those three other things have dependencies on 20 other things. And so on, and so on, usually until you can’t find a working RPM for one of the dependencies, and you give up.
APT, on the other hand, was designed to automatically find and download dependencies for your packages. So, if you want to install Gimp, it downloads Gimp’s package and any other software it needs to work. No more hunting around by hand, no more worrying about finding the right version, and certainly no more need to compile things by hand. APT also handles installation resuming, which means that if you lose your Internet connection part-way through an upgrade (or your battery runs out, or you have to quit, or whatever), APT picks up where it left off the next time you rerun it.
Day-to-Day Usage
To enable you to search for packages both quickly and thoroughly, APT uses a local cache of the available packages. Try running this command:
matthew@seymour:~$ sudo apt-get update
The apt-get update command instructs APT to contact all the servers it is configured to use and download the latest list of file updates. If your lists are outdated, it takes a minute or two for APT to download the updates. Otherwise, this command executes it in a couple of seconds.
After the latest package information has been downloaded, you are returned to the command line. You can now ask APT to automatically download any software that has been updated, using this command:
matthew@seymour:~$ sudo apt-get upgrade
If you have a lot of software installed on your machine, there is a greater chance of things being updated. APT scans your software and compares it to the latest package information from the servers and produces a report something like this:
mmatthew@seymour:~$ sudo apt-get upgrade Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be upgraded: cabextract google-chrome-beta icedtea6-plugin language-pack-en language-pack-en-base language-pack-gnome-en language-pack-gnome-en-base libfreetype6 libfreetype6-dev libsmbclient libwbclient0 openjdk-6-jre openjdk-6-jre-headless openjdk-6-jre-lib samba-common samba-common-bin smbclient upstart winbind xserver-common xserver-xorg-core 21 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Need to get 84.8MB of archives. After this operation, 623kB of additional disk space will be used. Do you want to continue [Y/n]?
Each part of that report tells you something important. Starting at the top, the line “the following packages will be upgraded” gives you the exact list of packages for which updates are available. If you’re installing new software or removing software, you see lists titled “The following packages will be installed” and “The following packages will be removed.” A summary at the end shows a total of 21 packages that APT will upgrade, with 0 new packages, 0 to remove, and 0 not upgraded. Because this is an upgrade rather than an installation of new software, all those new packages take up only 623KB of additional space. Although you have an 84.8MB download, the packages are overwriting existing files.
It’s important to understand that a basic apt-get upgrade never removes software or adds new software. As a result, it is safe to use to keep your system fully patched because it should never break things. However, occasionally you will see the “0 not upgraded” status change, which means some things cannot be upgraded. This happens when some software must be installed or removed to satisfy the dependencies of the updated package, which, as previously mentioned, apt-get upgrade will never do.
In this situation, you need to use apt-get dist-upgrade, so named because it’s designed to allow users to upgrade from one version of Debian/Ubuntu to a newer version—an upgrade that inevitably involves changing just about everything on the system, removing obsolete software, and installing the latest features. This is one of the most-loved features of Debian because it enables you to move from version to version without having to download and install new CDs. Keeping regular upgrades and distro upgrades separate is very useful for making sure that security updates and simple bug fixes don’t change software configurations that you may be counting on, especially on a machine that needs to be consistently available and working, such as a server.
Whereas apt-get upgrade and apt-get dist-upgrade are there for upgrading packages, apt-get install is responsible for adding new software. For example, if you want to install the MySQL database server, you run this:
matthew@seymour:~$ sudo apt-get install mysql-server
Internally, APT queries “mysql-server” against its list of software and find that it matches the mysql-server-5.5 package. It then finds which dependencies it needs that you don’t already have installed and gives you a report like this one:
matthew@seymour:~$ sudo apt-get install mysql-server [sudo] password for matt: Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: libaio1 libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18 libnet-daemon-perl libplrpc-perl libterm-readkey-perl mysql-client-5.5 mysql-client-core-5.5 mysql-server-5.5 mysql-server-core-5.5 Suggested packages: libipc-sharedcache-perl tinyca mailx The following NEW packages will be installed: libaio1 libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18 libnet-daemon-perl libplrpc-perl libterm-readkey-perl mysql-client-5.5 mysql-client-core-5.5 mysql-server mysql-server-5.5 mysql-server-core-5.5 0 upgraded, 13 newly installed, 0 to remove and 0 not upgraded. Need to get 26.8 MB of archives. After this operation, 96.2 MB of additional disk space will be used. Do you want to continue [Y/n]?
This time, you can see that APT has picked up and selected all the dependencies required to install MySQL Server 5.5, but it has also listed one recommended package and two suggested packages that it has not selected for installation. The “recommended” package is just that: The person who made the MySQL package (or its dependencies) thinks it would be a smart idea for you to also have the mailx package. If you want to add it, press N to terminate apt-get and rerun it like this:
matthew@seymour:~$ sudo apt-get install mysql-server mailx
The “suggested” packages are merely a lower form of recommendation. They don’t add any crucial features to the software you selected for install, but it’s possible that you might need them for certain non-crucial (to the main piece of software being installed) features or tasks.
If you try running apt-get install with packages you already have installed, APT considers your command to be apt-get update and looks to see whether new versions are available for download.
The last day-to-day package operation is removing things you no longer want, which you do through the apt-get remove command, as follows:
matthew@seymour:~$ sudo apt-get remove firefox
Removing packages can be dangerous because APT also removes any software that relies on the packages you selected. For example, if you were to run apt-get remove libgtk2.0-0 (the main graphical toolkit for Ubuntu), you would probably find that APT insists on removing more than a hundred other things. The moral of the story is this: When you remove software, read the APT report carefully before pressing Y to continue with the uninstall.
A straight apt-get remove leaves behind the configuration files of your program so that if you ever reinstall it, you do not also need to reconfigure it. If you want to remove the configuration files as well as the program files, run this command instead:
matthew@seymour:~$ sudo apt-get remove -purge firefox
That performs a full uninstall.
Finding Software
With so many packages available, it can be hard to find the exact thing you need using command-line APT. The general search tool is called apt-cache and is used like this:
matthew@seymour:~$ apt-cache search kde
Depending on which repositories you have enabled, that tool returns about a thousand packages. Many of those results will not even have KDE in the package name but will be matched because the description contains the word KDE.
You can filter through this information in several ways. First, you can instruct apt-cache to search only in the package names, not in their descriptions. You do this with the –n parameter, like this:
matthew@seymour:~$ apt-cache –n search kde
Now the search has gone down from more than 1,000 packages to a few hundred.
Another way to limit search results is to use some basic regular expressions, such as ^, meaning “start,” and $, meaning “end.” For example, you might want to search for programs that are part of the main KDE suite and not libraries (usually named something like libkde), additional bits (such as xmms-kde), and things that are actually nothing to do with KDE yet still match our search (like tkdesk). Do this by searching for packages that have a name starting with kde , as follows:
matthew@seymour:~$ apt-cache –n search ^kde
Perhaps the easiest way to find packages is to combine apt-cache with grep, to search within search results. For example, if you want to find all games-related packages for KDE, you could run this search:
matthew@seymour:~$ apt-cache search games | grep kde
When you’ve found the package you want to install, run it through apt-get install as usual. If you first want a little more information about that package, you can use apt-cache showpkg, like this:
matthew@seymour:~$ apt-cache showpkg mysql-server-5.0
This shows information on “reverse depends” (which packages require, recommend, or suggest mysql-server-5.0), “dependencies” (which packages are required, recommended, or suggested to install mysql-server-5.0), and “provides” (which functions this package gives you). The “provides” list is quite powerful because it allows different packages to provide a given resource. For example, a MySQL database-based program requires MySQL to be installed, but isn’t fussy whether you install MySQL 4.1 or MySQL 5.5. In this situation, the Debian packages for MySQL 4.1 and MySQL 5.0 both have “mysql-server-4.1” in the provides list, meaning that they offer the functionality provided by MySQL 4.1. Therefore, you can install either version to satisfy the MySQL-based application.