PHP is a free, open source and one of the most commonly used server-side programming languages. It is used for the development of dynamic and responsive websites. It is cross-platform and can be installed on many platforms such as Linux, Windows, Mac OS and Unix. With PHP, you can create a website, a blog, a customer relationship management system, a customer relationship management system and much more.
In this article, we will show you how to install multiple PHP versions on Ubuntu 22.04.
Requirements
- A server running Ubuntu 22.04.
- A root password is set up on the server.
First steps
Before you start, you should update and upgrade all system packages to the latest version. You can update all packages by running the following command:
apt update apt upgrade
Once all packages are updated, you can proceed to the next step.
Add PHP repository
At the time of writing this tutorial, the latest PHP version is PHP 8.1. Ubuntu 22.04 ships with this PHP version in its default repository. To install multiple PHP versions, you need to install Ondrej’s PHP repository on your system.
First install all required dependencies with the following command:
apt install software-properties-common curl gnupg2 wget -y
Once all required dependencies are installed, add the PHP repository with the following command:
add-apt-repository ppa:ondrej/php
Once the repository has been added, refresh the repository cache with the following command:
apt update
Install PHP on Ubuntu 22.04
In this section, we will show you how to install multiple versions of PHP on Ubuntu 22.04.
Install PHP 7.4
To install PHP 7.4, run the following command:
apt-get install php7.4 php7.4-fpm -y
Next, run the following command to install other PHP extensions:
apt-get install php7.4-cli php7.4-mbstring php7.4-xml php7.4-gd php7.4-mysql php7.4-curl
Install PHP 7.3
To install PHP 7.3, run the following command:
apt-get install php7.3 php7.3-fpm
Next, run the following command to install other PHP extensions:
apt-get install php7.3-cli php7.3-mbstring php7.3-xml php7.3-gd php7.3-mysql php7.3-curl
Install PHP 7.2
To install PHP 7.2, execute the following command:
apt-get install php7.2 php7.2-fpm
Then run the following command to install other PHP extensions:
apt-get install php7.2-cli php7.2-mbstring php7.2-xml php7.2-gd php7.2-mysql php7.2-curl
Install PHP 8.0
To install PHP 8.0, execute the following command:
apt-get install php8.0 php8.0-fpm
Then execute the following command to install other PHP extensions:
apt-get install php8.0-cli php8.0-mbstring php8.0-xml php8.0-gd php8.0-mysql php8.0-curl
Now check the active version of PHP with the following command:
php -v
You should see the PHP version in the following output:
PHP 8.0.21 (cli) (built: Jul 13 2022 08:26:57) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.21, Copyright (c) Zend Technologies with Zend OPcache v8.0.21, Copyright (c), by Zend Technologies
Changing the PHP default version
If you have multiple versions of PHP installed on your system. You can easily switch between them using the update-alternatives command:
update-alternatives --config php
You will be prompted to select the PHP versions:
There are 4 choices for the alternative php (providing /usr/bin/php). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/php8.0 80 auto mode 1 /usr/bin/php7.2 72 manual mode 2 /usr/bin/php7.3 73 manual mode 3 /usr/bin/php7.4 74 manual mode 4 /usr/bin/php8.0 80 manual mode Press to keep the current choice[*], or type selection number: 3
Enter the desired PHP version number from the output above and press Enter to set this PHP version as the default version.
update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in manual mode
You can now check the active PHP version with the following command:
php -v
You should see the following output:
PHP 7.4.30 (cli) (built: Jun 27 2022 08:21:19) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies
You can also change the default PHP versions directly with the following command:
update-alternatives --set php /usr/bin/php7.2
You will get the following output:
update-alternatives: using /usr/bin/php7.2 to provide /usr/bin/php (php) in manual mode
To check the new active version, run the following command:
php -v
You should see the following output:
PHP 7.2.34-32 ubuntu22.04.1 deb.sury.org 1 (cli) (built: Jun 27 2022 08:18:11) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.34-32 ubuntu22.04.1 deb.sury.org 1, Copyright (c) 1999-2018, by Zend Technologies
Remove PHP from your system
If you want to remove a specific PHP version from your system, run the following command followed by PHP version:
apt-get remove php7.2
To remove other PHP extensions of the specific PHP versions, run the following command:
apt-get remove php7.2-*
Conclusion
In this post, we have explained how to install different PHP versions on Ubuntu 22.04. I hope you now have enough knowledge to install and manage multiple PHP versions.