PHP is a popular server-side scripting language that is used to create dynamic web pages and web applications. It is open-source and is widely used for developing web applications, content management systems, and e-commerce websites. In this article, we will discuss how to install PHP 8.2, 7.4, and 5.6 on Ubuntu 18.04.
Ubuntu 18.04 comes with PHP 7.2 installed by default. However, if you need to use an older or a newer version of PHP, you can follow the instructions below.
Note: Before installing a new version of PHP, make sure to backup any files that may be affected by the installation process.
Step 1: Add the Ondřej Surý PPA
The Ondřej Surý PPA is a Personal Package Archive that contains the latest versions of PHP. To add this PPA to your system, run the following command in your terminal:
sudo add-apt-repository ppa:ondrej/php
After adding the PPA, update your package list with the following command:
sudo apt-get update
Step 2: Installing PHP
Once you have added the PPA to your Ubuntu system, You can install required PHP version on your system. Even you can install multiple PHP versions at the same time.
- Installing PHP 8.2:
sudo apt-get install -y php8.2
- Installing PHP 7.4:
sudo apt-get install -y php7.4
- Installing PHP 5.6:
sudo apt-get install -y php5.6
Step 3: Verify PHP Version
After the installation process is complete, you can check the PHP version by running the following command:
PHP 8.2.3 (cli) (built: Feb 14 2023 16:58:12) (NTS) Copyright (c) The PHP Group Zend Engine v4.2.3, Copyright (c) Zend Technologies with Zend OPcache v8.2.3, Copyright (c), by Zend Technologies
Step 4: Change Default PHP Version
In Ubuntu, multiple versions of PHP can be installed and used side-by-side. However, only one version can be set as the default at a time. If you have multiple versions of PHP installed and you want to change the default PHP version, follow the steps below:
- Check the available PHP versions by running the following command:
This command will list all the available PHP versions installed on your system.
- To change the default PHP version, you can use the update-alternatives command. For example, if you want to set PHP 7.4 as the default version, run the following command:
sudo update-alternatives --set php /usr/bin/php7.4
This command sets the /usr/bin/php7.4 binary as the default php command.
- If you have installed php-dev package, then you also need to set phpize and php-config as default.
sudo update-alternatives --set phpize /usr/bin/phpize7.4
sudo update-alternatives --set php-config /usr/bin/php-config7.4
- To verify the default PHP version, run the following command:
This command should display the version of PHP that you just set as the default.
By default, only one version of PHP can be set as the default, but multiple versions can be installed and used side-by-side. Remember to verify the default PHP version after making any changes.
In this article, we have discussed how to install PHP 8.2, 7.4, and 5.6 on Ubuntu 18.04. You can choose the version of PHP that you need for your project, depending on its requirements. Keep in mind that newer versions of PHP may not be compatible with older applications, so it’s always a good idea to check compatibility before upgrading. Additionally, remember to backup any files that may be affected by the installation process.