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.
Answer from Eric Carvalho on askubuntu.comapt-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
Videos
I run my own site on a linux server (Ubuntu 18.04) on a dedicated server, my knowledge on linux and programming is pretty low, i managed to install the site and all a couple of years ago but now i need to update it to a newer version that requires php 8.2 and i currently have php 7.2.
My question is, is it hard to change versions and how risky it is to do so, since i don't know much i'm afraid to loose dbs or my website (I wil have backups just in case).
Thanks in advance.
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