Working with apt-get, apt-cache and aptitude


Updated: July 15, 2009

If you're using a Debian-based Linux distribution, you must have come across APT. APT stands for Advanced Packaging Tool and it's probably the fastest, simplest, most robust software management utility for Linux. APT is so popular that it has been ported to Solaris and Mac and even some rather RPM-istic distributions like SUSE Enterprise use it.

Teaser

If you've never heard of APT, you've come to the right place, for today we're going to talk about three children of APT, apt-cache, apt-get and aptitude. We will learn what each one is good for, how to use them and why you should use them.

Follow me.

Why APT?

APT has proven its worth over the years. It is fast, responsive and stable, but most importantly, it is reliable. Should you lose Internet connection while updating your system or break the current operating, APT will gracefully pick up where you left the next time. Quite a few other management tools tend to remain in a broken state when the update process gets interrupted.

APT will manage the software dependencies in a smart way, both when installing and removing software. This includes maintaining a higher level revision control and topological sorting, in order to make sure your system will remain in a consistent state after numerous updates and uninstallations.

APT has many other smart features, all of which make it a very useful candidate for software management. It is not without a reason among the most popular choices in many Linux distributions.

You are welcome to read the long and geeky official howto, which documents the configurations, package management and other features of APT.

Why command line?

Truth to be told, you don't absolutely have to use the command line to manage your software installations. But there is a number of reasons that I think are worth considering:

Command line is fast

Working on the command line is usually much faster than running a GUI. This allows you to save time.

Working on the command line improves your understanding of the system

One of the biggest pitfalls of visual administrative work is that the beautiful menus hide away from the user the important details of how things work. On one hand, this is a good thing, as less knowledgeable users are spared the gory, dangerous technical stuff. On the other hand, the dependency on visual clues reduces the control the user has over the machine and effectively prevent him from learning the system internals.

The best way about system management is a balance between the two approaches. Users should be proficient in the both the graphical and command-line approaches, but work with any which method they like.

Less chance of an error

Typing in the terminal forces you to think through the next command you are about to execute. It is more difficult to type down a wrong command than select an option in one of the menus of a management GUI. Think about it: How many times have you misplaced a file working in a file manager, dragging and dropping files with the mouse? How many times have you accidentally "dropped" a file in the wrong directory when copying files on the command line?

This short list is by no means complete or some sort of a holy creed you should follow. It is a recommendation, which I think can help you work more safely, smartly and faster.

Now, let's see how we can use APT to manage our software.

apt-get

This one is probably the most common package handling utility you will use to maintain your software.

apt-get install

This command, when run against a package name, will install the desired package and all the required dependencies. You do not need to know the exact version of the package; you just need to know its alias. APT will automatically search for the latest version available in the software repositories.

apt-get install

apt-get remove

This is the exact opposite of apt-get install:

apt-get remove

apt-get remove will uninstall software, but it may leave behind unused dependencies. Furthermore, the package installer (.deb) files will remain in the local cache. To completely remove the files, you can use the --purge flag. The best analogy is deleting files to Trash and permanently deleting them.

apt-get remove --purge package-name

apt-get autoremove

This command will not only remove the specific package, but also all its dependencies. This is truly the full uninstall option, unlike remove, which will merely remove the main program. For example, if you installed a KDE program, like Amarok or Kile, on a Gnome desktop, dozens of KDE libraries will be installed. Now, if you remove only these programs, the extra KDE libraries will remain on the system, taking unnecessary space. autoremove allows you to get rid of them, too.

For Windows users, this is a sort of a registry cleaning, where shared DLLs and other files no longer in use are removed from the system, except the process is done on the package level rather than inside a pseudo dimension like registry.

If you do not write down any specific package, all orphaned packages will be removed.

apt-get autoremove

apt-get clean & apt-get autoclean

After several days, weeks or months of use, your system cache will contain lots of leftovers, including files that no longer exist in repositories and software you no longer need. Running the two command listed above allows you to trim down the crud and keep the cache small and tidy.

apt-get autoclean

apt-get update

This is one of the commands you should run first before trying anything else. It will synchronize the package index files with the software sources, making sure you have the most up to date lists. Only after doing this should you attempt to install programs.

apt-get update

apt-get dist-upgrade

This is one of the more useful commands. In one go, you can update your entire system. Every single package will be upgraded to the newest, latest version as reported by the software repositories.

Other useful commands

apt-get has many other modes. For instance, running apt-get with the -s (or --simulate) flag specified will perform a dry run of the installation without actually doing it, allowing you to see what is going to happen before committing the changes for real. On the other hand, the -f (--fix-broken) flag is useful for trying to correct a system with broken dependencies.

Many other flags exist. I warmly recommend you read the manual page if you have the time and nerve.

aptitude

You may also have heard people recommend aptitude instead of apt-get. This is a bit of a legacy from olden days when aptitude was more ... apt at handling packages than apt-get, usually with better results when it came to solving dependencies during installation and removal.

Today, any benefit that existed in aptitude has been equaled by improvements in apt-get, so the choice of the two is a matter of preference. aptitude takes options and flags very similar to apt-get, so using the two is practically identical on all levels.

aptitude install

apt-cache

apt-cache is a utility you probably did not hear about. It is an important part of the APT system, allowing you to perform a variety of operations on the package cache, without changing the state of the system.

apt-cache should be used before installations, especially if you're not sure what you want to download, what dependencies need to be satisfied, the exact package version, the purpose of the package, etc.

apt-cache search

This function allows you to look for packages, plain and simple:

apt-cache search

apt-cache show

This command, alongside showpkg, will tell you all you need to know about the specific package, including the priority, maintainer, version, dependencies and suggested packages, the hashes, and even a small description of what the package does. This is identical to performing a search via one of the graphical tools like Synaptic (see below).

apt-cache show

apt-cache stats

apt-cache stats will give you a short summary on your cache:

apt-cache stats

apt-cache has other useful options and flags. Please refer to the manual page for more details.

Configurations

Important settings about how APT behaves can be set in the configuration file. The file sits under /etc/apt/apt.conf and requires root privileges to edit.

Some of the things you may want to add to the configuration file include details like authentication of proxy network settings.

For instance, to enable installation of packages that cannot be authenticated:

APT::Get::AllowUnauthenticated 1;

Or to enable APT to work via proxy, for example:

Acquire::http::proxy "http://someproxy.com:8080/";

As you can see, APT uses a configuration syntax similar to cfengine, a powerful revision control and software management program.

Other APT tools

apt-get and apt-cache are only two of many useful facets of APT. Other utilities exist, which you should also consider for your work:

apt-file

apt-file allows you to search among packages and dependencies to determine where each file/package belongs. This is very useful for non-standard installations.

auto-apt

auto-apt allows you to automatically retrieve missing packages when compiling programs. If you encounter an error during a compilation of a program, the process will usually fail with an error. Using auto-apt allows you to install missing packages on the fly, without breaking the compilation process.

On a side note, the best analogy I have is the equivalent feature in the MiKTeX package manager for LaTeX, which will installing missing packages when needed during the compilation of documents.

Just run auto-apt run against the files you are compiling and avoid the numerous errors of satisfying missing packages and dependencies.

Graphical front ends

APT is a command-line tool. As such, it intimidates many users, despite its obvious simplicity. Nevertheless, several frontends have been created, to make software management easier and more pleasant for normal people.

Here's a briefest overview of some of the graphical package management utilities that you may be using, which call upon the powers of APT beneath the hood:

Synaptic

Quite a few distributions use Synaptic as their package manager of choice, Ubuntu, Linux Mint, PCLinuxOS, and Knoppix being some of the examples. Synaptic is fast and responsive and very easy to use.

Synaptic example 1

Synaptic example 3

Adept

Adept is another popular choice. For instance, the KDE version of Ubuntu, known as Kubuntu, uses Adept.

Adept

gdebi

gdebi is another useful candidate. It has both the command line and a graphical interface. In works against individual files. It is useful for local installs and solving of dependencies.

gdebi

Conclusion

The frightening, alien words apt, apt-get, apt-cache, and others are no longer strange concepts. They have become useful, powerful tools that we will use to manage our software in a fast, elegant and safe manner.

The use of command-line software management will surely help you improve your mastery of the Linux system administration and possibly goad you to expand your skills into new areas that were previously uncharted territory, mainly because of the unjustified fear of the terminal.

Working with APT is fun. It is a very convenient and fast way of managing software installations and distribution upgrades, cleaning the system, and searching for programs. The need to think through your actions and type them down makes it for a safe tool, as well. Lastly, the time spent on the command line will make you more comfortable working in Linux, helping you achieve new skills and new layers of knowledge.

Hopefully, this tutorial has helped you realize all this. Now, you can begin your own journey of pleasure and efficiency.

Cheers.

RSS Feed icon

del.icio.us del.icio.us stumbleupon stumble digg digg reddit reddit slashdot slashdot



Advertise!

Would you like to advertise your product/site on Dedoimedo?

Read more

Donate to Dedoimedo!

Do you want to
help me take early retirement? How about donating
some dinero to
Dedoimedo?

Read more

Donate