Search This Blog

Monday, October 10

How to Install Multiple PHP(7.3 and 8) Versions on Ubuntu > 20

 Step 1: System Update

First, log in to Ubuntu via console. Then update the Apt cache and upgrade the current packages of the system using the following command:

sudo apt-get update

sudo apt-get upgrade 

When prompted, press y to confirm the installation.

Step 2: Installing Multiple PHP Versions on Ubuntu > 20

The easiest way to install multiple versions of PHP is by using the PPA from OndÅ™ej Surý, who is a Debian developer. To add this PPA, run the following commands in the terminal. The software-properties-common package is needed if you want to install software from PPA. It’s installed automatically on the Ubuntu desktop but might miss on your Ubuntu server.

sudo apt install software-properties-common

sudo add-apt-repository ppa:ondrej/php

sudo apt update

The SURY repository contains PHP 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, 7.0 & PHP 5.6. As the latest stable version of PHP is 8.0, but many websites still required PHP 7. You can install any of the required PHP versions on your system.

Install PHP 8.1:

Now you can install PHP8.1 on Ubuntu by executing the following command:

sudo apt-get install php8.1 php8.1-fpm

And install some common PHP8.1 extensions.

sudo apt-get install php8.1-mysql php8.1-mbstring php8.1-xml php8.1-gd php8.1-curl

When prompted, press y to confirm the installation.

You can view all available PHP8.1 extensions by typing in sudo apt-get install php8.1 and pressing Tab key twice.

php

Install PHP 7.3:
Now you can install PHP7.3 on Ubuntu by executing the following command:

sudo apt-get install php7.3 php7.3-fpm
And install some common PHP7.3 extensions.

sudo apt-get install php7.3-mysql php7.3-mbstring php7.3-xml php7.3-gd php7.3-curl
When prompted, press y to confirm the installation. 

Step 3: Check the Active PHP Version
Now after installation, verify that the correct version of PHP is installed by checking the version number by the below-mentioned command:

php -v

# Output
PHP 8.1.8 (cli) (built: Jul 11 2022 08:30:39) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.8, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.8, Copyright (c), by Zend Technologies

Step 4: Switch the Default PHP Version for CLI
If you have multiple versions of PHP installed on Ubuntu 22.04, you can choose to make one of the versions the default PHP version.

To list the available versions, run the following command:

sudo update-alternatives --config php

php version



To change the default version, enter the number that matches the appropriate version you want to make as the default and press ENTER.

For example, to make PHP 7.3 the default version, type 2 and press ENTER.

You can execute the command below to change the version straight away:

sudo update-alternatives --set php /usr/bin/php7.3
After, Check the active PHP version:

php -v

PHP 7.3.33-7+ubuntu20.04.1+deb.sury.org+2 (cli) (built: Sep 29 2022 22:22:53) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.33, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.33-7+ubuntu20.04.1+deb.sury.org+2, Copyright (c) 1999-2018, by Zend Technologies


That's it. Happy coding!

No comments:

Post a Comment