Unable to change PHP version in Ubuntu 18.04 terminal

5.5k Views Asked by At

This question has been asked multiple times but non of them worked for me. I need to change my terminal's version to 7.4.

When I do phpinfo() it gives 7.4.10 but in terminal php -v it always show 5.6.0

I've done:

$ sudo a2dismod php5.6
$ sudo a2enmod php7.4
$ sudo service apache2 restart

set PATH="/usr/bin/php7.4/bin:$PATH"

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

sudo update-alternatives --config php

This show below result:

PHP version

2

There are 2 best solutions below

0
On

Recent versions of PHP aren't available for install, because the LTS (Long Term Support) versions of that version 18 of Ubuntu, and other derivatives, are now expired.

Ubuntu 18.04 LTS had normal LTS support for five years until May 2023 and has paid ESM support available from Canonical for an additional five years until April 2028. https://en.wikipedia.org/wiki/Ubuntu_version_history#Ubuntu_18.04_LTS_(Bionic_Beaver)

sudo add-apt-repository ppa:ondrej/php You are about to add the following PPA: Co-installable PHP versions: PHP 5.6, PHP 7.x, PHP 8.x and most requested extensions are included. Only Supported Versions of PHP (http://php.net/supported-versions.php) for Supported Ubuntu Releases (https://wiki.ubuntu.com/Releases) are provided. Don't ask for end-of-life PHP versions or Ubuntu release, they won't be provided.


Upgrading the system won't work either, up to a point, dependencies are missing. Upgrading from apt won't work in this situation, neither update-alternatives as the remote PPA will block further installation on expired LTS systems.

There is no proper solutions beside a fresh install of a supported recent LTS version.


However, to force install the last PHP versions, including the last development versions, we have to compile from sources.

This is really not recommended in a production server.

Get the last source archive from the PHP website:

https://www.php.net/downloads.php (See readme.md for tweaks)

./buildconf
./configure
make -j4
sudo make install
php -v
# PHP 8.3.0 (cli) (built: Dec 25 2023 00:00:00) (NTS DEBUG)
0
On

You can use the following command in the command line:

sudo update-alternatives --set php /usr/bin/php7.1

Simply provide the desired PHP version in the above line of command.