如果你习惯于在 Windows 中获取软件的方法——到每个软件发行商的网站上去下载软件安装包,那么在 Ubuntu 中获取软件的方法将会给你一个愉快的惊喜。
With so much software available for installation, it is no surprise that Debian-based Ubuntu 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:
matthew@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]?
Because this is an upgrade rather than an installation of new software, all those new packages take up only 623KB of additional space. Although we 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 allows 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 would query “mysql-server” against its list of software and find that it matches the mysql-server-5.0 package. It would then find which dependencies it needs that you don’t already have installed and give you a report like this one:
matthew@seymour:~$ sudo apt-get install mysql-server Reading package lists... Done Building dependency tree... Done The following extra packages will be installed: libdbd-mysql-perl libdbi-perl libnet-daemon-perl libplrpc-perl mysql-client-5.0 mysql-server-5.0 Suggested packages: dbishell libcompress-zlib-perl Recommended packages: mailx The following NEW packages will be installed libdbd-mysql-perl libdbi-perl libnet-daemon-perl libplrpc-perl mysql-client-5.0 mysql-server mysql-server-5.0 0 upgraded, 7 newly installed, 0 to remove and 11 not upgraded. Need to get 28.5MB of archives. After unpacking 65.8MB of additional disk space will be used. Do you want to continue [Y/n]?
APT maintains a package cache where it stores DEB files it has downloaded and installed. This usually lives in /var/cache/apt/archives and can sometimes take up many hundreds of megabytes on your computer. You can have APT clean out the package cache by running apt-get clean, which deletes all the cached DEB files. Alternatively, you can run apt-get autoclean, which deletes cached DEB files that are beyond a certain age, thereby keeping newer packages.
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. This is done 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 the configuration files removed as well as the program files, run this command instead:
matthew@seymour:~$ sudo apt-get remove --purge firefox
That performs a full uninstall.
You can see a more extensive list of apt-get parameters by running apt-get without any parameters. The cryptic line at the bottom, “This APT has Super Cow Powers,” is made even more cryptic if you run the command apt-get moo.
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. This is done 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). This can be done 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 per usual. If you first want a little more information about that package, you can use aptcache 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.0. In this situation, the Debian packages for MySQL 4.1 and MySQL 5.0 will 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.