How to Remove Packages Using the Command Line in Linux.

How to Remove Packages Using the Command Line in Linux.

Apt

The apt command is a powerful command-line tool, which works with Ubuntu’s Advanced Packaging Tool (APT) performing such functions as installing new software packages, upgrading existing software packages, updating the package, upgrading the entire Ubuntu system, and even removing packages.

List installed packages

You can list all installed packages by using the dpkg --list, apt list --installed command or grep with whatever your memory serves you.

dpkg --list

$ sudo dpkg --list

Output

Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                  Version                            Architecture Description
+++-=====================================-==================================-============-===============================================================================
ii  adduser                               3.118ubuntu5                       all          add and remove users and groups
ii  amd64-microcode                       3.20191218.1ubuntu2                amd64        Processor microcode firmware for AMD CPUs
ii  apparmor                              3.0.3-0ubuntu1                     amd64        user-space parser utility for AppArmor
ii  apport                                2.20.11-0ubuntu71.1                all          automatically generate crash reports for debugging

...
..
.

ii  distro-info-data                      0.51ubuntu1.1                      all          information about the distributions' releases (data files)
ii  dmeventd                              2:1.02.175-2.1ubuntu3              amd64        Linux Kernel Device Mapper event daemon
ii  dmidecode                             3.3-3                              amd64        SMBIOS/DMI table decoder
ii  dmsetup                               2:1.02.175-2.1ubuntu3              amd64        Linux Kernel Device Mapper userspace library
ii  dosfstools                            4.2-1build2                        amd64        utilities for making and checking MS-DOS FAT filesystems
ii  dpkg                                  1.20.9ubuntu2                      amd64        Debian package management system
ii  duf   <<<                             0.6.2-1                            amd64        Disk Usage/Free Utility
ii  e2fsprogs                             1.46.3-1ubuntu3                    amd64        ext2/ext3/ext4 file system utilities
ii  eatmydata                             105-9build2                        all          Library and utilities designed to disable fsync and friends


apt list --installed

$ sudo apt list --installed

Output

adduser/impish,now 3.118ubuntu5 all [installed,automatic]
amd64-microcode/impish,now 3.20191218.1ubuntu2 amd64 [installed,automatic]
apparmor/impish,now 3.0.3-0ubuntu1 amd64 [installed,automatic]
apport-symptoms/impish,now 0.24 all [installed,automatic]
apport/impish-updates,now 2.20.11-0ubuntu71.1 all [installed,automatic]

...
..
.

distro-info-data/impish-updates,impish-security,now 0.51ubuntu1.1 all [installed,automatic]
distro-info/impish,now 1.0 amd64 [installed,automatic]
dmeventd/impish,now 2:1.02.175-2.1ubuntu3 amd64 [installed,automatic]
dmidecode/impish,now 3.3-3 amd64 [installed,automatic]
dmsetup/impish,now 2:1.02.175-2.1ubuntu3 amd64 [installed,automatic]
dosfstools/impish,now 4.2-1build2 amd64 [installed,automatic]
dpkg/impish,now 1.20.9ubuntu2 amd64 [installed,automatic]
duf/impish,now 0.6.2-1 amd64 [installed] <<<
e2fsprogs/impish,now 1.46.3-1ubuntu3 amd64 [installed,automatic]
eatmydata/impish,now 105-9build2 all [installed,automatic]


grep

The grep command will show all the installed packages that have the string 'du' in their name anywhere, not just the beginning.

$ sudo apt list -- installed | grep -i du

Output

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

adduser/impish,now 3.118ubuntu5 all [installed,automatic]
bsdutils/impish-updates,impish-security,now 1:2.36.1-8ubuntu2.2 amd64 [installed]
duf/impish,now 0.6.2-1 amd64 [installed]  <<<
findutils/impish,now 4.8.0-1ubuntu2 amd64 [installed]
libmodule-find-perl/impish,now 0.15-1 all [installed,automatic]
libmodule-scandeps-perl/impish,now 1.30-1 all [installed,automatic]
libpam-modules-bin/impish,now 1.3.1-5ubuntu11 amd64 [installed,automatic]
libpam-modules/impish,now 1.3.1-5ubuntu11 amd64 [installed,automatic]
...
..
.


Remove a Package(s)


1. Using apt purge

$ sudo apt remove <package-name>

'remove' only gets rid of the package leaving any configuration files untouched.

Tip

Multiple Packages: You may specify multiple packages to be installed or removed, separated by spaces.


2. Using apt purge

$ sudo apt purge <package-name>

'purge' not only removes the package but also removes all configuration files. This is done on purpose since the same software will utilize the same configuration files if you reinstall it.

3. Using apt autoremove

Installed Linux apps rely on additional packages to work. When you remove software, it's possible that certain packages that the uninstalled application relied on are no longer needed. Use the 'autoremove' command to get rid of any unwanted packages.

$ sudo apt autoremove <package-name>


4. Combining both commands

You can combine the two commands for removing a program and removing dependencies that are no longer needed.

$ sudo apt purge --auto-remove <package-name>


5. Clean up

$ sudo apt clean 


The above command removes downloaded archive files from the aptitude cache in '/var/cache/apt/archives'. When you install a program, the package file is downloaded and stored in that directory. The files in the directory aren't required to be kept. The only disadvantage of uninstalling them is that if you desire to reinstall any of those programs, you will have to download the packages again.


Little weird Secret 😜

Both 'purge' and 'remove', as opposed to their primary duty, may be used to INSTALL packages. This is accomplished by adding a '+' to the end of the package name.

$ sudo apt <remove/purge> <package-name>+

As we have seen earlier, uninstalling programs via CLI is a process that is both fundamental and crucial. This also contributes to the security and stability of your Linux system.