Uninstall PHP 7 in Ubuntu 18.04
Running sudo apt-get remove php or sudo apt-get remove --purge php often leaves a phantom version of PHP installed on Ubuntu 18.04, even though the package manager claims it is gone. You might see output indicating that packages are not installed, yet executing php --version still returns version 7.2. This discrepancy usually stems from how Debian-based systems handle multi-version installations and the distinction between removing a package versus removing its configuration files and dependencies.
The core issue is that the generic php metapackage in Ubuntu often acts as an umbrella for specific versions (like php7.2) rather than a monolithic binary. When you uninstall the metapackage, the system might leave behind the actual versioned packages (php7.2, php7.2-cli, php7.2-fpm) if they were installed directly or if dependencies weren't fully resolved during removal. Additionally, Ubuntu 18.04 frequently bundles PHP with other extensions and modules that are not strictly part of the main package group but are pulled in as dependencies. Simply running remove commands might strip the main executable path but leave these ancillary components behind.
To truly uninstall PHP 7.2 on Ubuntu 18.04, you must explicitly target the versioned packages. Start by listing all installed packages that belong to the PHP 7 family:
dpkg -l | grep php7.2
This command reveals the specific binaries still occupying disk space and memory mappings. You will likely see entries for php7.2, php7.2-cli, php7.2-common, php7.2-fpm, and various extension modules like php7.2-mysql or php7.2-gd.
Once identified, iterate through these packages to remove them individually. The most thorough approach uses the --purge flag to strip both the binary files and their configuration directories:
sudo apt-get remove --purge php7.2 php7.2-cli php7.2-common php7.2-fpm php7.2-dev
If you installed extensions, ensure those are purged as well. For example:
sudo apt-get remove --purge php7.2-mysql php7.2-gd php7.2-xml php7.2-curl
After removing the versioned packages, check for any remaining orphaned dependencies that might have been installed alongside PHP 7. These are often libraries required by other services but no longer needed:
sudo apt-get autoremove
This step cleans up unused libraries and ensures your system state is consistent with a fresh installation or a move to a different PHP version, such as the default php8.1 or php8.2 available in Ubuntu 20.04 LTS repositories (though note that PHP 7.x is still supported in Ubuntu 18.04 repos until their end-of-life dates).
A common pitfall occurs when developers assume removing the metapackage removes everything. The package manager treats php as a virtual package pointing to the actual versioned implementations. If you only remove the virtual package without addressing the underlying versions, the binaries persist because they are not considered "uninstalled" by the system's dependency resolver.
Another consideration is the presence of legacy configurations in /etc/php/. Even after purging packages, files in /etc/php/7.2/apache2/, /etc/php/7.2/cli/, or /etc/php/7.2/fpm/ might remain if they were manually edited or created outside of package management. While not strictly necessary for the CLI to stop working, cleaning these directories prevents confusion if you reinstall PHP later:
sudo rm -rf /etc/php/7.2
Finally, verify the cleanup by running php --version again. If it still shows version 7, check if a non-standard binary path is being invoked or if an alternative package like php7.4 was inadvertently installed alongside and not removed. In some cases, third-party repositories (like Ondrej's PHP repository) might have installed conflicting versions that standard apt commands don't catch without specifying the full package name.
If you are migrating from Ubuntu 14.04 to 18.04 and need to run legacy applications on PHP 5.6 alongside a newer stack, consider using containerization or chroot environments rather than trying to maintain multiple versions of PHP directly on the host OS. This isolates dependencies and avoids the complexity of manual package purging.