Remove unused packages in Ubuntu? [4 Methods]


Ubuntu

Author: Omer Cakmak
Reviewer: Deepak Prasad

One of the most common operations when using an operating system is the package installation process. In this process, package(s) are installed, but sometimes unused packages remain in the system. These packages can sometimes cause problems in package dependencies.

In this article, we will share information about removing unused packages on Ubuntu, which is a Debian based distribution.

 

Different methods to remove unused packages in Ubuntu

To delete unused packages in Ubuntu can be done with apt package manager and packages installed with this package manager. Let's explain what to do with apt, then how to remove unused packages using a few packages. Finally, let's delete unused packages with a useful script.

 

Method -1-  Using apt parameters

While apt-get is used as the package manager in old ubuntu distributions, apt is used in new version Ubuntu. The package manager's parameters are used to remove unused packages. Let's look at the parameters of apt in order.

 

autoremove

The autoremove parameter is used to remove packages that are automatically installed to meet the dependencies of other packages. These packages are no longer needed because their dependencies have changed or the package(s) that need them have been removed.

Use as:

root@foc-ubuntu21:/# apt autoremove
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 248 not upgraded.

After the command, unused packages are listed and deleted after confirmation.

 

clean

The clean cleans the local repository of imported package files. Removes everything but the lock file from the /var/cache/apt/archives/ and /var/cache/apt/archives/partial/ directories.

Use as:

root@foc-ubuntu21:/# apt clean

 

autoclean

The autoclean cleans the local repository of received package files like the clean parameter. The difference is that it only removes package files that are no longer downloadable and largely useless.

Use as:

root@foc-ubuntu21:/# apt autoclean
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done

 

Method -2-  Using the popcon-largest-unused command

The popcon-largest-unused command, which comes with the popularity-contest package, lists the unused packages.

First, install it on the system:

root@foc-ubuntu21:/# apt install popularity-contest -y

When you want to run the post-installation command, you will receive the following warning. In the same warning, you will be shown what you need to do:

root@foc-ubuntu21:/# popcon-largest-unused
warning: Missing required file /var/log/popularity-contest.
info:    Run 'popularity-contest > /var/log/popularity-contest' to generate it.

Then run the following command:

root@foc-ubuntu21:/# popularity-contest > /var/log/popularity-contest

Now get a list from most used package to least used:

root@foc-ubuntu21:/# popcon-largest-unused
...
32 libencode-locale-perl
31 libhtml-tagset-perl
31 libfile-listing-perl
30 libwacom-bin
30 gkbd-capplet
29 libhttp-date-perl
27 perl-openssl-defaults
27 liblwp-protocol-https-perl
25 libextutils-pkgconfig-perl
22 tcl
19 libnet-smtp-ssl-perl

Now remove the least used package:

root@foc-ubuntu21:/# apt remove libnet-smtp-ssl-perl
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
  libauthen-sasl-perl
Use 'sudo apt autoremove' to remove it.
The following packages will be REMOVED:
  libmailtools-perl libnet-smtp-ssl-perl
0 upgraded, 0 newly installed, 2 to remove and 248 not upgraded.
After this operation, 248 kB disk space will be freed.
Do you want to continue? [Y/n]

With your approval, the package is removed from the system. You can see that the package was installed automatically and is no longer needed.

 

Method -3-  Using the deborphan command

The deborphan, finds packages that don't have packages linked to the package typed after the command.

Run the following command for installation:

root@foc-ubuntu21:/# apt install deborphan -y

Use as:

Usage: deborphan [OPTIONS] [PACKAGE]...

For an example:

root@foc-ubuntu21:/# deborphan -a
main/admin                deborphan
main/web                  firefox-locale-en
main/perl                 libauthen-sasl-perl
main/kernel               linux-generic-hwe-20.04
main/misc                 popularity-contest
main/python               python3-cffi-backend
main/utils                shim-signed
main/metapackages         ubuntu-minimal
main/metapackages         ubuntu-standard

The -a parameter checks all packages. It is best used with --priority (if used at all). This option means --show-section.

Remove an unused package:

root@foc-ubuntu21:/# apt remove firefox-locale-en
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
  libauthen-sasl-perl
Use 'sudo apt autoremove' to remove it.
The following packages will be REMOVED:
  firefox-locale-en
0 upgraded, 0 newly installed, 1 to remove and 247 not upgraded.
After this operation, 1.169 kB disk space will be freed.
Do you want to continue? [Y/n]

After your confirmation, the package will be deleted. After the deletion, the system will not have any problems, because it was not used anyway.

 

Method -4- Using unusedpkg (Bash Script)

WARNING:
This script is maintained by a third party owner on GitHub. Please make sure you test it thoroughly or go through the script before using it.

There is a bash script called unusedpkg, we will download this script and use. Download the file with the following command:

foc@foc-ubuntu21:~$ wget https://github.com/epinna/Unusedpkg/archive/refs/heads/master.zip
--2022-11-28 14:31:20-- https://github.com/epinna/Unusedpkg/archive/refs/heads/master.zip
...
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/zip]
Saving to: ‘master.zip’

master.zip [ <=> ] 26,30K --.-KB/s in 0,09s

2022-11-28 14:31:21 (281 KB/s) - ‘master.zip’ saved [26928]

Then extract the downloaded zip file and enter the directory:

foc@foc-ubuntu21:~$ unzip master.zip && cd Unusedpkg-master

Give unusedpkg file execute permission:

foc@foc-ubuntu21:~/Unusedpkg-master$ chmod +x unusedpkg

Then run:

foc@foc-ubuntu21:~/Unusedpkg-master$ ./unusedpkg 

* UnusedPkg	1.0
Find unused packages in your Linux system.

Database /home/foc/.unusedpkg/pkglist doesn't exist (first run?). Automatically executing './unusedpkg update'.

1476 packages available.
[18%] gir1.2-javascriptcoregtk-4.0:amd64                                [21%] gnome-shell-extension-appindicator  
...
[79%] network-manager-config-connectivity-ubuntu                        [90%] speech-dispatcher-audio-plugins:amd64                             Database update done.                                                

WARNING:
The reported idle days may be wrong. Before removing packages, check timestamps with
'unusedpkg info <package>' or check the timestamps with 'ls -alu'. See README for more info.

Minimum packages size: 1MB

IDLE DAYS 	SIZE PACKAGE NAME
2497		3MB	tzdata 
1057		1MB	printer-driver-m2300w 
1029		2MB	x11-apps 
913		   2MB	printer-driver-foo2zjs-common
...

UnusedPkg is a tool to find unused packages in Ubuntu operating system (All Debian based operating systems) sorted by their idle time. The packet with the largest idle time is at the beginning of the queue.
After this output, you can perform the deletion by looking at the dependencies of the relevant package.

For example, you can delete the printer-driver-foo2zjs-common package like this:

foc@foc-ubuntu21:~$ sudo apt remove printer-driver-foo2zjs-common
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
dc mscompress
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
printer-driver-foo2zjs printer-driver-foo2zjs-common
0 upgraded, 0 newly installed, 2 to remove and 249 not upgraded.
After this operation, 3.339 kB disk space will be freed.
Do you want to continue? [Y/n]

If you approve, the package will be deleted.

 

Summary

We explained how to delete unused packages on Ubuntu with the apt package manager, popcon-largest-unused and deborphan command.

You can get the parameters of the Deborphan package, and information about the popcon-largest-unused command from the popularity-contest package's man page.

If it is about the apt command, you can use the local manual page:

root@foc-ubuntu21:/# man apt
      
       apt - command-line interface

DESCRIPTION
       apt provides a high-level commandline interface for the package
       management system. It is intended as an end user interface and
       enables some options better suited for interactive usage by default
       compared to more specialized APT tools like apt-get(8) and apt-
       cache(8).
...

 

References

askubuntu.com - how do I eliminate unused packages in version

 

Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment