How to Install and Uninstall PHP on Ubuntu (F.A.Q)
What is the difference between apt remove and apt purge when uninstalling PHP?
apt remove
: This command removes the PHP packages but retains the configuration files. This is useful if you plan to reinstall PHP later and want to keep your settings.apt purge
: This command removes both the PHP packages and their associated configuration files. Use this command if you want to completely remove PHP and all its traces from your system.
How do I switch between different PHP versions on Ubuntu?
If you need to switch between different PHP versions (for example, PHP 7.4 and PHP 8.0), you can use the update-alternatives
command to set the desired version as the default.
- Install the desired PHP versions:
sudo apt install php7.4 php8.0
- Configure
update-alternatives
:sudo update-alternatives --set php /usr/bin/php7.4
To switch to PHP 8.0, run:
sudo update-alternatives --set php /usr/bin/php8.0
- Verify the active version:
php -v
How do I restart Apache after installing or uninstalling PHP?
After installing or uninstalling PHP, it’s a good idea to restart Apache to ensure the changes take effect:
sudo systemctl restart apache2
This command restarts the Apache service, incorporating any changes made to PHP.
How do I check which PHP modules are installed and enabled?
To list all installed PHP modules, use the following command:
php -m
This command displays a list of installed and enabled PHP modules. To check specific modules, such as curl
or mbstring
, look for their names in the output list.