PHP (Hypertext Preprocessor) is a server-side scripting language used for web development, which is open-source and widely used around the world. It enables the creation of dynamic web pages, the manipulation of databases, and the handling of user authentication and authorization. In this tutorial, you will learn how to install PHP on Ubuntu 22.04
Before dive in to PHP installation keep in mind that, quick command of PHP installation on Ubuntu will install the prepackaged PHP version in Ubuntu 22.04(Which may not be the latest version). If you are looking for latest version of PHP or you may need older version, then you need to add Ondrej PPA for installing PHP on Ubuntu 22.04 LTS system. Which contains PHP 8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, 7.0 & PHP 5.6 packages. By using Ondrej PPA, you can install any version of PHP according to your requirement.
Before Installation Step
Always remember to update and upgrade your system before doing any system changes in Ubuntu or any Linux based operating system.
To update and upgrade the system with latest packages, run following command:
sudo apt update && sudo apt upgrade
Press ‘y’ to continue.
Install PHP on Ubuntu 22.04
If you are not looking for specific version, PHP installation is pretty straight forward. Just you have to run following command to install PHP on Ubuntu.
sudo apt install php
It will install the prepackaged version of PHP, remember as I said earlier it may not be the latest version or specific PHP version you want. To check the installed version, run following php version check command.
php -v
It will show the installed version of PHP. Following should be the desired output
Install PHP on Ubuntu 22.04 ( Latest Version or Specific Version)
For installing latest PHP version or any required version, first we need to install a few dependencies followed by adding Ondrej PPA to the system, we can install any required version, be it latest or specific version of our choice.
Step 1: Dependencies Installation
Install required dependencies by following command to continue with PHP installation on Ubuntu 22.04
sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https
Step 2: Add Ondrej PPA to the sytem
Ondrej PPA contains all the versions of PHP, add the package using following command.
LC_ALL=C.UTF-8 sudo add-apt-repository ppa:ondrej/php
On running this command, it will ask for permission. Press “Enter” to continue.
Step 3: Update Package Manager Cache
Now, update package manager using following command.
sudo apt update
Step 4: Install PHP on Ubuntu 22.04 (Any Version)
Ondrej PPA (SURY) repository contains PHP 8.2, PHP 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, 7.0 & PHP 5.6. You can install any version. Remember till now, most of the websites still require PHP 7, although PHP 8.2 is latest stable version. Use following commands to install your required version.
Install PHP 8.2 (Latest Version)
sudo apt install php8.2
Install PHP 8.1
sudo apt install php8.1
Install PHP 7.4
sudo apt install php7.4
Install PHP 5.6 (End of Life)
sudo apt install php5.6
Just replace the version number as above shown, you can install PHP on Ubuntu 22.04 as required.
You can install multiple PHP versions on single Ubuntu system for different PHP applications as needed.
Install PHP Extensions on Ubuntu
Most of the PHP Applications or frameworks need various PHP Extensions to run the application on Ubuntu. Let me consider I am installing WordPress on Ubuntu server. In that case, I will require some PHP extensions to run it properly on the server. Let’s install those PHP extensions.
For WordPress, you will need following extensions.
php-mysql, php-mbstring, php-xml, php-curl, php-gd,, php-xmlrpc, php-soap, php-intl, php-zip
Let’s install php extensions at once by below command.
sudo apt install php8.2-mysql php8.2-mbstring php8.2-xml php8.2-curl php8.2-gd php8.2-xmlrpc php8.2-soap php8.2-intl, php8.2-zip
Change your version number according to your installation.
Check PHP Version (Active)
Now, after installation check your installed PHP version is whether correct or not. Run below command to check the version of PHP.
php -v
PHP Configurations Files Location on the Ubuntu System
The location of PHP configuration files can vary depending on the operating system, web server, and PHP version you are using. In our case, PHP configuration files such as (php.ini) file and other PHP configuration files are stored under /etc/php directory with installed version numbers. Let me consider, you installed PHP version 8.2 on your Ubuntu system, then all the configuration files will be at following location:
PHP configuration files location on Ubuntu 22.04
Important PHP configuration files location:
- PHP CLI: /etc/php/8.2/cli/php.ini
- Apache: /etc/php/8.2/apache2/php.ini
- PHP FPM: /etc/php/8.2/fpm/php.ini
All the installed PHP modules are stored under /etc/php/8.2/mods-available directory.
PHP Active modules configuration directory location:
- 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/
If you are using other versions, just replace the version number (8.2) with your installed version.
How to Switch Default PHP Versions on Ubuntu
If you want to change the default PHP version, use ‘update-alternatives’ command. Run below command to change the default version for CLI and Apache
sudo update-alternatives --config php
It will show the default PHP version selection option. Ideally output should look like below.
There are 4 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php8.1 81 auto mode
1 /usr/bin/php5.6 56 manual mode
2 /usr/bin/php7.4 74 manual mode
3 /usr/bin/php8.0 80 manual mode
4 /usr/bin/php8.1 81 manual mode
5 /usr/bin/php8.2 82 manual mode
Press to keep the current choice[*], or type selection number: 3
Alternatively, if you have single version installed on the system it will look like below
There is only one alternative in link group php (providing /usr/bin/php): /usr/bin/php7.4
Nothing to configure.
How to Uninstall PHP
To completely uninstall PHP from your system, you can follow these steps
sudo apt remove php8.2
Also, uninstall all the modules of the version by following commnad.
sudo apt remove php8.2*
Conclusion
In this article, you have learnt how to install PHP on Ubuntu 22.04. By using Ondrej PPA we were able to installed any PHP versions as required. Also, you have learnt to switch PHP versions.