Uninstalling and ReInstalling PHP with apt on Ubuntu

Uninstalling

Uninstalling PHP on Ubuntu with the apt the package manager is a common task you may need to perform for various reasons, such as upgrading to a newer version, troubleshooting issues, or cleaning up unused software. In this blog post, we will guide you through uninstalling PHP step-by-step.

Step 1: Remove all PHP-related packages The first step in uninstalling PHP is to remove all PHP-related packages. This can be achieved by using the apt-get remove command with the --purge option. This option removes not only the packages themselves but also any configuration files associated with them. For example, to remove all PHP-related packages, you can run the following command:

For example, to remove all PHP-related packages, you can run the following command:

sudo apt-get remove --purge php*

This command will remove all packages that start with “php”, including the main PHP package, and any extensions or dependencies installed alongside it.

Step 2: Remove any remaining configuration files After removing all the PHP-related packages, you should run the autoremove command with the --purge option to remove any remaining configuration files. This helps to ensure that all traces of PHP have been removed from your system. You can do this by running the following command:

sudo apt-get autoremove --purge

Step 3: Verify that PHP has been completely uninstalled Finally, you can verify that PHP has been completely uninstalled by running the php command with the -v option. If PHP is still installed, you will see a version number. If not, you will see an error message indicating that PHP is not installed.

php -v

Re-Installing

Install PHP on your Ubuntu system, you can follow these simple steps:

Step 1: Update your package manager Before installing PHP, it’s always a good idea to make sure your package manager is up to date. You can do this by running the following command:

sudo apt update

Step 2: Install PHP and its dependencies Next, you can install PHP and its required dependencies by running the following command:

sudo apt install php php-cli php-fpm php-json php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath

This command will install the main PHP package, as well as various extensions and libraries that are commonly used with PHP.

Step 3: Verify your installation Finally, you can verify that PHP has been installed correctly by running the php command with the -v option. This will display the version of PHP that is currently installed on your system.

php -v

If everything was installed correctly, you should see an output that looks something like this:

PHP 8.1.2-1ubuntu2.11 (cli) (built: Feb 22 2023 22:56:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.11, Copyright (c), by Zend Technologies

In conclusion, installing PHP on Ubuntu is a straightforward process that can be accomplished using the apt package manager. By following these steps, you can quickly and easily install PHP and its required dependencies on your Ubuntu system.