Ubuntu Package Management from Command line using apt (Advanced Packaging Tool)
Sponsored Link
The Advanced Packaging Tool, which has since been ported by Conectiva for use with rpm and has been adopted by some other distributions.
The apt-get command is a powerful command-line tool used to work with Ubuntu's Advanced Packaging Tool (APT) performing such functions as installation of new software packages, upgrade of existing software packages, updating of the package list index, and even upgrading the entire Ubuntu system.
APT uses a file that lists the ‘sources' from which packages can be obtained. This file is /etc/apt/sources.list.
The entries in this file follow this format
deb http://host/ubuntu distribution section1 section2 section3
deb-src http://host/ubuntu distribution section1 section2 section3
The first word on each line, deb or deb-src, indicates the type of archive: whether it contains binary packages (deb), that is, the pre-compiled packages that we normally use, or source packages (deb-src),
Now we will see available commands for apt-get
Update the list of available packages
The packaging system uses a private database to keep track of which packages are installed, which are not installed and which are available for installation. The apt-get program uses this database to find out how to install packages requested by the user and to find out which additional packages are needed in order for a selected package to work properly.
sudo apt-get update
The following options to apt-get may be useful
-h This help text.
-d Download only -- do NOT install or unpack archives
-f Attempt to continue if the integrity check fails
-s No-act. Perform ordering simulation
-y Assume Yes to all queries and do not prompt
-u Show a list of upgraded packages as well
Install a Package Using apt-get
sudo apt-get install packagename
Example
sudo apt-get install apache2
Reinstall a Package Using apt-get
If you somehow damage an installed package, or simply want the files of a package to be reinstalled with the newest version that is available, you can use the --
reinstall option
sudo apt-get
--
reinstall install packagename
Example
sudo apt-get
--
reinstall install apache2
Remove a Package Using apt-get
sudo apt-get remove packagename
Example
sudo apt-get remove apache2
Running apt-get as above will cause the packages to be removed but their configuration files, if any, will remain intact on the system. For a complete removal of the package
sudo apt-get
--
purge remove packagename
Example
sudo apt-get
--
purge remove apache2
Just as in the case of the install method, you can use a symbol with remove to invert the meaning for a particular package. In the case of removing, if you add a ‘+' right after the package name, the package will be installed instead of being removed.
sudo apt-get
--
purge remove packagename+
Example
sudo apt-get
--
purge remove apache2+
Upgrade Packages
You can use this command to upgrade packages within the same distribution, as well as to upgrade to a new distribution
sudo apt-get -u upgrade
Upgrade to a new release
sudo apt-get -u dist-upgrade
If you want to upgrade specific package use the following command
sudo apt-get -u install packagename
Example
sudo apt-get -u install apache2
Remove unused package files
When you install a package APT retrieves the needed files from the hosts listed in /etc/apt/sources.list, stores them in a local repository (/var/cache/apt/archives/), and then proceeds with installation.
In time the local repository can grow and occupy a lot of disk space. Fortunately, APT provides tools for managing its local repository.
apt-get clean removes everything except lock files from /var/cache/apt/archives/ and /var/cache/apt/archives/partial/. Thus, if you need to reinstall a package APT should retrieve it again.
sudo apt-get clean
apt-get autoclean removes only package files that can no longer be downloaded.
sudo apt-get autoclean
Search for specific packge
If you want to search for specific packge use apt-cache. This program is used by the APT system for maintaining its database.
Search specific package
sudo apt-cache search packagename
Example
sudo apt-cache search atari
If you want to get more information about a specific package use the following commands
sudo apt-cache show packagename
Example
sudo apt-cache show nagios3
If you want to find out what packages it depends on specific package
sudo apt-cache depends packagename
Example
sudo apt-cache depends nagios3
How to discover to which package a file belongs
If you want to install a package, and you can't find out what it is called by searching with apt-cache, but know the filename of the program itself, or some other filename that belongs to the package, then you can use apt-file to find the package name. This is done like this:
sudo apt-file search filename
You can also list the contents of a package, by running
sudo apt-file list packagename
apt-file keeps a database of which files all packages contain, just like auto-apt does and it needs to be up-to-date. This is done by running:
sudo apt-file update
Downloading source packages
To download a source package, you would use the following command:
sudo apt-get source packagename
This will download three files: a .orig.tar.gz, a .dsc and a .diff.gz. In the case of packages made specifically for ubuntu, the last of these is not downloaded and the first usually won't have "orig" in the name.
The .dsc file is used by dpkg-source for unpacking the source package into the directory packagename-version. Within each downloaded source package there is a debian/ directory that contains the files needed for creating the .deb package.
To auto-build the package when it's been downloaded, just add -b to the command line
sudo apt-get -b source packagename
Packages needed for compiling a source package
Normally, specific headers and shared libraries need to be present in order for a source package to be compiled. All source packages have a field in their control files called ‘Build-Depends:' that indicates which additional packages are needed for the package to be built from source. APT has a simple way of downloading these packages.
sudo apt-get build-dep package
Example
sudo apt-get build-dep gmc
Very good article.
One thing I wanted to point out was that you do not need to use ‘sudo’ for apt-cache
$ apt-cache show build-essential
will work as a normal user as well
Use aptitude if you have it available.
It should be noted that apt-file is not in the default install and it has to be installed prior to usage.
Hm, as far as I remember ‘apt-get dist-upgrade’ and not ‘apt-get upgrade’ is the recommended way to upgrade packages. The reason is that a simple ‘apt-get upgrade’ will not update a package if it depends on packages not already installed on the system. ‘apt-get dist-upgrade’ will update the package and retrieve and install the needed dependencies.
instead of “apt-get –purge remove” you can simply use “apt-get purge”.
I cant believe you left out the most important
and by far the most useful command
sudo apt-get moo
What the heck does apt-get moo do? I looked at the man page but I still don’t get it and there’s no way I’m gonna just punch that in without a clear idea of the result.
How to add some more apt-get sources ?? nvidia ??
Nice one. Just one thing, as Hmpf said apt-get dist-upgrade is higher level of upgrade, not release upgrade. Upgrade to new release e.g. in Ubuntu systems is done via do-release-upgrade.