Install newest (or any) PHP on Ubuntu

I.e. shell command generator.


Before you do anything

If you already have some PHP installed, you might want to save a list of all the packages related to php, in case of any problems (here we save it to a /tmp/php-packages.txt file):

dpkg -l | grep php | tee /tmp/php-packages.txt

You should also backup everything, or at least php-related stuff, like /etc/php directory

Optional: you might also want to remove your current version with:

sudo apt purge 'php5*' Or: sudo apt purge 'php7*'

Depending on version you already have.

Installing version from official repo

If you simply want to install whatever version of PHP is available in the official repos then the following commands (plus the below additional libs) should be enough:

sudo apt update sudo apt install php

Installing any version, including the newest stable one

But, if you want to grab a different version (including the newest one), you need to first add an unofficial, but really well esablished and well maintained PPA by Ondřej Surý:

add-apt-repository ppa:ondrej/php apt update

Install command #1:


You may need additional modules depending on your applications. The most commonly used modules can be installed with the following

Install command #2:

Other things

Now you might want to open the the file you created in Before you do anything step with

less /tmp/php-packages.txt

And install them, if needed