How to Install PHP (8.2, 8.1, 8.0 7.4) on Ubuntu 22.04

Updated: May 23rd, 2023, 10:53:53 IST
Published: May 23rd, 2023
How to Install PHP (8.2, 8.1, 8.0 7.4) on Ubuntu 22.04
Title: How to Install PHP (8.2, 8.1, 8.0 7.4) on Ubuntu 22.04

PHP is a widely used server-side scripting language that creates many dynamic websites and applications. In this blog post, we will guide you through the step-by-step process of installing PHP versions 8.2, 8.1, and 7.4 on Ubuntu 22.04. This will allow you to choose the PHP version that best suits your application's requirements. To install PHP versions 8.2, 8.1, 8.0, or 7.4 on Ubuntu, we are using Ondrej PPA (Personal Package Archive).

Prerequisites:

Before you begin, make sure that you have:

  • An Ubuntu 22.04 system already installed with administrative access. (Using WSL install Ubuntu from Microsoft Store)
  • A terminal or command-line interface to execute commands.
  • An active internet connection.

For installing PHP on an Ubuntu 22.04 LTS machine, we are going to use the Ondrej PPA. This bundle includes PHP 8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, 7.0, and PHP 5.6. You can install any of the versions that your application requires. It is recommended that new application developers utilize the most recent PHP version, namely PHP 8.2.

This guide will teach you how to install PHP on an Ubuntu 22.04 LTS machine. This lesson also works with Ubuntu 20.04 LTS and 18.04 LTS computers.

Step 1: Update System Packages

To ensure that you have the latest package information, open a terminal and execute the following commands:

sudo apt update
sudo apt upgrade

Step 2. Install PHP on Ubuntu 22.04

PHP installation process on Ubuntu PCs is rather simple. Simply add the necessary PPA and you may install any PHP version on your Ubuntu laptop.

To install PHP on Ubuntu 22.04, execute the following command in the terminal:

1. Install the following dependencies, which are essential before installing this latest version:

sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https 

2. Add the Ondrej PPA (Personal Package Archive) to your system, which contains all versions of PHP packages for Ubuntu systems.

LC_ALL=C.UTF-8 sudo add-apt-repository ppa:ondrej/php 

3. Now, update and upgrade the Apt package manager cache.

sudo apt update && sudo apt upgrade

4. PHP 8.2, 8.1, 7.4, 7.3, 7.2, and PHP 5.6 are all available in the SURY repository. Although PHP 8.2 is the most recent stable version, a substantial number of websites still require PHP 7.x. Any of the needed PHP versions can be installed on your system.

The SURY repository is a popular third-party PHP package repository. It is maintained by Ondrej Sury, a Debian engineer and PHP team member. The SURY repository contains the most recent PHP packages for different Ubuntu and Debian editions.

The SURY repository's main goal is to provide PHP versions that are not accessible in the official Ubuntu repositories or for users who like to have the most recent PHP releases. It makes it simple for developers and system administrators to install and upgrade PHP versions, including minor updates and major releases.

To install a specific PHP version, execute the appropriate command in the terminal:

  • For PHP 8.2 Installation:
    sudo apt install php8.2
    
  • For PHP 8.1 Installation:
    sudo apt install php8.1
    
  • For PHP 8.0 Installation:
    sudo apt install php8.0
    
  • For PHP 7.4 Installation:
    sudo apt install php7.4
    

To install PHP on Ubuntu, replace version 8.2, 8.1, 7.4, or 5.6 with the needed PHP version. Even multiple PHP versions can be installed on a single Ubuntu machine.

5. Install Additional PHP Extensions (Optional)

If your application requires additional PHP extensions, you can install them using the apt package manager. For example, to install the MySQL extension for PHP, run the following command:

sudo apt install php-extension-name

Replace extension-name with the desired extension, such as mysql for MySQL support or gd for image manipulation. You can install multiple extensions such as “php-mbstring, php-mysql, php-xml, and php-curl” by running the below-given command:

sudo apt install php8.2-mysql php8.2-mbstring php8.2-xml php8.2-curl 

Step 3. Check the Current Active PHP Version

To verify the PHP installation, run the following command:

php -v

OR

php --version

This will display the PHP version and other relevant information if the installation was successful.

Output:

PHP 8.2.1 (cli) (built: Jan 13 2023 10:43:08) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.1, Copyright (c), by Zend Technologies

PHP Configuration Files

1. Main PHP Configuration Files - all the configuration files related to PHP 8.2 are located below:

  • PHP CLI (Command Line Interface): /etc/php/8.2/cli/php.ini
  • PHP with Apache: /etc/php/8.2/apache2/php.ini
  • PHP-FPM (FastCGI Process Manager): /etc/php/8.2/fpm/php.ini

2. Additional PHP Configuration Files:

  • PHP modules directory: /etc/php/8.2/mods-available
  • PHP active modules configuration directory:
    • PHP CLI: /etc/php/8.2/cli/conf.d/
    • Apache: /etc/php/8.2/apache2/conf.d/
    • PHP-FPM: /etc/php/8.2/fpm/conf.d/

Note: Replace the version number (8.2 in the above examples) with your desired PHP version to access the respective configuration files.

Remember to restart your web server (Apache, Nginx) or PHP-FPM service after making changes to the PHP configuration files for the modifications to take effect.

It's important to refer to the official PHP documentation or your distribution's documentation for accurate file paths and additional details related to your specific PHP version and operating system.

Switch Default PHP Version for CLI on Ubuntu

1. Check the available PHP versions:

sudo update-alternatives --list php

2. Select the desired PHP version as the default for the CLI:

sudo update-alternatives --set php /usr/bin/phpX.X

Replace X.X with the desired PHP version number. For example, 7.4 or 8.2.

3. Verify the updated default PHP version:

php -v

This command will display the PHP version, confirming that the switch was successful.

You can change the default PHP version for the CLI on your Ubuntu system by following these instructions. Please keep in mind that this update only affects the PHP version used in the command-line interface and not the PHP version used by the web server (such as Apache or Nginx).

Uninstalling PHP

To uninstall a specific PHP version from your Ubuntu system, you can use the following command:

sudo apt remove phpX.X

Replace X.X with the version number you want to remove. For example, to uninstall PHP 8.2, you would run:

sudo apt remove php8.2

This command removes the specified PHP version from your system.

Additionally, if you want to uninstall all the modules associated with that PHP version, you can use the following command:

sudo apt remove phpX.X-*

Again, replace X.X with the version number. For example, to remove all modules for PHP 8.2, you would run:

sudo apt remove php8.2-*

By running these commands, you can uninstall the specified PHP version and its associated modules from your Ubuntu system. It will free up disk space and remove any potential security risks associated with the unused PHP version.

Conclusion

You have successfully installed PHP versions 8.2, 8.1, 8.0, or 7.4 on your Ubuntu system using the Ondrej PPA repository. You can now develop and host web applications using the PHP version that meets your project requirements. Remember to configure your web server (such as Apache or Nginx) to work with the desired PHP version.

Note: Ondrej PPA provides updated PHP versions for Ubuntu, but it is a third-party repository. Ensure you trust the source and refer to the official documentation for any specific instructions or updates related to the PHP versions you are installing.