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.
On Ubuntu LTS you need to add ondrej repository to install older versions:
sudo add-apt-repository ppa:ondrej/php
For this example i'll be installing php7.3
sudo apt install php7.3
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
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
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
Now if you try to use
php -v , it won't work because now it has been changed to
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
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
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
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
Thank you for making it up to here , please feel free to share your thoughts and problems