You arrive at a Linux server which has some history of neglect. Let’s suppose someone else neglected it but if your new-year resolution is to stop neglecting your beloved server, this applies as well.
One form of neglect is to install, install, install and never un-install any package. The common utility to perform installation and un-installation of packages is apt-get which adds to the problem because it doesn’t have automatic removal of non-needed dependences.
PHP and ton of other packages. phpMyAdmin was removed when it was no longer needed but Apache, PHP and the ton of packages remain there.
Aptitude to the rescue. Aptitude is another package manager front-end like apt-get but it can keep track of automatically and non-automatically installed packages. That means that when you installed phpMyAdmin it was marked as non-auto while Apache and company was marked as auto. When you remove phpMyAdmin all the non-needed automatically installed packages like Apache would be removed.
That is nice, but since the neglecting previous administrator didn’t use Aptitude, all the packages are marked as non automatically installed. The safe way so Aptitude doesn’t remove anything that it is needed.
So, what to do now ?
Well, the answer is: try to mark all files as automatically installed except those that you really want. To do that you can use the following (which you could write in one line if you want):
for pkg in $(aptitude search ~i | grep -v "i A" | cut -d " " -f 4) ; do echo "-- markauto $pkg --" aptitude markauto $pkg done
A little explanation about this. This piece:
aptitude search ~i | grep -v "i A" | cut -d " " -f 4
you can run it by itself. It list all installed packages, then grep remove all those marked as automatically (we really don’t care about them). The cut part extracts the name of the package.
Once you have the name of each package you print a little header to know what package we are talking about and then try to mark it as automatic with:
aptitude markauto <pkg name>
in many cases marking a package as auto will not remove it, because another package depends on it, on other cases it’ll remove the package and maybe some other non-needed packages. In those cases Aptitude will ask you wether you want to continue or not. That is where you’ll have to do your job, analyze wether those packages are needed or not. If they are, tell Aptitude not to perform the operation and the for loop will continue with the next package.
At last, you may also consider removing the configurations of all those removed packages. Before doing this, make backups, that’s very important and I meant it.
To remove the configurations you can use the following code:
for pkg in $(dpkg -l | grep ^rc | cut -d " " -f 3) ; do dpkg -P $pkg ; done
At the end you’ll have a system that is a bit cleaner.