Friday, August 22, 2008

Howto APT and dpkg

OR: Howto fall in love with CLI.

  • Understand what APT and dpkg is
  • Effectively use APT and dpkg from CLI/Terminal

dpkg is a tool for installing, removing and building Debian packages. Shortly told, it handles *.deb packages.

Using dpkg
Knowing some basic dpkg commands can make your life allot easier. For one you can retrieve a ton of information of installed packages, or installing new packages, and even build your own deb packages.

dpkg options
-i | --install
Code: Select all
dpkg -i /path/to.deb

Code: Select all
dpkg --install /path/to.deb

Kind of self explaining really, it installs a .deb package to your system. It requires root access.

Code: Select all
dpkg --unpack /path/to.deb

This only extract the package to your system, but do not configure it. Need root access.

Code: Select all
dpkg --configure package

Code: Select all
dpkg --configure -a

Code: Select all
dpkg --configure --pending

Configures selected package(s). -a | --pending only configure unpacked packages. Needs root access.

-r | --remove | -P | --purge
Code: Select all
dpkg -r /path/to.deb

Code: Select all
dpkg --remove /path/to.deb

Code: Select all
dpkg -P /path/to.deb

Code: Select all
dpkg --purge /path/to.deb

Removes selected packages. -r or --remove only removes installed files, which can be useful if you plan to reinstall the package later on. -P or --purge also remove configuration files. Requires root access.

-p | --print-avail
Code: Select all
dpkg -p package

Code: Select all
dpkg --print-avail package

Prints details about a package. Does not require root.

Code: Select all
dpkg -I /path/to.deb

Shows detail about a package, including dependencies. Can be useful for checking what you need for installation of a package, or checking the dependencies of a already installed package. Remember, your local repository is located at /var/apt/archives .

There are more options available, but these are the basic options. You could use Google if you want more.

Combining APT and dpkg to "backup"
Not really a back up, but more exporting the list of installed packages to a file, then import it again. You could also use APTOnCD for this, but if you got to configure a ton of machines with different hardware setup, this can be just as useful (I.e. open the file and remove packages you don't need after a stock install).
Code: Select all
sudo dpkg --get-selections > mypackages.txt
sudo dpkg --set-selections < mypackages.txt
sudo apt-get dselect-upgrade

Be careful with this, it can be extremely useful, but can potentially break a system. For instance, generating the list in Daryna and importing in Elyssa is not recommended unless you know what you are doing. Also note that this lists ALL installed packages, not just the ones you have installed after a fresh install, it could be a good idea to go through the list and remove some of the packages.

APT is a combination of wget and dpkg, simply put. It keeps a list of available packages in the repositories and can be used to download and install those packages. It can also be used to remove these packages.

aptitude vs. apt-get
I prefer aptitude over apt-get any day. It handle dependencies better (especially noticeable when removing packages), it offers better upgrade options (not recommended in Mint), and is not divided like apt-get it (more on that later). My recommendation is to get used to aptitude from the start, and it is what I will focus on in this howto. Aptitude can also run as a "Synaptic in terminal". If you want to look at it, I recommend starting it by using:
Code: Select all
sudo aptitude -u

This updates the list of packages before starting aptitude.

Updating list of packages
Since the repositories are constantly being updated, new packages added or packages being updated etc, so it could be a good idea to update the local list of available packages. Both aptitude and apt-get handles updates:
Code: Select all
sudo aptitude update
sudo apt-get update

Searching for packages
Using the CLI for searching is faster than loading synaptic and gives the same results, hence, in my opinion, better.
Code: Select all
aptitude search string

Code: Select all
apt-cache search string

Just replace string with the search string of your choice. It could also be a good idea to do a update (aptitude update) before searching. Here you see that aptitude handles searches, while apt-get does not.
And aptitude offers additional information about the package, like whether it is installed or not, with the first (few) characters on left of the package name. The first character(s):
i: Installed
p: No trace found on the system
c: Packages has been deleted, but the configuration files exists.
v: Virtual package.
The second character(s) are the action which is to be taken with the package:
i: Going to be installed
d: Going to be deleted
p: Going to be deleted, including configuration files.
If the third character is A it means the package was automatically installed.
Here is an example of a search result (taken from the aptitude man pages):
i apt - Advanced front-end for dpkg
pi apt-build - frontend to apt to build, optimize and in
cp apt-file - APT package searching utility -- command-
ihA raptor-utils - Raptor RDF Parser utilities

Here you see apt is installed, apt-build does not have any files on the system, but is going to be installed, apt-file only has the configuration files on the system, but those are to be removed, and raptor-utils is installed, no changes to it, and was automatically installed.

Installing packages
So after updating the list, and you know what to install after the search it is just using:
Code: Select all
sudo aptitude install package
sudo apt-get install package

You need the correct name for the package to install, for instance just "deluge" won't work, you have to type in "deluge-torrent". You could also use some options here:

Download only, you just download the package to a local repository, you do not install it.

Assume yes on yes/no prompts.

Not recommended to use, this forces yes on even warning messages. By using this you might end up doing something harmful to your system.

Using options:
Code: Select all
sudo aptitude -option install package
sudo apt-get -option install package

Removing packages
So you got tired or don't need a package any more and are ready to remove it. This is pretty straightforward:
Code: Select all
sudo aptitude remove package
sudo apt-get remove package

This is where aptitude shines. Where apt-get only removes the packages you specify, while aptitude also removes the dependencies of that package.

Fixing broken dependencies
From time to time, and for some reason, you can get packages with broken dependencies. These can be a pain, as APT usually refuse to install or remove any packages before this is fixed. Luckily this can be done automagically:
Code: Select all
sudo aptitude -f install
sudo apt-get -f install

This is a pretty aggressive command, use with care and do not combine with -y or --force-yes!

Updating your system
It is not recommended using APT to keep Mint up to date, you should use mintUpdate for this. But I will still include a few words of this here, as there are a few ways of doing this. Again, aptitude is the preferred way of upgrading:
Code: Select all
sudo aptitude safe-upgrade

safe-upgrade prevents you from ending up in dependency hell. It does not upgrade/install packages that have unmet dependency, I.e. a newer version than available, and keeps everything neat and clean. But as I mentioned above, upgrade is not recommended, so if you do not know what you are doing, don't do it!

Why you should not update Mint using CLI
Mint is based on Ubuntu, and use Ubuntu repositories, but that does not mean Mint IS Ubuntu, Mint (Main) forked from Edgy (or 6.10), and thus has it's differences from the current stable Ubuntu. This also means that not all updates from Ubuntu is safe/stable on Mint. To prevent the installation of these updates, the Mint development team has built mintUpdate, that should be your primary update tool. Not only does this filter out known "ugly updates", it also filters out potential "ugly updates", like kernel updates. One of the reasons for excluding kernel updates is to provide a consistent desktop for the user, without having to recompile drivers after a update, but if you are a security freak, feel free to include all updates, but do not come back and complain because X fails to start or similar (but we will of course help you set things straight again, if you ask politely :)).
And UNDER NO circumstances should you EVER do a dist-upgrade *, unless there is an official announcement that explicitly states that this is possible and even then you should read carefully and follow the directions given. After almost every Ubuntu release there are some who run a dist-upgrade on their system and end up with a mix between Mint and Ubuntu, and often a buggy mix. The only solution for this is a FRESH INSTALL!
*Note - if you have the correct sources.list for your version of Mint you can do a dist-upgrade. But the minute you add repositories from the next (or previous) generation you're done (like adding Hardy repos for Daryna). But the dist-upgrade option is the only way to get new applications added even in mintUpdate. But remember to have the right sources.list
It is (always) safe (and recommended) to use the dist-upgrade option in mintUpdate. Nothing is installed until you click apply. This note added by husse
If you do not feel confident with apt and Synaptic (or package management in general) you should stay away from upgrading your system via CLI.

Cleaning out the local repository
APT keeps a local repository of all packages you install. This can be very useful, especially if you want to reconfigure a packages or re install it. But this could take up allot of space if you install a few packages, so a clean out once in a while could be useful.
Now, there are two ways of doing this:
Code: Select all
sudo aptitude autoclean

This only removes downloaded packages which are no longer available, i.e. an old version, from the local repository.
Code: Select all
sudo aptitude clean

This removes ALL downloaded packages from the local repository. I prefer autoclean, that way I get to keep a .deb of all my installed applications, but if you are short on space, you could just run clean.

Cleaning out dependencies
If you haven't followed my recommendations and used aptitude to remove packages, some of your installed packages might not be needed any longer. Fear not, the can easily be removed:
Code: Select all
sudo apt-get autoremove

Easter eggs
No proper software is complete without any easter eggs, and APT has some. First apt-get:
Code: Select all
apt-get --help

Look at the bottom of the printout, it states that "This APT has Super Cow Powers". And it does indeed:
Code: Select all
apt-get moo

Try it for your self :)
Code: Select all
aptitude --help

At the bottom it states "This aptitude does not have Super Cow Powers." Well, try:
Code: Select all
aptitude moo
aptitude -v moo
aptitude -vv moo
aptitude -vvv moo
aptitude -vvvv moo
aptitude -vvvvv moo
aptitude -vvvvvv moo

This is a small introduction to APT and dpkg, there are a ton of other possibilities, but knowing these here, you will effectively maintain your installed packages using the CLI only and never feel the need for loading Synaptic or similar GUIs...

No comments: