Learn the steps to install the latest version of PHP language on Ubuntu 22.04 LTS Focal Fossa using the command terminal.
PHP is the most common server-side programming language in the web world. Server-side means that the language can only perform actions on the server: For example, it can establish connections to a database or delete files on the server.
You can easily convert an HTML file into PHP. You simply have to save the HTML file under .php. You can upload this PHP file to your server and call it up – without any actual PHP code. The output is unchanged.
Steps to install Latest PHP on Ubuntu 22.04 LTS
1. Add Ondrej repository
Well, we can install PHP using the default system repository without adding any third-party package source repo. However, the default repository doesn’t offer the latest version of PHP. Therefore, for the latest and even old versions, we can use the Ondrej repository. Follow the given command to do that.
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
Run system update:
sudo apt update
2. Install Required PHP version
Once the repository is on your system, we can install any latest or previous version of PHP. According to your requirement, you can go for the one.
Note: While performing this tutorial, the latest version was 8.1.
• Install PHP 8.1 on Ubuntu 20.04
The command will be the same for installing any version i.e using the APT package manager, however, the version of the PHP will be different.
sudo apt install php8.1
• For PHP 8.0
Those who want PHP 8.0 instead of 8.1 can go for this command.
sudo apt install php8.0
• To get PHP 7.4
Well, many web applications are not compatible with PHP 8.x, and still required a 7.x version of PHP. If you are using any of such applications, can go for the given command:
sudo apt install php7.4
• Install PHP 5.6 on Ubuntu 22.04 LTS
We don’t recommend installing the 5.6 version because it is not supported by its developers anymore. Therefore, no security updates. Nevertheless, if there is any web application that requires this version, you can go for it:
sudo apt install php5.6
3. To Check Version
Once the installation is completed, we can check the version of PHP that has been configured by the APT package manager on our system, using:
4. Install PHP Extensions
Few common extensions of PHP will automatically get installed on your system. Whereas many web applications required some specific one, to install PHP modules of your choice, we can use the following commands.
Check the available modules:
sudo apt-cache search php8.0*
In the same way, we can search the modules for other versions as well:
To install any module using the apt package manager:
sudo apt install module-name
sudo apt install php8.0-mysql php8.0-json php8.0-cgi
5. Change Default PHP version
If you have installed multiple versions of PHP on your system and want to change the default version system-wide, then we can do that as well.
sudo update-alternatives --config php
Go through the “Selection” number for the available PHP version and then use the same to set as default.
6. Uninstallation or Remove
Due to some reason, if you don’t want to use PHP on your system then removing the same is also very easy. Simply use the APT package manager with –remove option.
sudo apt remove php8.0
In this way, we can install any available version of PHP on our Ubuntu 20.04 Focal Fossa Linux operating system.