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_name
This 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/ogg123


apt-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 clean
apt-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_name
Will 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-tools

Package: vorbis-tools

Priority: optional

Section: sound

Installed-Size: 580

...


apt-cache search pattern
Will 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 terminal

gmfsk - MFSK, RTTY and other digital mode terminal for HF/amateur radio

gnome-terminal-data - Data files for the GNOME terminal emulator

gnome-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-selections

This command will list all the packages installed on a system using the package manager. Example:

embryo@debian:~$ dpkg --get-selections

acpi-support-base install

acpid install

adduser install

akregator install

...


dpkg -S filename
Will list the packages which install filename. Example:

embryo@debian:~$ dpkg -S oggenc
vorbis-tools: /usr/share/man/man1/oggenc.1.gz
vorbis-tools: /usr/bin/oggenc

embryo@debian:~$


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 dump
This command will list every package in the cache.

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

embryo@debian:~$ apt-cache depends vorbis-tools
vorbis-tools

Depends: libao2

Depends: libc6

Depends: libcurl3-gnutls

...


apt-cache rdepends package_name
Will 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_name
This 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 amarok
apt-cache policy sun-java6-jre


apt-cache showsrc package_name
Will 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:

apt-cdrom -d=/home/USER/mydir

dpkg -c local_file.deb
The -c argument tells dpkg to list the contents of a DEB package. Example:

embryo@debian:~$ dpkg -c google-chrome-stable_current_i386.deb
...

drwxr-xr-x root/root 0 2010-07-30 23:54 ./usr/share/man/man1/

-rwxr-xr-x root/root 3098 2010-07-30 23:54 ./usr/share/man/man1/google-chrome.1

lrwxrwxrwx root/root 0 2010-07-30 23:54 ./usr/bin/google-chrome -> /opt/google/chrome/google-chrome


dpkg -f local_file.deb
This 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.

embryo@debian:~$ dpkg -f google-chrome-stable_current_i386.deb
Package: google-chrome-stable

Version: 5.0.375.126-r53802

...


These are the most popular uses of APT and DPKG. Have some more? Please, share them!

8 comments:

Giorgow said...

Very nice, thank you :-)

Regarding "dpkg -f install", perhaps you meant "apt-get -f install" instead?

Craciun Dan said...

Oh yes, you're right, I modified it accordingly. Thank you!

Debianero Rumbero said...

I'm asking just out of curiosity.

Why do you prefer 'old' apt command instead of 'new' aptitude command?

Craciun Dan said...

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.

Giorgos said...

Apt-get is slightly faster to start up and get going, but aptitude is king when it comes to flexible dependency resolution. It really depends on what you need.

Personally, I use apt-get for day-to-day tasks and aptitude helps me sort out the occasional package version/file conflict mess (I like to try stuff from experimental).

igor said...

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.

However, these things probably should never be done anyway :)

Anonymous said...

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.

LenW said...

I'm a command-line kind of user, but I can't remember the details of all
of these commands. Thanks for helping me augment my alias list.
Here it is:

alias aptsrch='apt-cache search' ## search for pkg or phrase in repos
alias aptshow='apt-cache show' ## details about a pkg in repository
alias aptdep='apt-cache depends' ## list dependencies for pkg
alias aptrdep='apt-cache rdepends' ## list pkgs depending on pkg
alias aptsrcinfo='apt-cache showsrc' ## show info about pkg source
alias aptfiles='apt-file list' ## list files in repository pkgs
alias aptfsrch='apt-file search' ## search for files in repository pkgs
alias aptfileslcl='dpkg -S' ## search for files in installed pkgs
alias aptfilespkg='dpkg -L' ## list files in one installed pkg
alias aptpkgs='dpkg --get-selections' ## list pkgs "selected" (=installed??)
alias aptfilupd='apt-file update' ## update the file-list cache
alias aptupd='apt-get update' ## update the pkg info from repos
alias aptfixdep='apt-get -f install' ## try to fix broken dependencies
alias aptinst='apt-get install' ## install pkg(s)
alias debinfo='dpkg -f' ## show deb control file
alias deblist='dpkg -c' ## list contents of a local deb-file
alias debinst='dpkg -i' ## install pkg from a local deb-file
function aptstat { ## status of installed pkgs (optional display width)
if [[ $1 = +([0-9]) ]] ;then typeset cols=$1 ; shift ;fi
COLUMNS=${cols:-$COLUMNS} dpkg --list "$@" ;}