Ubuntu PHP Setup

PHP and Required Extensions

sudo apt install php will install the latest stable version of PHP available in the default repositories for your distribution.

sudo apt-get install curl php php-cli php-mbstring unzip

If you want to install a specific version of PHP, add ondrej/php PPA which provides different PHP versions for Ubuntu, then you can specify the version number after the "php" package name, for example sudo apt install php7.4

# add the `ondrej/php` repo
sudo add-apt-repository ppa:ondrej/php
sudo apt-get install php8.2 php8.2-cli php8.2-common php8.2-imap php8.2-redis php8.2-snmp php8.2-xml php8.2-zip php8.2-mbstring php8.2-curl php8.2-mysql

Node, Git and npm

sudo apt install nodejs npm git

Composer

# Go to your home directory, then retrieve the installer using curl:
cd ~
# download composer
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
# Verify the hash of the downloaded PHP composer script with the signatures at the official page:
HASH=`curl -sS https://composer.github.io/installer.sig`
# validate if the PHP Composer installer can be safely executed or not:
php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
# Run the installer script
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer

Remove Composer

sudo apt-get remove composer
# If you want to remove any configuration files associated with Composer
sudo apt-get purge composer
# Finally, you can remove any remaining dependencies that were installed along with Composer
sudo apt-get autoremove

If you do have Composer installed, but the sudo apt-get remove composer command doesn't work, you can try removing Composer by running the following command:

sudo rm -rf /usr/local/bin/composer

How to Uninstall PHP on Ubuntu

To uninstall PHP from Ubuntu, follow the instructions below:

  1. Run the following command in the terminal:

    sudo apt-get purge php

For example, if you installed the PHP 7.4 FPM version, run:

sudo apt-get purge php7.4-fpm
  1. Remove the orphaned packages with:

    sudo apt-get autoremove

  2. Lastly, check the PHP version to confirm the uninstall worked:

    php -v