Howto APT and dpkg

Write tutorials here
There are more tutorials here http://community.linuxmint.com/tutorial/welcome
Forum rules
Please don't add support questions to tutorials,start your own thread in the appropriate sub-forum instead. Before you post please read this
Post Reply
MagnusB
Level 6
Level 6
Posts: 1252
Joined: Sun Nov 18, 2007 12:39 pm
Location: Norway

Howto APT and dpkg

Post by MagnusB »

OR: Howto fall in love with CLI.

Scope
  • Understand what APT and dpkg is
  • Effectively use APT and dpkg from CLI/Terminal
dpkg
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
Usage:

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.

--unpack
Usage:

Code: Select all

dpkg --unpack /path/to.deb
This only extract the package to your system, but do not configure it. Need root access.

--configure
Usage:

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
Usage:

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
Usage:

Code: Select all

dpkg -p package

Code: Select all

dpkg --print-avail package
Prints details about a package. Does not require root.

-I
Usage:

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
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:

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

-y
Assume yes on yes/no prompts.

--force-yes
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 :)
Aptitude:

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...
Last edited by MagnusB on Thu May 29, 2008 1:32 pm, edited 5 times in total.
Image
You're not drunk if you can lie on the floor without holding on.
--Dean Martin
User avatar
Fred
Level 10
Level 10
Posts: 3337
Joined: Fri Jan 04, 2008 11:59 am
Location: NC USA

Re: Howto APT and dpkg

Post by Fred »

MagnusB,

Very good my friend. Informative and useful.

Fred
Insanity: Doing the same thing over and over and each time expecting a different result.

Democracy is 2 wolves and a lamb voting on the menu. Liberty is an armed lamb protesting the electoral outcome. A Republic negates the need for an armed protest.
MagnusB
Level 6
Level 6
Posts: 1252
Joined: Sun Nov 18, 2007 12:39 pm
Location: Norway

Re: Howto APT and dpkg

Post by MagnusB »

Thanks, Fred, I might add some stuff later on :)
Image
You're not drunk if you can lie on the floor without holding on.
--Dean Martin
Husse
Level 23
Level 23
Posts: 18677
Joined: Sun Feb 11, 2007 7:22 am
Location: Near Borås Sweden

Re: Howto APT and dpkg

Post by Husse »

Very useful - I intend to write something about the dangers of upgrade and in particular dist-upgrade
This is a good reference in that - or would you care to do it MagnusB?
Image
Don't fix it if it ain't broken, don't break it if you can't fix it
MagnusB
Level 6
Level 6
Posts: 1252
Joined: Sun Nov 18, 2007 12:39 pm
Location: Norway

Re: Howto APT and dpkg

Post by MagnusB »

Sure, I can fit in the dangers of upgrade and dist-upgrade somewhere along here :)
EDIT:
Updated, some typos and added the dangers of upgrade and dist-upgrade, please let me know if there is something that should be added under that part :)
Also, if anyone have any additions, let me know :)
Image
You're not drunk if you can lie on the floor without holding on.
--Dean Martin
Husse
Level 23
Level 23
Posts: 18677
Joined: Sun Feb 11, 2007 7:22 am
Location: Near Borås Sweden

Re: Howto APT and dpkg

Post by Husse »

Thanks
I've got a bit here
http://www.linuxmint.com/forum/viewtopi ... =18&t=9146
apart from the thing in General announcements
I added a note on dist-upgrade and corrected a few typos :)
Image
Don't fix it if it ain't broken, don't break it if you can't fix it
MagnusB
Level 6
Level 6
Posts: 1252
Joined: Sun Nov 18, 2007 12:39 pm
Location: Norway

Re: Howto APT and dpkg

Post by MagnusB »

I edited a typo in your note :) Didn't see your edit when I pressed edit, and I am a bit tired and mixed up a few terms and procedures :)
Image
You're not drunk if you can lie on the floor without holding on.
--Dean Martin
MagnusB
Level 6
Level 6
Posts: 1252
Joined: Sun Nov 18, 2007 12:39 pm
Location: Norway

Re: Howto APT and dpkg

Post by MagnusB »

Thanks, ed, should have caught that my self, but it is not only added, it is properly explained as well :) And I will be adding some easter eggs as well, I just need to do some more research on it first...
Image
You're not drunk if you can lie on the floor without holding on.
--Dean Martin
Husse
Level 23
Level 23
Posts: 18677
Joined: Sun Feb 11, 2007 7:22 am
Location: Near Borås Sweden

Re: Howto APT and dpkg

Post by Husse »

Added a little bit about mintUpdate in my note in your original post MagnusB - forgot that when I wrote the original one
Image
Don't fix it if it ain't broken, don't break it if you can't fix it
MagnusB
Level 6
Level 6
Posts: 1252
Joined: Sun Nov 18, 2007 12:39 pm
Location: Norway

Re: Howto APT and dpkg

Post by MagnusB »

No probs Husse, feel free to edit it to safe standards :)
Image
You're not drunk if you can lie on the floor without holding on.
--Dean Martin
User avatar
lagagnon
Level 7
Level 7
Posts: 1879
Joined: Wed Jun 17, 2009 7:38 pm
Location: an island in the Pacific...

Re: Howto APT and dpkg

Post by lagagnon »

MagnusB: an excellent contribution to the Mint Community. Thanks!
ChrisBryan
Level 1
Level 1
Posts: 3
Joined: Thu Aug 11, 2011 12:41 pm

Re: Howto APT and dpkg

Post by ChrisBryan »

Hi Magnus, I have used ubuntu previously but decided to change to Linux mint because of a few unresolved problems, and can only say that I have not regretted it, I must say that this is an excellent article, I have learned a great deal from reading this, more strength to your elbow.
Regards Chris Bryan
xpasaway
Level 1
Level 1
Posts: 2
Joined: Tue Aug 23, 2011 4:48 am

Re: Howto APT and dpkg

Post by xpasaway »

hi.. thanks for the post. i have a request: will you please specify the exact commands in terminal for updating your entire linux mint wherein once you type it, it will automatically download and install all the packages? sorry im newbie and im very confused..

thanks.
Habitual
Level 13
Level 13
Posts: 4863
Joined: Sun Nov 21, 2010 8:31 pm
Location: 0.0.0.0

Re: Howto APT and dpkg

Post by Habitual »

Terminal >

Code: Select all

sudo apt-get update && apt-get -y upgrade
ChickenPie4Tea

Re: Howto APT and dpkg

Post by ChickenPie4Tea »

can you clarify what you meant by
it offers better upgrade options (not recommended in Mint)
so I should not use it in mint or just avoid upgrading?
User avatar
mads
Level 6
Level 6
Posts: 1293
Joined: Wed Nov 03, 2010 3:44 pm
Contact:

Re: Howto APT and dpkg

Post by mads »

ChickenPie4Tea wrote:...so I should not use it in mint or just avoid upgrading?
Just ignore this part about aptitude. The guide is 4 years old. :)
Post Reply

Return to “Tutorials”