Monday, 20 September 2010

Collection of 18 Popular APT & DPKG Tips for Debian and Ubuntu

This is a guide containing the most popular and useful ways of using the APT and DPKG commands, and it applies to both Ubuntu and Debian (and their derivatives). I mentioned where super user privileges are required, the ones without a mention can be executed as normal user. If you're using Ubuntu, precede a command with sudo in order to gain root privileges (and enter your user password); on Debian, type su, enter the root password, and then type in the commands as shown below.

dpkg -L package_nameThis command will show what files package_name installed on your computer. Notice that this works only for already installed packages. Example:

embryo@debian:~$ dpkg -L vorbis-tools/./usr/usr/bin/usr/bin/ogg123apt-get build-dep package_name(needs root privileges)This is a very useful command, which will install all the development libraries needed by an application. So if you need to compile a newer version of a program than the one included in the repositories, use this command to fetch the dependencies. Notice that the libraries installed will be for the current version coming with the repositories, so you may need to install additional libraries if needed. However, this usually works for most of the applications. Example:apt-get build-dep wesnoth

This will fetch all the development packages needed for Wesnoth. Keep in mind that your sources repositories must be enabled (they are enabled by default in Ubuntu).

apt-get clean (and apt-get autoclean)(needs root privileges)This command will remove the archives installed on your system. Whenever you install a package, it is downloaded and the DEB package is kept inside the /var/cache/apt/archives/ directory. This command removes those packages, freeing up space.

apt-get cleanapt-get autoclean

Regarding apt-get autoclean: this command will remove only the archives which are no longer available in the repositories, and will keep the ones which are currently available. Helpful if you want to get rid of old archives which have been updated, but still want to keep the upgraded ones just in case. To see how much space the archive occupy, use du -h (the -h argument stands for human readable, so it will show sizes in human readable format):du -h /var/cache/apt/archives/

apt-cache show package_nameWill show information about any package in the repository. You only have to type in the name of it, not the entire name and version. Example:embryo@debian:~$ apt-cache show vorbis-toolsPackage: vorbis-toolsPriority: optionalSection: soundInstalled-Size: 580...

apt-cache search patternWill search and list all the packages which contain pattern in their name or in their description. You can pipe this with grep for more relevant results:embryo@debian:~$ apt-cache search gnome terminal|grep terminalgmfsk - MFSK, RTTY and other digital mode terminal for HF/amateur radiognome-terminal-data - Data files for the GNOME terminal emulatorgnome-terminal - The GNOME 2 terminal emulator application

apt-get update && apt-get upgrade(needs root privileges)This is a one-liner to update your packages list and upgrade your system. Since it requires root privileges, if your distribution is Ubuntu use sudo apt-get update && sudo apt-get upgrade. Notice that the second command (after the double ampersand) is executed only if the first one was successful, so if fetching any of the lists fails, the second command will not be executed. If you're running a testing or beta release, use instead the following:sudo apt-get update && sudo apt-get dist-upgrade

This will fetch updates even if they depend on newer packages.dpkg --get-selectionsThis command will list all the packages installed on a system using the package manager. Example:embryo@debian:~$ dpkg --get-selectionsacpi-support-base installacpid installadduser installakregator install...

apt-get autoremove(needs root privileges)This command will remove unnecessary packages (e.g. orphaned packages), which are no longer needed by any of the packages installed in the system. This happens often after you uninstall a certain application, and its dependencies still remain installed on the system. Use this command to get rid of them.

apt-cache dumpThis command will list every package in the cache.

apt-cache depends package_nameA very useful command which lists every dependency a package has, including recommended packages.

apt-cache rdepends package_nameWill show dependencies in reversed order, that is, it will list all the packages which depend on the specified package.

apt-cache rdepends mplayer

apt-cache policy package_nameThis will list the repository in which a package is located. In Ubuntu, there are four such repositories: main, restricted, universe and multiverse. Debian has main, contrib and non-free. There is a certain policy for each package, and depending on that, the package is included in one of the specified repositories.

apt-cache policy amarokapt-cache policy sun-java6-jre

apt-cache showsrc package_nameWill show information about a package source.

apt-get -f install(needs root privileges)Will try to fix broken dependencies. This usually comes in handy if you try to manually install a DEB package without installing first its dependencies. Run apt-get -f install to fix the missing dependencies and try to install the package again.

apt-cdrom add(needs root privileges)Will add a CD ROM to your sources, so you can install packages from it. This command is mostly useful if you have some ISO images, which you don't want to burn to a CD. In order to scan the ISO images, use:

apt-cdrom -d=/cdrom add

This will mount the ISO image inside the /cdrom directory, and then add its contents to the sources.list. Notice that the /cdrom directory has to be empty. You can use any empty directory for this:

dpkg -f local_file.debThis shows the control file for a binary package. A control file is a file which provides information to the package manager about dependencies, maintainer, homepage, description, and more.

As far as I know 'aptitude' is the recommended method now, but I'm still used to the old apt-get. It's probably because that was the way I've learned it. Nevertheless, I think most of these apply to aptitude too, since it borrows the same arguments from apt-get.

I mostly use aptitude for it's better dependency handling. But sometimes for an unusual situation you might exploit the subtle differences in a way that apt-get and aptitude work. For example, I used it when Migrating from Ubuntu to Debian" or downgrading.

As far as I can tell...aptitude is better if you want something installed right now and don't mind if it breaks somethings in the progress.apt-get is better if you want to keep your system working nicely together, and have easier installs latter.Basically use apt-get as far as you can, and if that doesn't work try aptitude.