Package management in Kali Linux and other Debian-based distributions (searching, installing and removing programs, troubleshooting)

Linux's distribution maintainers provide package repositories from which programs can be installed on a distribution using a package manager. Such repositories (their composition, update frequency, pre-configuration of packages) are the main differences between distributions from each other (and not wallpaper or graphical desktop environment, which can always be changed).

This centralized approach allows you not to waste time on updating individual programs (as, for example, you need to do in Windows, where you yourself need to track new versions and update them).

Knowing how to manage packages in Linux is necessary both for installing and updating programs and solving other problems, such as missing files, required to compile a program that you want to install from source. This article covers both the basics and provides more advanced tips (for example, how to find the package containing the file required for compilation).

This article focuses on the apt package manager and the Kali Linux distribution, but it also applies to other Debian-based distributions (Linux Mint, Ubuntu).

How Linux application repositories work

Linux's distribution maintainers select software for placement in the repository. These programs are stored in packages, for Debian and derivatives they are files with the .deb extension. These packages contain compiled program files, configuration files and all other files necessary for the program to work, as well as meta information that indicates what other packages need to be installed to resolve dependencies, as well as what actions need to be performed before and after installing the package, and also before and after removing it.

To install a package from the repository, just run the apt install command specifying the package name. This command will itself find the URL address of the .deb file, download it, determine and download the necessary dependencies and then install it all.

If new versions of packages are released, they can all be updated with one command – very convenient.

What are dependencies for?

If you have already installed packages on Linux, then you may have noticed that you enter the name of one program to install, but often other packages are installed simultaneously with it – these are dependencies.

Dependencies are libraries, programs, drivers, etc. that are necessary for another program to work. This approach allows the same code (library) to be used in different programs. For example, we are writing an application in which we want to add network functions with support for various protocols – we do not need to write everything from scratch, we can use the libcurl library (from the authors of the cURL program), which already supports dozens of network protocols. And many other programs can do this – they can all use the same library without having to duplicate code – just install the library on the OS once.

Some programs require other programs to work, or they are wrappers, graphical interfaces to facilitate the use of the utility. Such programs are also dependencies.

Use apt or apt-get

The apt or apt-get command can be used to manage packages. In fact, there are even more choices: dpkg, aptitude, synaptic, wajig, as well as graphical interfaces from the distribution authors. But all these package managers have their own distinct features, but apt and apt-get are very similar, they have almost the same commands and options, they perform the same functions. Let's see how apt and apt-get differ.

apt is intended to be used interactively and its behavior may change with new versions. And the apt-get program is designed for use in scripts and its behavior and output is very conservative.

In fact, for most common tasks (installing and removing packages), the programs are very similar.

I prefer apt for being more “fresh”, but in fact, in almost all the following command examples, you can replace “apt” with “apt-get” and you won't feel any difference.

How to update the application information cache

When changes are made to the repository, for example when updating a package, you need to synchronize this information with the local cache of your Linux operating system. This is done with the command:

sudo apt update

The same result (cache refresh) can be achieved in different ways, including several more commands. For example, the following command will also update the cache (it doesn't really matter which command you use):

sudo apt-get update

Even if you are not going to perform a full system update, you must update the cache before installing any packages. Otherwise, you may run into the error that the required package was not found. This error occurs due to the fact that your system has information about the previous version of the package and a link to download it, and it is at this address that the package manager is trying to get the package file. But in the repository, the obsolete version may have already been removed. As a result, it turns out that the package is in the repository, but your system does not know its new address until it updates its cache.

How to install the program

Use a command like:

sudo apt install PACKAGE

You can install multiple packages at once:

sudo apt install PACKAGE1 PACKAGE2 PACKAGE3 PACKAGE4

Search for packages

To search for packages, use a command of the form:

apt search WORD

This command searches through package descriptions and may list too many. This command can be useful if the name of the required utility does not match the name of the package (for example, one package contains several utilities).

Note that this command, unlike most others with apt, does not require root privileges (can be used without sudo).

Search packages by name only

The previous command is useless due to too much output. You can use the following command to search by package name:

apt list PACKAGE

You can also use wildcards, for example to search for a name that starts with the word PACKAGE:

apt list PACKAGE*

To search for a name ending in PACKAGE:

apt list *PACKAGE

To search by name containing the word PACKAGE:

apt list *PACKAGE*

An even more convenient, in my opinion, command for searching for packages is:

apt-cache search PACKAGE

The last command searches the names for the word PACKAGE and lists all packages where it occurs. You do not need to use wildcards.

How to check if a package is installed

You can use the apt list or apt search commands not only to find packages, but also to see if they are installed.

Next to the package descriptions, you can see one of three options:

  • string “[installed]” – the package is installed manually
  • string “[installed,automatic]” – the package is installed as a dependency or as included in the metapackage (during the installation of the operating system, Kali Linux heavily uses metapackages, so many tools have this mark)
  • nothing is written – it means the package is not installed

How to view package information

You can find out the following information about any package, whether it is installed on your system or not:

  • version number
  • website address
  • short description
  • list of dependencies
  • installation size
  • source repository
  • priority
  • what kind of program it is

To find out information about a package, use a command like:

apt show PACKAGE

For instance:

apt show sqlmap

How to remove a package

To remove, use a command like

sudo apt remove PACKAGE

You can remove many packages at once:

sudo apt remove PACKAGE1 PACKAGE2 PACKAGE3 PACKAGE4

How to completely remove a package, along with configuration files

Removing a package removes all of its files, but usually small (modified) user configuration files remain in case the removal was accidental. In this case, simply submitting an install request for an accidentally deleted package will restore it to work as before. On the other hand, you can get rid of these leftovers by calling purge even for packages that have already been removed. Please note that this does not affect the data or configuration stored in your home directory.

So, to completely remove a package, use a command like:

sudo apt purge PACKAGE

Updating all packages on a system in Kali Linux

To update all packages for which new versions have been released, as well as install the necessary dependencies and remove interfering packages, use the command:

sudo apt update
sudo apt full-upgrade

There may be conflicting packages among the packages that are removed (preventing the installation of required dependencies).

A similar result can be obtained with the commands (this is one of the few commands that differs for apt and apt-get):

sudo apt update
sudo apt-get dist-upgrade
# or
sudo apt update
sudo aptitude full-upgrade

For convenience, you can combine the cache update command and the command for launching a full system update into one, the -y option is used so that we are not asked for confirmation, but the process of installing new packages immediately begins:

sudo apt update && sudo apt full-upgrade -y

How to find out which package contains a file

If you compile programs from source codes, then you have probably encountered compilation errors when the program does not find a necessary header file and compilation fails. What to do in this situation? It is clear that you need to install the package that contains the required file. But how do you find out the name of this package?

With the apt-file program, you can search by filenames in packages (both installed and not). It can also show all files in the package.

Program installation:

sudo apt install apt-file

Immediately after installation, the program data cache is empty. To update it, you need to run the command:

sudo apt-file update

When everything is ready, then the search is carried out as follows:

apt-file search FILE-NAME

For example, search for the ffi.h file:

apt-file search ffi.h

There are alternatives to apt-file: whichpkg (wajig), auto-apt, dlocate.

How to view package dependencies

You can see which packages will be installed as dependencies with a command like:

apt-cache depends PACKAGE

How to find out for which programs a given package is a dependency

If you want to uninstall a program, but are not sure if it is needed for other programs, then you can check for which packages it is a dependency with a command of the form:

apt-cache showpkg PACKAGE

For instance:

apt-cache showpkg nmap

How to view a list of package changes

For packages, you can list the changes made in different versions of the program. To do this, run a command of the form:

apt changelog PACKAGE

For instance:

apt changelog sqlmap

How to download a package without installing

If you want to download a package without installing, for example, to study it or make changes, then run a command like this:

apt download PACKAGE

Example:

apt download sqlmap

How to check if everything is ok with the application cache

If you want to find out if there are broken dependencies, then run the diagnostic command:

sudo apt-get check

How to install local .deb package

Sometimes it may be necessary to install local .deb files, that is, files not from repositories.

The following dpkg command will install the package without checking dependencies:

sudo dpkg -i <deb FILE>

When performing an operation by the package manager, for example, updating the cache:

sudo apt update

you may get a message about missing dependencies, you can fix this with the command:

sudo apt install -f

The apt program can also install packages from the specified files, while the necessary dependencies for this package will be installed at the same time:

sudo apt install ./PACKAGE.deb

Removing unnecessary programs

From time to time, it is recommended to run commands to remove packages that were automatically installed (since they were dependencies of other programs), but are no longer needed.

To do this, use the command:

sudo apt autoremove -y

Its use is safe and shouldn't lead to problems.

Clearing the installation file cache

With each software update, the package files are downloaded to the cache. After the update, the downloaded files (you can call them installation files) are not deleted, and gradually the cache grows to large sizes. This is done on purpose with the idea that if after the next update you find that the new package has problems, and the old version is no longer available in the online repository, then you can roll back to the old version by installing it from a file stored in the cache.

For rolling distributions, the cache grows very quickly. Therefore, from time to time you can run the command:

sudo apt clean -y

clean command cleans out the received package files from the local repository. It removes everything except the lock file from /var/cache/apt/archives/ and /var/cache/apt/archives/partial/.

Fixing package installation errors and dependencies

sudo apt install -f -y

The -f option fixes, tries to cure a system with broken dependencies. This option, when used with install/remove, can skip any packages to allow APT to find a likely solution. If packages are listed, this command should completely fix the problem.

Alternatives to apt

For apt, besides the apt-get program, which is very similar, there are several other alternatives.

On Debian, you can choose from the following package managers:

dpkg

dpkg – This package provides a low-level infrastructure for handling the installation and removal of Debian software packages. It does not install dependencies when installing programs.

aptitude

aptitude is a package manager with a number of useful features, including: a mutt-like syntax for flexible package matching, the ability to fetch and display the Debian changelog for most packages, and a command line mode similar to apt-get. The program does not allow the cache to grow too much and performs automatic cleaning.

To understand the main features of the program, take a look at the available commands and options.

Usage:

aptitude [-S fname] [-u|-i]
aptitude [options] <action> ...

Actions (if none is specified, aptitude will enter interactive mode):

 install         Install/upgrade packages.
 remove          Remove packages.
 purge           Remove packages and their configuration files.
 hold            Place packages on hold.
 unhold          Cancel a hold command for a package.
 markauto        Mark packages as having been automatically installed.
 unmarkauto      Mark packages as having been manually installed.
 forbid-version  Forbid aptitude from upgrading to a specific package version.
 update          Download lists of new/upgradable packages.
 safe-upgrade    Perform a safe upgrade.
 full-upgrade    Perform an upgrade, possibly installing and removing packages.
 build-dep       Install the build-dependencies of packages.
 forget-new      Forget what packages are "new".
 search          Search for a package by name and/or expression.
 show            Display detailed info about a package.
 showsrc         Display detailed info about a source package (apt wrapper).
 versions        Displays the versions of specified packages.
 clean           Erase downloaded package files.
 autoclean       Erase old downloaded package files.
 changelog       View a package's changelog.
 download        Download the .deb file for a package (apt wrapper).
 source          Download source package (apt wrapper).
 reinstall       Reinstall a currently installed package.
 why             Explain why a particular package should be installed.
 why-not         Explain why a particular package cannot be installed.

 add-user-tag    Add user tag to packages/patterns.
 remove-user-tag Remove user tag from packages/patterns.

Options:

 -h              This help text.
 --no-gui        Do not use the GTK GUI even if available.
 -s              Simulate actions, but do not actually perform them.
 -d              Only download packages, do not install or remove anything.
 -P              Always prompt for confirmation of actions.
 -y              Assume that the answer to simple yes/no questions is 'yes'.
 -F format       Specify a format for displaying search results; see the manual.
 -O order        Specify how search results should be sorted; see the manual.
 -w width        Specify the display width for formatting search results.
 -f              Aggressively try to fix broken packages.
 -V              Show which versions of packages are to be installed.
 -D              Show the dependencies of automatically changed packages.
 -Z              Show the change in installed size of each package.
 -v              Display extra information. (may be supplied multiple times).
 -t [release]    Set the release from which packages should be installed.
 -q              In command-line mode, suppress the incremental progress
                  indicators.
 -o key=val      Directly set the configuration option named 'key'.
 --with(out)-recommends     Specify whether or not to treat recommends as
                            strong dependencies.
 -S fname        Read the aptitude extended status info from fname.
 -u              Download new package lists on startup.
                  (terminal interface only)
 -i              Perform an install run on startup.
                  (terminal interface only)

synaptic

Synaptic is a graphical package management tool based on GTK+ and APT.

Synaptic allows you to install, update and remove software packages in a convenient way.

Besides these basic functions the following features are provided:

  • Search and filter the list of available packages
  • Perform smart system upgrades
  • Fix broken package dependencies
  • Edit the list of used repositories (sources.list)
  • Download the latest changelog of a package
  • Configure packages through the debconf system
  • Browse all available documentation related to a package (dwww is required)

wajig

Command line wrapper for apt, apt-cache, dpkg, aptitude and others. Its goal is to simplify package management by combining the main functions of these tools in one interface.

In addition to the programs discussed, Linux distributions can use their own tools. For example, Linux Mint has its own GUI tool for updating software. Also in Linux Mint, when you run the apt program, it will use its own script instead, which is a wrapper for aptitude (because of this, the output of the apt command may confuse you).

Portable programs in Linux

Linux programs can be used as portable. For example, you can download a .deb file, extract an executable file from it, and run it from anywhere. Some packages contain, in addition to the installation file, other files: configuration files, functionality extension files, etc., in this case, for most programs, you can specify the location of these files using the command-line options.

When compiling a program from source, it is not at all necessary to install it at the system level – you can run the compiled files from anywhere on your system. This allows you to try a new version of the program without removing the one installed from the repository.

How to solve the “command not found” problem

If, when trying to start a program, you encounter an error “command not found”, then it means that the package containing this program is not installed. Assuming you entered the command name correctly, of course – so start by checking the name, and also use the TAB key for prompts and auto-completion.

Start by trying to install a package with the same name – quite often the command name is the same as the package name, for example nmap, sqlmap are the launch commands and the names of the packages that contain them:

sudo apt install COMMAND

If you get a message that a package with this name was not found, then try searching for the package description – quite often the description lists the programs that are included in this package:

apt search COMMAND

If you can determine the package name, install it.

If that doesn't help, then install (if you haven't already) the apt-file command, update the cache and search:

apt-file search COMMAND

This method will surely help you find the package you want. Even if this did not work, then the following options are possible:

  • you are entering the command name incorrectly
  • this program is not in the repositories of your Linux distribution and you just have to install it manually – compile from the source code or download precompiled files, if the author of the program creates them

See also:

Recommended for you:

Leave a Reply

Your email address will not be published.