Dive under the hood and manage packages directly from the command line.
Ubuntu provides a number of very nice graphical tools for managing software packages from the desktop, but sometimes you just have to get hands-on to really get things done. Servers don’t generally have a graphical desktop environment installed, and if you manage machines remotely through a shell session, you need to know how to use Ubuntu’s command-line package-management tools.
Ubuntu strives for consistency in the process of managing software. All software is packaged using a strictly defined format that contains the program itself plus information about how it needs to be installed, and all packages are stored on your computer in the exact same way. The package-management tools are layered, with each layer building on the levels below. At the lowest level is dpkg, which directly manages packages; mid-level tools like apt sit on dpkg and provide more functionality, such as automatic dependency resolution; and high-level tools like Synaptic and Adept sit on apt and let you graphically browse package lists and do simple point-and-click installation.
dpkg is the basis of the Debian package-management system and allows direct manipulation of packages. If you have a local package on disk called program-1.0-1.deb that you want to install, dpkg is the tool to use. Because it’s such an important part of the package-management stack, it has a whole hack of its own.
While it was not originally intended as a frontend tool, but rather as an intermediate layer between dpkg and end-user tools such as Synaptic and Adept, running apt directly is probably the most common method for managing packages on the command line.
Here are some commands you’ll wonder how you ever lived without:
Retrieve the current list of packages from all servers
If you don’t do this from time to time, your local list of available packages may become out of date. Run this command occasionally before doing a dist-upgrade or searching for a new package. The package lists are large, and doing an update may result in several MB of data being retrieved from the Internet:
$ sudo apt-get update
Do a keyword search
The search command searches through the list of available packages, including package names and descriptions. You can use several keywords; for example, apt-cache search text editor finds a list of text editors:
$ apt-cache search
Get more information
Once you’ve found a package that looks interesting using apt-cache search, you can display more detailed information about it using apt-cache show program. This will tell you things like the size of the package (important if you are installing it via the Internet) and an extended description, as well as what other packages it depends on in order to work and the name of the developer who maintains the package:
$ apt-cache show
Install a package
This will get the latest version of the specified package and install it, along with any other packages that it depends on in order to work. If the requested package is already installed, this will upgrade it to the latest available version:
$ sudo apt-get install
Remove a program
If you’ve previously installed a program and decide you don’t want it anymore, you can remove it using this command. Because some software packages can depend on others, removing one program may break other programs. Running apt-get remove therefore checks first to see if any other software programs need the program to work, and offers to uninstall them as well. This is just one example of the way the Ubuntu package-management tools have been designed to try to keep your computer in a sane state, without broken or half-installed software. It’s certainly possible to break an Ubuntu system, but generally you have to try to do it. The remove command also has a --purge option that removes not just the program itself but also all configuration files associated with it:
$ sudo apt-get remove
$ sudo apt-get remove --purge
Upgrade your system
Over time, most of the software packages on your computer will become out of date, as new versions are released to add features or fix bugs. You could manually do apt-get install foo on each one, but that’s not very convenient, so apt provides a simple way to upgrade your entire system at once. Just type apt-get upgrade to have apt check every single package on your system for a new version and then download and install it. This command will never install new packages; it will only upgrade packages that are already installed:
$ sudo apt-get upgrade
Perform a full upgrade
Sometimes, you’ll have a software package installed, and a new version will come out that has a lot of new features and therefore now depends on some other program to run. For example, you may have a movie player installed that supports a lot of different movie formats. When new formats come out, modules for those formats may be added in separate packages, so the latest version of the movie-player software now depends on a new package that you don’t yet have installed on your system. If you just run apt-get upgrade, you’ll get the latest movie player, but you won’t get all the new movie-format packages. The apt-get dist-upgrade command solves that problem for you: not only does it get the latest version of every package already installed just like apt-get upgrade, it also installs any new packages they need that may not be on your system yet. If you want to keep your system up-to-date with all the latest updates and security patches, running apt-get update; apt-get dist-upgrade from time to time is the best way to do it:
$ sudo apt-get dist-upgrade
When you ask apt to install a software package, it downloads the package and stores it in a cache on your disk before it does the actual installation. If you then remove the package but later change your mind again and reinstall it, apt doesn’t need to fetch it from the Internet again because the package is sitting in the local cache. That’s great for saving bandwidth, but after a while, the cache can use up space on your disk, so it’s a good idea to periodically delete old packages from it. Running apt-get clean will totally flush the package cache, possibly freeing up some precious disk space. This command is quite safe because the worst that can happen is apt may need to download a package again if you remove it and then reinstall it:
$ sudo apt-get clean
autoclean is almost the same as clean, except it’s just a little bit smarter: instead of cleaning out your entire package cache, it deletes only superseded packages. For example, your package cache may contain packages for the last four versions of a text editor that has been upgraded a number of times. Running apt-get autoclean will delete the oldest three versions from the cache, leaving only the latest one. That makes sense because you’re not likely to reinstall anything except the latest version anyway:
$ sudo apt-get autoclean
If you spend a lot of time working on the command line, you can make things easier for yourself by creating shortcuts for the common commands. Add these lines to your ~/.bashrc:
alias agi='sudo apt-get install'
alias agu='sudo apt-get update'
alias ags='apt-cache search'
alias agsh='apt-cache show'
alias agr='sudo apt-get remove'
alias agd='sudo apt-get dist-upgrade'