Simplifying Linux Maintenance: Set and Forget Unneeded Dependency Removal with Systemd

Simplifying Linux Maintenance: Set and Forget Unneeded Dependency Removal with Systemd

Why Remove Unneeded Dependencies?

When you install packages on your Linux system, they often come with dependencies that are required for the package to function properly. However, some of these dependencies may no longer be needed if you uninstall the package or if
-more commonly- the package has been updated to no longer require them. Over time, these unneeded dependencies can accumulate and take up disk space, slowing down your system and making package management more difficult.

To keep your system running smoothly, it's a good idea to periodically remove unneeded dependencies. In this tutorial, I'll show you how to use pacman to list and remove unneeded dependencies on Arch Linux, and how to automate the process using a systemd timer and service. I'll also briefly mention the required changes for Ubuntu (or any Linux distro that uses apt).

Removing Unneeded Dependencies on Arch Linux

To list unneeded dependencies on Arch Linux, you can use the following command:

pacman --query --deps --unrequired

or for the version with short flags:

pacman -Qdt

This will show a list of packages that are installed as dependencies but are no longer required by any installed packages.

To remove these unneeded dependencies, you can use the following command:

sudo pacman --remove --unneeded --recursive --noconfirm <package_name>

short version:

sudo pacman -Rus --noconfirm <package_name>

Replace <package_name> with the name of the package you want to remove. You can also use the output of the pacman -Qdt command to remove all unneeded dependencies at once:

pacman -Qdt | awk '{print $1}' | xargs sudo pacman -Rus --noconfirm

This command uses awk to extract the package names from the output of the
pacman -Qdt command, and then uses xargs to pass them to the
pacman -Rus --noconfirm command.

Automating the Process with systemd

While manually running the pacman commands to remove unneeded dependencies is straightforward, it can be tedious to do this on a regular basis. To automate the process, we'll use systemd, the system and service manager for Linux.

Creating the Bash Script

Before we can create a systemd service and timer, we need to create a bash script that will remove unneeded dependencies. Here's the script for Arch Linux:

#!/bin/bash
pacman -Qdt | awk '{print $1}' | xargs sudo pacman -Rus --noconfirm

This script uses the pacman -Qdt command to list unneeded dependencies, and then pipes the output to awk to extract the package names. Finally, xargs is used to pass the package names to the pacman -Rus --noconfirm command to remove the packages.

Save this script to a file called remove-unneeded.sh in the $HOME/.local/bin directory. You can create the directory if it doesn't exist:

mkdir -p $HOME/.local/bin

Make the script executable by running:

chmod +x $HOME/.local/bin/remove-unneeded.sh

Creating the Systemd Service

Now that we have our bash script, we can create a systemd service to run the script. Here's the service file:

[Unit]
Description=Remove Unneeded Dependencies
[Service]
Type=simple
ExecStart=$HOME/.local/bin/remove-unneeded.sh
[Install]
WantedBy=default.target

Save this file as remove-unneeded.service in the $HOME/.config/systemd/user directory.

Creating the Systemd Timer

The final step is to create a systemd timer that will run the service on a regular schedule. Here's the timer file:

[Unit]
Description=Run Remove Unneeded Dependencies Service Weekly

[Timer]
OnCalendar=weekly
Persistent=true

[Install]
WantedBy=timers.target

This timer will run the remove-unneeded.service service once a week. The Persistent=true option ensures that if the timer is missed due to the system being powered off or otherwise unavailable, it will run as soon as possible after the system is available again.

Save this file as remove-unneeded.timer in the $HOME/.config/systemd/user directory.

Starting and Enabling the Timer

Once the service and timer files are in place, we can start and enable the timer with the following command:

systemctl --user enable --now remove-unneeded.timer

This will both start the timer and enable it to run on boot.

Ubuntu Version of the Bash Script

If you're using Ubuntu, you can use the following bash script instead:

#!/bin/bash
# list unneeded dependencies and remove them
sudo apt-get autoremove -y

This script uses the apt-get autoremove command to remove unneeded dependencies.

Save this script to a file called remove-unneeded.sh in the $HOME/.local/bin directory, make it executable with chmod +x $HOME/.local/bin/remove-unneeded.sh, and follow the same steps as above to create the systemd service and timer files.

A Note on Ubuntu vs Arch Linux

It's worth noting that the need for regularly removing unneeded dependencies may be less critical in Ubuntu than in Arch Linux. Ubuntu typically has longer release cycles and a more stable package base, so packages with changed dependencies are more likely to be scheduled for a new release rather than being pushed to the current version. Arch Linux, on the other hand, is a bleeding-edge distribution that updates packages frequently, making the accumulation of unneeded dependencies a more common occurrence. Nonetheless, automating the removal of unneeded dependencies with Systemd is a great way to keep your Linux system clean and efficient, regardless of the distribution you're using.