Complete Guide to Completely Uninstall PHP 7 in Ubuntu 18.04

Nov 25, 2025 · Programming · 22 views · 7.8

Keywords: Ubuntu | PHP uninstallation | version management

Abstract: This article provides a comprehensive guide for completely removing PHP 7.x versions from Ubuntu 18.04 systems, including using apt-get purge commands to remove all PHP 7 related packages, cleaning system cache and dependencies. It also covers steps for reinstalling specific PHP versions and configuring Apache modules to resolve version conflicts after system upgrades.

Problem Background and Challenges

During Ubuntu system upgrades, PHP version changes often cause application compatibility issues. When users upgrade from Ubuntu 14.04 to 18.04, PHP may be automatically upgraded from 5.6 to 7.2, while existing applications might only support older PHP versions. In such cases, simple apt-get remove php commands often fail to completely remove PHP 7 due to multiple PHP-related packages and dependencies present in the system.

Methods for Complete PHP 7 Removal

To fully remove PHP 7.x series versions, more precise package matching and cleanup commands are required. The following steps are based on Ubuntu 18.04's APT package management system:

sudo apt-get purge php7.*
sudo apt-get autoclean
sudo apt-get autoremove

Here, the purge php7.* command removes all packages starting with php7 and their configuration files. autoclean cleans obsolete deb package caches, while autoremove automatically removes orphaned dependency packages.

Reinstalling Specific PHP Versions

After uninstallation, if specific PHP versions need to be reinstalled, you can add Ondřej Surý's PPA repository, which provides maintained versions of multiple PHP releases:

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

Then install the desired version as needed:

sudo apt-get install php7.0  # Install PHP 7.0
sudo apt-get install php7.1  # Install PHP 7.1
sudo apt-get install php7.2  # Install PHP 7.2

Apache Module Configuration

If the system uses Apache as the web server, corresponding PHP modules need to be configured:

sudo a2dismod php7.0        # Disable old version module
sudo a2enmod php7.2         # Enable new version module
sudo service apache2 restart # Restart Apache service

Extension Package Installation

To ensure application normal operation, it's recommended to install a complete PHP extension suite:

sudo apt install php7.2-common php7.2-mysql php7.2-xml php7.2-xmlrpc php7.2-curl php7.2-gd php7.2-imagick php7.2-cli php7.2-dev php7.2-imap php7.2-mbstring php7.2-opcache php7.2-soap php7.2-zip php7.2-intl -y

Common Issues and Solutions

The Apache service startup failure mentioned in the reference article is typically caused by PHP module configuration errors. By completely removing conflicting PHP versions and reinstalling the correct versions, such issues can be resolved. It's recommended to backup important configuration files before operations and conduct thorough testing in production environments.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.