Package Management (APT)

Package Management (APT)

APT is the abbreviation for Advanced Package Tool.

APT was originally designed as a front-end for dpkg to work with Debian's .deb packages (the first functional version of apt-get was called dpkg-get), and after it was developed with additional features. One major benefit that it considers the packages as a whole, taking into account dependencies as well. APT was introduced in 1998, and the first Debian version that included it was Debian 2.1, released in 1999.

Some history:
(http://www.howtogeek.com/234583/simplify-command-line-package-management-with-apt-instead-of-apt-get/)

APT, the Advanced Package Tool from the Debian project, is for managing packages by using a lot of separate tools. In the past, users needed to know multiple command structures like apt-get, apt-cache, apt-config, and many more to utilize the full feature-set of APT.  For over a decade, practically all tutorials and guides for installing and removing packages on a Debian/Ubuntu based system have been suggesting apt-get to users. In the past, that was the correct suggestion because “apt” as a command hadn’t existed at the time. (Unfortunately a lot of people aren’t aware that now it does exists.)

If you wanted to install a package almost all guides will suggest: sudo apt-get install package. But now instead you can simplify that with: sudo apt install package.

Some may argue that “apt” isn’t much different than “apt-get” and that is true but I think the hyphen in the command is what creates a bottleneck for many users due to its awkwardness. Using apt instead will save time and save the amount of keystrokes required to accomplish the same task.

===========================

apt, apt-get, aptitude and synaptic

APT is a vast project, and the original plans included a graphical interface. It is based on a core library, and  the first front end (command line) tool, which was developed to that core is "apt-get" (before that an early release was called as "dpkg-get"). Later a second command-line front end was developed (which overcomes some design mistakes of apt-get), and it is called "apt". During these days numerous other graphical interfaces appeared as external projects, like "aptitude" and "synaptic" (which includes both a text mode and a graphical interface).

The most recommended interface is apt, however apt-get and aptitude have a very similar command line syntax.

aptitude:
aptitude is an interactive program that can be used in semi-graphical mode on the console. You can browse the list of installed and available packages, look up all the available information,
and select packages to install or remove.

synaptic:
synaptic is a graphical package manager. You can browse through the list of packages, then select the operations to be done on the packages (install, upgrade, remove, purge); these operations are not performed immediately, but put into a task list. A single click on a button then validates the operations, and they are performed in one go.

==========================

Package Management with "apt"

Before trying to install a new software, you need to make sure the apt internal database is up to date.  The command "apt update" will update that database (also known as cache) by automatically connecting to the Raspbian repository (listed in /etc/apt/sources.list file) and downloading the latest package information list. It is recommended that before installing any software, you run this command. After it finished "apt search ..." can be used to search for available packages in apt internal database.

To actually install the software package, you need to run "apt install" followed by the name of the package (like sudo apt install apache2) The apt command will automatically connect to the Internet and downloads this package and all other dependencies that Apache2 needs to operate. After the package is installed and configured, your new software is ready to go!

Meta packages represent a collection of packages. For example, if in a distribution a graphical user interface was not included, you could install this interface in one go by installing a metapackage. These packages behave as normal packages but are actually collections of several different packages behind the scenes.

==========================

/etc/apt/sources.list

APT needs a source file (/etc/apt/sources.list) which contains the different repository (or “source”) servers, that publish Debian packages. APT will then import the list of packages published by each of these sources. This operation is achieved by downloading Packages.xz (gz or bz2)  file (in case of binary packages) and Sources.xz (in case of source packages) and by analyzing their contents. When an old copy of these files is already present, APT can update it by only downloading the differences.

The entries of /etc/apt/sources.list follow this format:
deb http://site.example.com/debian distribution component1 component2 component3


On a Raspbian:
pi@raspberrypi:~ $ cat /etc/apt/sources.list
deb http://mirrordirector.raspbian.org/raspbian/ jessie main contrib non-free rpi


Archive type
The first word on each line is "deb" or "deb-src". It indicates the type of archive. Deb indicates that the archive contains binary packages (deb), that we normally use. Deb-src indicates source packages, which are the original program sources.


Repository URL:
The second field gives the URL of the source (repository) server. It tells from where to download the packages.


Distribution:
The 'distribution' can be either the release code name (like wheezy, jessie, stretch) or the release class (stable, testing, unstable). If you want to track a release class then use the class name, if you want to track a specific Debian release, use the code name.

For example, if you have a system running Debian 8.7, which is called "jessie" and don't want to upgrade to the next realeas (which is called stretch), then use 'jessie' instead of 'stable' for the distribution.

Component:
main: contains all softwares which makes up the Raspbian distribution. If a package is not there, it is not considered to be part of the Raspbian distribution. (all packages here are free)
contrib:  if a package needs an additional package, which is outside of the main distibution, it can be found in this archive (packages in this archive are also free)
non-free: any other software that does not comply with the Debian guideline for free software is included in the non-free archive.

When a package is available on several repositories, the first one listed in the sources.list file will be used. For this reason, non-official sources are usually added at the end of the file.

==========================

apt-cache

The apt-cache command displays the information stored in APT’s internal database. During the "apt update" operation, APT gathers information from different sources listed in the sources.list file, and puts this information into a cache database. (Cache in general IT means, a temporary storage to speed up frequent data access.)

It would be very ineffective to use the network for every search for available packages. That is why APT stores a copy of necessary files in /var/lib/apt/lists/ (which is the cached database) and searches are done within those local files. (Similar to /var/cache/apt/archives/, which contains a cache of already downloaded packages to avoid downloading them again if you need to reinstall them after removal.)

Searching in the cache can be done with the command apt-cache (apt-cache search <keyword>) or with apt command:

apt search <keyword>                     keyword-based package searche
apt show <package>                       display the headers of the package (package description, dependency...)
apt-cache pkgnames                       prints the name of each package APT knows  (lists all the packages which appear in the cache)

==========================

Tracking Automatically Installed Packages

One of the essential functionalities of apt is the tracking of packages installed only through dependencies. These packages are called “automatic”, and often include libraries for instance.
With this information, when packages are removed, the package managers can compute a list of automatic packages that are no longer needed  apt-get autoremove will get rid of those packages.

People might want to know why an automatically installed package is present on the system. To get this information from the command line, you can use aptitude why package (apt and apt-get have no similar feature):

$ aptitude why python-debian
i aptitude Recommends apt-xapian-index
i A apt-xapian-index Depends python-debian (>= 0.1.15)


==========================

/etc/apt/sources.list             lists repositories (internet servers) from where packages can be downloaded
/var/cache/apt/archives/          a copy of each downloaded .deb file is stored here
/var/lib/apt/lists/               contains a list of available packages (which is downloaded from the source repository

apt update                        updates the list of available packages (any work with apt should start with this)

apt install <package>             installs a package (with necessary dependencies)
apt install <package>=version     installs a specific version of the package (possible to install an older version)
apt --reinstall install <package> reinstalls a package even if it is already installed

apt remove <package>              delete a package (and other pkgs. which are dependent on this one, but config files will be kept)
apt purge <package>               complete uninstallation (configuration files are also deleted)

apt-get clean                     entirely empties the directory
apt-get autoclean                 removes packages which cannot be be downloaded (not in the repository anymore) so those are useless
sudo apt-get autoremove           uninstall any redundant packages (after an upgrade redundant packages can be removed)

apt upgrade                       it looks for and upgrades  installed packages which can be upgraded without removing any packages
apt full-upgrade                  performs the function of upgrade but may also remove installed packages
                                  (if that is required in order to resolve a package conflict (major release change))

apt search <keyword>              keyword-based package searche
apt show <package>                display the headers of the package (package description, dependency...)

apt-cache pkgnames                prints the name of each package APT knows  (lists all the packages which appear in the cache)

==========================

Installing and Reinstalling a package

"apt install" will install new packages, but if a package is already installed, apt tries to do an upgrade on that package. If the latest version is already installed apt will tell you it cannot upgrade the software and exit.

After a file has been deleted, it can happen that an installed application does not work correctly. The easiest way to solve this would be to reinstall the affected package, however the packaging system finds that it is already installed and politely refuses to reinstall it. To avoid this, use the --reinstall option of the apt command.

apt --reinstall install <package>        reinstalls a package even if it is already installed

==========================

Cleaning up package copies (/var/cache/apt/archives/)

APT keeps a copy of each downloaded .deb file in the directory /var/cache/apt/archives/.  In case of frequent updates, this directory can quickly take a lot of disk space, with several versions of each package. apt-get clean and apt-get autoclean can free up space from this directory. (apt does not support these commands, only apt-get)

apt-get clean           entirely empties the directory
apt-get autoclean       removes those packages which can no longer be downloaded (not in the repository sources anymore)

==========================

Upgrading a distribution

Debian (and Raspbian) has the ability to upgrade an installed system from one stable release to the next. For example upgrading a Wheezy system to Jessie. This is a major operation on
a system and a full back up is recommended before doing anything.

1. change the /etc/apt/sources.list file: Tell APT to get its packages from Jessie instead of Wheezy. If the file only contains references to "Stable" rather than explicit codenames, the change isn’t even required, since Stable always refers to the latest released version of Debian.

2. apt update: It will  refresh the list of available packages

3. apt upgrade: First do a minimal upgrade (ease the job of the package management tool, and ensure we have the latest versions of installed packages)

4. apt full-upgrade: It will do the actual full upgrade

==========================

Installing software from source

All software programs are built from different files. These files are called source code and are written in programming languages (like C or C++). If a software is not available in the Raspbian repositories it is possible to compile and install it from the source code. The build-essential package contains the most common tools to compile an application. (One disadvantage of installing software from source is that updates will not be automatically installed for this software.)

sudo apt-get install build-essential  <---install software tools needed to compile C and C++ applications
                                      (including the make application and the gcc and g++ compilers)
download source code                  <--like with wget, receive a file like:  httpd-2.4.10.tar.bz2.
tar xvjf httpd-2.4.10.tar.bz2         <--extract the tar.bz2 archive
./configure                           <--it is a script that generates the MakeFile file, used by "make"
                                      (it will also tell if there are any dependencies for this software)
make                                  <--compiles the software from the source code
make install                          <--install the compiled software

===============================

Just use this fancy one-liner to keep your Pi up-to-date:
apt-get update && apt-get dist-upgrade && apt-get clean

If you are worried about not-needed stuff add this:
apt-get autoremove --purge && apt-get remove `deborphan` --purge

No comments:

Post a Comment