# Package Management

In the daily use and system administration of Linux operating systems, managing software packages is critically important. Linux distributions provide various package managers to facilitate tasks such as software installation, updating, and removal. These package managers allow you to manage software in package formats, along with their dependencies.

Each Linux distribution uses specific package management systems and package formats. In this section, we will explore package management tools commonly used in Debian-based Linux distributions and their basic usages.

#### Key Features of Package Managers

Linux package managers offer various features for managing software packages:

* **Package Downloading:** Automatically download software packages and dependencies from the internet.
* **Dependency Resolution:** Identify and automatically install the dependencies required by a package.
* **Package Installation and Removal:** Install software packages to the system and remove them when necessary.
* **Updates and Upgrades:** Check for new versions of installed packages and update them.
* **Configuration Files and Directories:** Provide standards for the configuration files and directories of software packages.

#### Advanced Package Manager (APT)

APT (Advanced Package Tool) is a powerful tool used to manage software packages in Debian-based Linux distributions such as Ubuntu. This tool allows users to easily install new software, update existing software, remove unnecessary ones, and automatically resolve dependencies between software packages. With simple command-line commands, users can keep their systems up-to-date and secure, while also quickly accessing the software they need. The convenience and efficiency offered by APT make it a popular choice among Linux users.

Updating Package Lists

APT keeps package lists in a database on your local system. It doesn't connect to servers each time you search, thus providing faster results, but the results might not be up-to-date. To get the most current results, you need to update your lists.

To update your package list with apt, use the **update** command.

```auto
user@hackerbox:~$ sudo apt update
Hit:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:2 http://us.archive.ubuntu.com/ubuntu bionic InRelease
Hit:3 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:4 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease
Reading package lists... Done
Building dependency tree       
Reading state information... 
DoneAll packages are up to date.
```

Searching in Package Lists

Now that we have updated the package lists in the database, we can search within those lists. For example, to search for a package with the name **htop**, you can perform the following search.

```auto
user@hackerbox:~$ sudo apt search htop
aha - ANSI color to HTML converter
htop - interactive processes viewer
libauthen-oath-perl - Perl module for OATH One Time Passwords
```

As you can see in the results, in addition to the **htop** package, there are other unrelated packages. This is because the apt search command also searches within the package descriptions. For example, if we look at the description of the **aha** package, we can see where htop is mentioned.

The apt search command supports regular expressions. For example, to search for packages starting with htop, you could do:

```auto
user@hackerbox:~$ sudo apt search ^htop
htop - interactive processes viewer
```

Or, you could search for packages whose names contain the term **htop**.

```auto
user@hackerbox:~$ sudo apt search --names-only htop
htop - interactive processes viewer
```

Installing and Updating Packages

To install a package found, use the apt command.

```auto
user@hackerbox:~$ sudo apt install htop
Reading package lists... Done
Building dependency tree   
Reading state information... Done
The following NEW packages will be installed:  
    htop0 
upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 80.0 kB of archives.
After this operation, 201 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/universe amd64 htop amd64 2.1.0-3 [80.0 kB]
Fetched 80.0 kB in 1s (110 kB/s)    
Selecting previously unselected package htop.
(Reading database ... 32507 files and directories currently installed.)
Preparing to unpack .../htop_2.1.0-3_amd64.deb ...
Unpacking htop (2.1.0-3) ...Setting up htop (2.1.0-3) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
```

This command also updates an existing installed package to the latest version if it's already installed.

To update all packages on your system, you can use the **upgrade** command.

```auto
user@hackerbox:~$ sudo apt upgrade
```

This method will not install any new packages or remove any old ones. If this isn't a concern, you can use the **dist-upgrade** command.

```auto
user@hackerbox:~$ sudo apt dist-upgrade
```

Removing Packages

To remove a package, use the **remove** command.

```auto
user@hackerbox:~$ sudo apt remove htop
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:  htop0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 201 kB disk space will be freed.
(Reading database ... 32558 files and directories currently installed.)
Removing htop (2.1.0-3) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
```

The output above shows that the htop package has been successfully removed from the system, freeing up 201 kB of disk space.

The **remove** command does not delete the configuration files and deb files for **htop**. To remove those as well, you would use the **purge** command.

```auto
user@hackerbox:~$ sudo apt purge htopReading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:  
    htop
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 201 kB disk space will be freed.
(Reading database ... 32558 files and directories currently installed.)
Removing htop (2.1.0-3) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Purging configuration files for htop (2.1.0-3) ...
```
