PHP: Installing,Switching between Multiple PHP versions on Linux

sirimhrzn

sirimhrzn

Posted on August 7, 2023

PHP: Installing,Switching between Multiple PHP versions on Linux

As a beginner working in production, you will be facing many problems that you were unaware of while you were doing personal projects, one of them being having to deal with older projects which used an older version of PHP but also work with latest PHP version at the same time. It is not just PHP but happens with every programming language so I decided to write this article for those who are starting out and have this problem.
The most easiest way would be running it on docker but that is not the topic for now , so let's begin.

This tutorial uses Ubuntu but it works on any other distro.

Installing multiple version

On Ubuntu LTS you need to add ondrej repository to install older versions:

sudo add-apt-repository ppa:ondrej/php
Enter fullscreen mode Exit fullscreen mode

For this example i'll be installing php7.3

sudo apt install php7.3
Enter fullscreen mode Exit fullscreen mode

If you don't specify PHP version then, the latest version of PHP will be installed from the Ubuntu default repository

sudo apt install php
Enter fullscreen mode Exit fullscreen mode

Now on terminal , if you check your PHP version, by default it will be from Ubuntu's default repository

php -v
PHP 8.2.8 (cli) (built: Jul  8 2023 07:10:21) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.8, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.8, Copyright (c), by Zend Technologies
Enter fullscreen mode Exit fullscreen mode

Now to use older PHP version we will create a symbolic link, so lets move the default PHP version into a folder with it's version name.

sudo mv /usr/bin/php /usr/bin/php8.2
Enter fullscreen mode Exit fullscreen mode

Now if you try to use php -v , it won't work because now it has been changed to php8.2

To use older version as PHP command, you will now create a symbolic link, which calls your desired PHP version when php is called.

sudo ln -s /usr/bin/php7.3 /usr/bin/php
Enter fullscreen mode Exit fullscreen mode

Now if you try php -v , the version of PHP which you specified when creating symbolic link should run.

php -v
PHP 7.3.33-11+ubuntu22.04.1+deb.sury.org+1 (cli) (built: Jun  8 2023 15:22:14) ( 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-11+ubuntu22.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
Enter fullscreen mode Exit fullscreen mode

Now to switch back to any other php version you have to unlink to in order to create another symbolic link,

sudo unlink /usr/bin/php
Enter fullscreen mode Exit fullscreen mode

And just repeat the linking and unlinking to switch and forth between multiple versions of PHP

sudo ln -s /usr/bin/php8.2 /usr/bin/php
Enter fullscreen mode Exit fullscreen mode

Thank you for making it up to here , please feel free to share your thoughts and problems

💖 💪 🙅 🚩
sirimhrzn
sirimhrzn

Posted on August 7, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related