You can do this with update-alternatives. If you would like to do this interactively, you can do this:
sudo update-alternatives --config php
If you like to specifically choose the PHP version (via an alias or whatnot), you can do this:
sudo update-alternatives --set php /usr/bin/php7.4
Of course, be sure to change php7.4 to the actual version you want to switch between.
You can do this with update-alternatives. If you would like to do this interactively, you can do this:
sudo update-alternatives --config php
If you like to specifically choose the PHP version (via an alias or whatnot), you can do this:
sudo update-alternatives --set php /usr/bin/php7.4
Of course, be sure to change php7.4 to the actual version you want to switch between.
Please use following command which will ask you to select a number against your required PHP version
sudo update-alternatives --config php
Then select your choice and press enter.
Interactive switching mode
sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
Manual Switching
From PHP 5.6 => PHP 7.1
Default PHP 5.6 is set on your system and you need to switch to PHP 7.1.
Apache:
$ sudo a2dismod php5.6
$ sudo a2enmod php7.1
$ sudo service apache2 restart
Command Line:
$ sudo update-alternatives --set php /usr/bin/php7.1
$ sudo update-alternatives --set phar /usr/bin/phar7.1
$ sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.1
From PHP 7.1 => PHP 5.6
Default PHP 7.1 is set on your system and you need to switch to PHP 5.6.
Apache:
$ sudo a2dismod php7.1
$ sudo a2enmod php5.6
$ sudo service apache2 restart
Command Line:
$ sudo update-alternatives --set php /usr/bin/php5.6
Source
$ sudo update-alternatives --config php
should work for all ubuntu versions after 16.04 (18.04, 20.04 and 22.04)
This is what you should see as a response
There are 4 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php7.2 72 auto mode
1 /usr/bin/php5.6 56 manual mode
2 /usr/bin/php7.0 70 manual mode
3 /usr/bin/php7.1 71 manual mode
4 /usr/bin/php7.2 72 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Choose the appropriate version
Upgrading PHP version
Change PHP version (use multiple versions at once)
apache httpd - Switch PHP versions in the console, Ubuntu - Unix & Linux Stack Exchange
How to make new PHP version a system default?
Videos
apt-get update updates only the package lists. To actually upgrade the packages you have to run:
sudo apt-get upgrade
or
sudo apt-get dist-upgrade
More info here.
If you ran the above commands but the packages still were not upgraded, probably you didn't add Ondřej Surý's PHP PPA to your system.
Run the following to add the PPA:
For PHP 5.5, PHP 5.6 and PHP 7.0:
sudo apt-add-repository ppa:ondrej/phpFor PHP 5.4 (Deprecated, upgrade at least to PHP 5.5):
sudo apt-add-repository ppa:ondrej/php5-oldstableFor PHP 5.5 (Deprecated, use
ppa:ondrej/php):sudo apt-add-repository ppa:ondrej/php5For PHP 5.6 (Deprecated, use
ppa:ondrej/php):sudo apt-add-repository ppa:ondrej/php5-5.6
Then update the package lists and perform the upgrade:
sudo apt-get update
For PHP 5.5:
sudo apt-get install php5.5
For PHP 5.6:
sudo apt-get install php5.6
For PHP 7:
sudo apt-get install php7.0
Release upgrade
If you want to install the latest PHP (for example PHP7) but you want to stick to the release's packages intead of hacking the sources around; and you want to do a full-release upgrade (for example you are in Ubuntu 14.04 -which does not have php7- and you want to go to Ubuntu 16.04 -which does- then you can do:
do-release-upgrade
Now you should have the latest PHP version.
For newest updating process use the following command
sudo LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
Then run the following command in terminal
sudo apt-get update
This is worked well when installing php 5.5,5.6 and 7.0
If there some errors this command will resolve the errors
sudo dpkg --configure -a
Update 2020-10-14: For versions upto 7.*
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Then
sudo apt install php7.2
Here also you can use this for php 7.0,7.1,7.3 to 7.4
After updating process you can use below command to install extensions
sudo apt update
sudo apt install php-xml
or you can use like below
sudo apt install php7.2 php-xml
Update 2021-01-31: For versions upto 8.*
sudo apt-add-repository ppa:ondrej/php
sudo apt update
Then
sudo apt install -y php8.0 php8.0-cli php8.0-common
It's hard getting the exact PHP 8.1.22 version. However, if you are OK with something close to it, 8.1.24 is the current version at ppa:ondrej/php.
Remove your current PHP 8.1:
sudo apt remove php8.1Install ondrej's repo:
sudo add-apt-repository ppa:ondrej/php sudo apt updateInstall PHP 8.1:
sudo apt install php8.1Disable the default PHP 8.2 in Ubuntu 22.04:
sudo a2dismod php8.2Enable PHP 8.1:
sudo a2enmod php8.1Restart the Apache:
sudo service apache2 restart
My answer would be - uninstall the official version and find some alternative package. https://launchpad.net/~ondrej/+archive/ubuntu/php?field.series_filter=jammy
This seems really problematic that the PHP version isn't raised to the latest bugfix version. On Ubuntu 22.04 the version 8.1.2 is included but the latest version is 8.1.24 - 1 and 3/4 of a year old. I found this out while researching why my installation is consuming so much memory: https://github.com/php/php-src/issues/8646 So the bug is probably already fixed.
So, I have a VPS, Im running several web applications (built on PHP ofc).
I want to upgrade PHP for some of the web applications.
Today Im running this:
Web application A - PHP 7.3
Web application B - PHP 7.3
Web application C - PHP 7.3
Web application D - PHP 7.3
I would like to do this:
Web application A - PHP 7.3
Web application B - PHP 8.1
Web application C - PHP 7.3
Web application D - PHP 8.1
Can I simply change the PHP version here (in cPanel): https://ibb.co/9tpQdTr
Since I don't know anything about web servers, PHP Apache and so on, I don't really know what I'm doing.
Below is a description of what I did. I hope this information can help someone else:
I installed PHP 7.1 along side PHP 7.2. I also installed most of the needed extensions for PHP 7.1. I then did a2dismod php7.2 and a2enmod php7.1 so that I could switch over to PHP 7.1 while keeping 7.2 still installed on the server. Most of my sites work after making the switch. The only site that doesn't seem to be working is a Joomla site. The full list of commands I ran are below:
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php7.1
sudo apt-get install php7.1-cli php7.1-common php7.1-json php7.1-opcache php7.1-mysql php7.1-mbstring php7.1-mcrypt php7.1-zip php7.1-fpm
sudo a2dismod php7.2
sudo a2enmod php7.1
sudo service apache2 restart
for display enabled php version in terminal by php -v. we need to set update cli version of php.
use below command to update php cli version
// change terminal (cli) version (7.2 to 7.1)
sudo update-alternatives --set php /usr/bin/php7.1