How to Upgrade or Downgrade PHP on Ubuntu (to any version)

Let’s Check the PHP version first:

php -v

In order to be able to useΒ add-apt-repositoryΒ command install required 3rd party repository support:

sudo apt install software-properties-common

First add the repo and update apt.

sudo add-apt-repository ppa:ondrej/php

Then you can install any PHP version.

Let’s say, we will install PHP 7.3

sudo apt install php7.3

Also let’s install PHP 8.0

sudo apt install php8.0

Now we will get the location of where PHP is installed with the following command:-

sudo which php

Ex:- /usr/bin/php is the output

Now let’s check the PHP version-

php -v

Let’s take a look at

ls -la <path_where_PHP_is_located>

Ex:-

ls -la /usr/bin/php

Let’s see all the installed versions:

ls /usr/bin/php*

So in our case- ls /usr/bin/php*

STEP 7:Β Set PHP 7.x or PHP 8.x as default PHP version

Default Ubuntu PHP 7.0.x binary is still handling all PHP requests, we need to change it:

for PHP 7.3

sudo update-alternativesΒ --setΒ phpΒ /usr/bin/php7.3

for PHP 7.4

sudo update-alternativesΒ --setΒ phpΒ /usr/bin/php7.4

for PHP 8.0

sudo update-alternativesΒ --setΒ phpΒ /usr/bin/php8.0

Set PHP 7.x or PHP 8.x as default PHP version

sudo update-alternativesΒ --config php

You will all the PHP versions available or installed. And also you will get the current version which will have *

Enter the number selecting the PHP version.

Once you enter the number, that version will be selected as default or currently running on your machine.

You can directly do it as shown below:-

for PHP 7.3

sudo update-alternativesΒ --setΒ phpΒ /usr/bin/php7.3

for PHP 7.4

sudo update-alternativesΒ --setΒ phpΒ /usr/bin/php7.4

for PHP 8.0

sudo update-alternativesΒ --setΒ phpΒ /usr/bin/php8.0

Now check the PHP version by

php -v

I hope this helped you, do comment below giving feedback or if you have any queries.

Happy Learning !!

Thank You !!