This is a short tutorial (Primer) on using Apt-get and Dpkg tools to manage software packages in Debian and, Debian-based Linux distributions such as Ubuntu.

What is Package Management ?

A 'Package Management System' or 'Package Manager' is a collection of tools used to automate the process of installing, updating, configuring, and removing of software packages for a computer's operating system in a consistent manner. Different operating systems have their own package managers. For instance, this is a list of package management tools used by different operating systems. [Courtesy: Wikipedia]

Things you should know

Debian uses the Deb package format for packing together files belonging to an application. You can look at it as something like the Setup installer (Eg: Installshield) in Microsoft Windows.

Software meant to be installed in Debian Linux has the file extension '.deb'.

Debian, (and most other Linux distributions), use a publicly accessible place on the Internet - called a repository - to store all their software packages.

The file /etc/apt/sources.list contain a list of web addresses pointing to the Debian repository.

The web address has the following syntax :

deb [web address] [distribution name] [options]

An example of a web address pointing to the Ubuntu repository is shown below.

deb http://in.archive.ubuntu.com/ubuntu breezy main restricted

Managing software in Debian

Debian (and related) Linux (distributions) use two important command line tools to manage deb packages. They are -

Dpkg, and

Advanced Packaging Tools or APT

Dpkg - The low level package management tool

Here are a few ways of using dpkg in Debian Linux.

Install a package

dpkg -i <package-name>

Example : Let's say you want to install Gedit text editor in Debian and you have downloaded its deb package from the Gedit website. The package is named gedit-2.12.1.deb. To install it in Debian, open a terminal and move to the directory containing the deb file and execute the command :

# dpkg -i gedit-2.12.1.deb

You can also use --install instead of -i.

Remove a package

To remove an installed package, the syntax is :

dpkg [-r | -P] <program name>

-r - Will remove the program but leave its configuration files intact. You can also use --remove instead of -r.

-P - Will completely remove the package including the configuration files. You can also use --purge instead of -P.

Continuing with the previous example, to remove Gedit (but not its configuration files) from Debian -

# dpkg -r gedit

To remove Gedit completely -

# dpkg -P gedit

Get more information

To see the contents of a deb package (Eg: gedit-2.12.1.deb), use the -c option.

# dpkg -c gedit-2.12.1.deb

To get more information about a deb package such as - the authors name,the year of compilation, a short description of its use, and so on, use the -I option.

# dpkg -I gedit-2.12.1.deb

To check if a program is installed in Debian, use the -s option.

# dpkg -s gedit

The above command also provides other details about the program such as the package maintainer, dependencies, installed version, a small description of the package and so on.

To know where all the files associated with a program are installed, use the -L option.

# dpkg -L gedit

Advanced Packaging Tool (APT)

APT is the Advanced Package Tool and provides the apt-get program. apt-get provides a simple way to retrieve and install packages from multiple sources using the command line.

Unlike dpkg, apt-get does not understand .deb files, it works with the packages proper name and can only install .deb archives from a source specified in '/etc/apt/sources.list'.

apt-get will call dpkg directly after downloading the .deb archives from the configured sources.

Some common ways to using apt-get are shown below :

To update the list of packages known by your system, do the following :

# apt-get update

You should update your package lists regularly by running the above command.

To upgrade all the installed packages on your system -

# apt-get upgrade

To install the gedit package and all its dependencies -

# apt-get install gedit

To remove the gedit package from your system -

# apt-get remove gedit

To remove gedit and also its configuration files -

# apt-get --purge remove gedit

Upgrading Debian to a new version can be done with the command -

# apt-get dist-upgrade

Saving valuable hard disk space

Each time you install an application using apt-get, the corresponding deb package is downloaded and stored in a location on your hard disk - usually in the directory '/var/cache/apt/archives/'.

The following command will remove all the archives and free up hard disk space -

# apt-get clean

To delete only those packages in the cache which are found useless or partially complete, do -

# apt-get autoclean

Another tool in the APT tool suite is apt-cache.

apt-cache tool can be used to search for and find packages, and it supports use of regular expressions.

To find all packages that contain the word "editor", do the following -

$ apt-cache search editor

To print detailed information about a package, the syntax is -

apt-cache show <package name>

To print the packages that a given package depends on, the syntax is -

apt-cache depends <package name>

To print detailed information of the versions available for a package and the packages that reverse-depends on it -

apt-cache showpkg <package name>

To get statistics of the packages available in the repositories -

$ apt-cache stats

I hope you have enjoyed reading this concise tutorial on apt-get and dpkg which will help you manage the packages in Debian Linux.

43
comments:

OK, this is a dpkg primer for new Debian users, but sooner or later everybody will have to use dpkg --force, especially dpkg --force-overwrite, because dpkg not being able to overwrite a file from one package with another is one of the most common error messages which need manual intervention.

I think aptitude is the recommended option. It has more sophisticated problem-solving options. But personally I find the interface enough to induce psychosis and stick to apt and if I need it synaptic where possible. Out of the box, aptitude also treats recommended packages as dependencies, I think. This means that mixing apt and aptitude together can produce different results and probably isn't a good idea.

There are a couple of other nice things about apt on Debian, like apt listbugs, so you can check first whether you want to install potentially borked packages on Testing or Unstable. All over, it's very thorough.

The other half of the story, I think, is the very good packaging of Debian files that lies behind apt. I also run apt on SuSE but I wouldn't feel so confident about doing a big apt-get upgrade there. On Debian I can feel confident that the whole process will run smooth as butter.

You must mean the poor aptitude (G)UI? But you can use aptitude simply from the commandline too:aptitude updateaptitude installaptitutude upgradeetc.

In problematic situations, like when running Debian Sid, I've found that sometimes apt-get solves some situation better than aptitude, sometimes it is vice versa, so you may try both if in trouble because they work slightly differently from each other.

Good Tutorial, truly helpful. I am using Simply Mepis and apt-get and synaptic are really important. In my experience if you are using Mepis # apt-get dist-upgradeis not very wise to use. It tends to mess with the install. You can do that if your reps all are "stable". Its not a big problem with Mepis though coz if ur install is gone u need like 15min to reinstall Mepis again.

It is always important to have a good understanding of how low level things works (in this case ).Some day, in the future, I wish I would see a dpkg/apt-get related article mentioning other fundamental utilities like dpkg-reconfigure, dpkg-divert, dpkg-statoverride and so on... Maybe that day people won't think "dpkg --force-*"'s mandatory?

Is there a feature that will tell you what you have installed or uninstalled, and when? In other words, a logging feature? If I install something and all of its dependencies, I'd like to be able to go back at some future date and see what I installed at previous times.

Your example for listing all packages starting with "gcc" has a flaw: you need to enclose "gcc*" in quotes to prevent the shell from expanding the wild card. You want dpkg to see the asterisk and do the pattern matching.

I have a quick run-down of the keys used in aptitude's gui at my website, at http://jobezone.wordpress.com/2006/03/17/aptitude-in-30-seconds/. And remember, everything is available from the Top menu, opened with Ctrl+T .

@Dustin You could download the additional packages from the net at another location (cyber cafe??) and burn it into a CD. Then it is a matter of including your CD path in the /etc/apt/sources.list and doing an

$ sudo apt-get update

By the way, if you do not have an internet connection, you can/should remove all the links pointing to the external repositories from the sources.list file.

I know how to download the packages. What I'm looking for is a way to get an updated program list. With a fresh install, I'm stuck with installing only the packages that are available on the install CD. I want the effect of running 'apt-get update' and getting the updated package lists from the repositories, except with me doing the work by hand. Sorry if I worded my previous post confusingly. :)

I think I've found a tool that does exactly what I'm looking for. I'll test it when I get home and see how it works and then I'll post back with my results or other questions. Link at the end of the post.

deborphan returns a list of packages that can be safely purged.apt-get remove --purge `deborphan`is the way to run it.

Also localepurge saves you disk space every upgrade, by deleting man pages for locales other than your preferred location. On a machine that has been running debian since slink this saved me about 3 Gb of disk

Some commands that can help.dpkg -S | --search filename-search-pattern ...Search for a filename from installed packages. All standard shell wildchars can be used in the pattern.

dpkg -p|--print-avail packageDisplay details about package, as found in /var/lib/dpkg/available.

dpkg --update-avail | --merge-avail Packages-fileUpdate dpkg's and dselect's idea of which packages are available. With action --merge-avail, old information is combined with information from Packages-file. With action --update-avail, old information is replaced with the information in the Packages-file. The Packages-file distributed with Debian GNU/Linux is simply named Packages. dpkg keeps its record of available packages in /var/lib/dpkg/available.

dpkg -A | --record-avail package_file ...Update dpkg and dselect's idea of which packages are available with information from the package package_file. If --recursive or -R option is specified, package_file must refer to a directory instead.

dpkg -l | --list package-name-pattern ...List packages matching given pattern. If no package-name-pattern is given, list all packages in /var/lib/dpkg/available. Normal shell wildchars are allowed in package-name-pattern. (You will probably have to quote package-name-pattern to prevent the shell from performing filename expansion. For example, dpkg -l 'libc5*' will list all the package names starting with "libc5".)