Ubuntu 18.04 - Following packages have unmet dependencies: php7.2-fpm: ... How to solve this error?

I have php version 7.1 in my Ubuntu 18.04.

PHP 7.1.33-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Oct 26 2019 19:28:18) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.33-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

I would like to install php 7.2 on it and keep both versions. Following this post I've got the follow outputs:

apt-cache pkgnames | grep php7.2

enter image description here

sudo apt-get install php -y

The following packages have unmet dependencies:
 php : Depends: php7.2 but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

I have found similar errors and possible solutions in the internet. Most of them was about the content in the /etc/apt/sources.list.

I just added the follow (uncommented): sudo gedit /etc/apt/sources.list

#deb http://archive.ubuntu.com/ubuntu bionic-security multiverse
#deb http://archive.ubuntu.com/ubuntu bionic-security universe
#deb http://archive.ubuntu.com/ubuntu bionic-updates multiverse
#deb http://archive.ubuntu.com/ubuntu bionic-updates universe

#deb http://archive.ubuntu.com/ubuntu bionic main multiverse restricted universe
#deb http://archive.ubuntu.com/ubuntu bionic-security main multiverse restricted universe
#deb http://archive.ubuntu.com/ubuntu bionic-updates main multiverse restricted universe

deb http://archive.ubuntu.com/ubuntu bionic main restricted universe
deb http://archive.ubuntu.com/ubuntu bionic-updates main restricted universe 
deb http://security.ubuntu.com/ubuntu bionic-security main restricted universe multiverse 
deb http://archive.canonical.com/ubuntu bionic partner

I still have the same error. And this is what I have inside the ect/php: enter image description here

And this is what I see in the apt list

ila@ila:~$ apt list php
Listing... Done
php/bionic,bionic 1:7.2+60ubuntu1 all
ila@ila:~$ rmadison
E: need at least one package name as an argument.
ila@ila:~$ rmadison php
 php | 1:7.0+35ubuntu6   | xenial         | all
 php | 1:7.0+35ubuntu6.1 | xenial-updates | all
 php | 1:7.2+60ubuntu1   | bionic         | all
 php | 2:7.2+69ubuntu1   | disco          | all
 php | 2:7.3+69ubuntu2   | eoan           | all
 php | 2:7.3+69ubuntu2   | focal          | all
ila@ila:~$ apt list php
Listing... Done
php/bionic,bionic 1:7.2+60ubuntu1 all
ila@ila:~$ sudo apt install php
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help resolve the situation:

The following packages have unmet dependencies:
 php : Depends: php7.2 but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

After remove the 7.2 and 7.3 directories the error is still there. how can I solve it?


I have found an answer:

Following this post, I just did:

  • sudo add-apt-repository ppa:ondrej/php
  • sudo apt update
  • sudo apt install apt-transport-https lsb-release ca-certificates
  • sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
  • sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
  • sudo apt update
  • dpkg -l | grep php | tee packages.txt (check the content)
  • sudo apt install php7.2 php7.2-common php7.2-cli php7.2-fpm (install the basic)

Then

php -v
PHP 7.2.24-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Oct 24 2019 18:29:11) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

And I still have my projects working with php7.1 and the new 7.2.

ila@ila:/etc/php$ ll
total 24
drwxr-xr-x   4 root root  4096 Nov 21 12:24 ./
drwxr-xr-x 161 root root 12288 Nov 21 12:14 ../
drwxr-xr-x   5 root root  4096 Jul 26  2018 7.1/
drwxr-xr-x   5 root root  4096 Nov 21 12:24 7.2/
ila@ila:/etc/php$ tree 7.2/
7.2/
├── cli
│   ├── conf.d
│   │   ├── 10-opcache.ini -> /etc/php/7.2/mods-available/opcache.ini
│   │   ├── 20-json.ini -> /etc/php/7.2/mods-available/json.ini
│   │   └── 20-readline.ini -> /etc/php/7.2/mods-available/readline.ini
│   └── php.ini
├── fpm
│   ├── conf.d
│   │   ├── 10-opcache.ini -> /etc/php/7.2/mods-available/opcache.ini
│   │   ├── 20-json.ini -> /etc/php/7.2/mods-available/json.ini
│   │   └── 20-readline.ini -> /etc/php/7.2/mods-available/readline.ini
│   ├── php-fpm.conf
│   ├── php.ini
│   └── pool.d
│       └── www.conf
└── mods-available
    ├── json.ini
    ├── opcache.ini
    └── readline.ini

6 directories, 13 files

Or install some available modules

sudo apt install -y php7.2-bz2 php7.2-common php7.2-cgi php7.2-cli php7.2-dba php7.2-dev libphp7.2-embed php7.2-bcmath php7.2-fpm php7.2-gmp php7.2-mysql php7.2-tidy php7.2 php7.2-sqlite3 php7.2-json php7.2-opcache php7.2-sybase php7.2-curl php7.2-ldap php7.2-phpdbg php7.2-imap php7.2-xml php7.2-xsl php7.2-intl php7.2-zip php7.2-odbc php7.2-mbstring php7.2-readline php7.2-gd php7.2-interbase php7.2-snmp php7.2-xmlrpc php7.2-soap php7.2-pspell php7.2-pgsql php7.2-enchant php7.2-recode