54

Currently running PHP 5.4 on CentOS 6.5.

I installed the webtatic php55w package then installed PEAR+PECL without issue along with redis and mongo through PECL.

Shortly after, I realized 5.5 is not compatible with the framework I was working with so I yum erased php55w and installed php54w in it's place.

Now the pecl command doesn't work at all. It just produces this really long string of errors every time I issue any pecl command (abbreviated...most repeated dozens of times):

Warning: Invalid argument supplied for foreach() in Command.php on line 259

Warning: Invalid argument supplied for foreach() in /usr/share/pear/PEAR/Command.php on line 259

...etc etc etc...

Notice: Undefined index: honorsbaseinstall in Role.php on line 180

Notice: Undefined index: honorsbaseinstall in Role.php on line 180

...etc etc etc...

Notice: Undefined index: installable in Role.php on line 145

Notice: Undefined index: installable in Role.php on line 145

...etc etc etc...

Notice: Undefined index: phpfile in Role.php on line 212

Notice: Undefined index: phpfile in Role.php on line 212

...etc etc etc...

Notice: Undefined index: config_vars in Role.php on line 49

Notice: Undefined index: config_vars in Role.php on line 49

...etc etc etc...

Warning: Invalid argument supplied for foreach() in PEAR/Command.php on line 259

Warning: Invalid argument supplied for foreach() in /usr/share/pear/PEAR/Command.php on line 259

...etc etc etc...

XML Extension not found

How can I fix this?

eComEvo
  • 971
  • 3
  • 17
  • 31

7 Answers7

98

I came across this error after updating my PHP installation to 5.5.14, on RedHat EL v6. I had installed PHP via the Yum package manager, and then needed to re-install some of the PHP extensions I was using. In searching for tips on how to solve this issue, I came across this question, and now that I have discovered a working solution I wanted to share my findings here. Other suggestions I had found online which included erasing and re-installing PECL/PEAR and even my PHP installation did not solve this issue. Finally after some further research and reviewing the source code for PECL/PEAR I found the real cause. Hopefully what follows will be of help to others:

You may see this error when trying to run PECL if your PHP installation does not have XML enabled by default, but instead XML support is usually loaded into your PHP installation via a PHP extension module (this could occur if the ./configure --disable-xml flag was specified when building PHP from source, or if you installed PHP via various package managers where that build of PHP is configured to load XML via an extension module).

Notice how the last line of the error output from PECL is XML Extension not found – the reason this error is appearing is because when PECL tries to use its XMLParser.php class it fails because it cannot access the XML extension (it checks for the XML module using extension_loaded('xml') around line 259 of the XMLParser.php source), and because the XML module is unavailable, it cannot parse its configuration/settings files and outputs all of the other errors seen above.

The reason this issue occurs is due to the way that PECL operates. The PECL command itself is just a shell script, which first works out where PHP is installed on your system installation, and then calls PHP on the command line with a number of flags before providing the path to the main PECL PHP script file. The problem flag which the PECL shell script is using is the -n option, which tells PHP to ignore any php.ini files (and therefore PHP will not load any of the additional extensions your php.ini file specifies, including in this case XML).

One can see the impact of the -n flag by running the following two commands:

  • first try running php -m on the command line
  • then compare the output to php -n -m

You should not see the XML extension listed when you run the second command because the -n flag told PHP not to parse our php.ini file(s).

If you run vi `which pecl` on the command line you should see the contents of the PECL command (as noted above, its just a shell script), and if you inspect the last line, you will see something like this:

exec $PHP -C -n -q $INCARG -d date.timezone=UTC -d output_buffering=1 -d variables_order=EGPCS -d safe_mode=0 -d register_argc_argv="On" $INCDIR/peclcmd.php "$@"

You should see the -n flag listed between the -C and -q flags. If you edit the PECL shell script, omitting the -n flag you should now be able to run PECL again without issues.

Alternatively, you can recompile PHP from source making sure that the XML module is compiled into the PHP binary instead of being loaded from a PHP extension module at run-time. Obviously editing the PECL shell script to remove the -n flag will only fix the issue until PECL/PEAR gets re-installed, hopefully however the maintainers of PECL/PEAR can update their repo with this fix. Ensuring PHP is built with XML support compiled in, is however a long-term fix to the solution, but may not be ideal for everyone's circumstances.

Just for completeness, if you run vi `which pear` you will see a very similar shell script to the one that PECL uses, however the -n flag is missing from the command which calls PHP and as such the PEAR command is not subject to these same issues.

bluebinary
  • 1,096
  • 8
  • 5
  • 5
    Epic answer, editing the pecl script worked for me. – Alex Ross Jun 22 '15 at 02:44
  • 30
    You sir ... are a saint. ... ... literally. I'm going to write a script that scans the obituaries of every newspaper, and as soon as you pass away, I'm going to tell the Vatican to start the beatification process, using this answer as an example of your deeds on earth. Awesome answer :) – riwalk Oct 23 '15 at 13:37
  • 5
    Thanks. TLDR; edit the last line of `/usr/bin/pecl` to not use the -n parameter. – dtbarne Jan 20 '16 at 20:30
  • 7
    Run `sed -i "$ s|\-n||g" /usr/bin/pecl` – AVProgrammer Jul 19 '16 at 15:45
  • 8
    I don't have `-n` in the last command of `pecl` and the long list of errors remain unfortunately – whiteletters in blankpapers Jul 24 '16 at 18:29
  • Hmm, this issue with Pecl is still an issue even in Alpine (edge). I wonder if it is something that just won't be fixed? Does anyone know if it has been logged as a bug with the PHP team? – halfer Jun 29 '17 at 10:44
  • Thank you for the beautiful answer!! Kudos! Just one thing though: if you are having this issue in WHM / cPanel, you have to modify the version appropriate script! For example, if you are using php 7.0, the file to modify is this one: /opt/cpanel/ea-php70/root/usr/bin/pecl So the command would look like this: **nano /opt/cpanel/ea-php70/root/usr/bin/pecl** – Peter Sep 14 '17 at 09:37
  • 5
    well I am using `php7.2` and for me, I just had to use the command `sudo apt-get install php7.2-xml` and that is it no editing of the script or any other thing – Muhammad Omer Aslam Sep 06 '18 at 14:21
  • tl;dr - On the command-line, run `nano \`which pecl\`` and remove `-n` from the last line. – rinogo Apr 17 '19 at 18:16
  • installing the php 8 xml extension did the work for me. thanks for the pointers early in your answer! – hakre Nov 18 '20 at 21:45
  • Yeah, as beautiful as this answer is, it seems that the pecl version from apt repositories is now patched. Make sure to check for the xml extension (`php -m`) on the version binary of the php you want then install `php[version]-xml` as said by @MuhammadOmerAslam – Scott Anderson Dec 29 '20 at 15:37
34

I just have faced this problem on ubuntu when i called for PECL command. The only thing that helped me is to install php-xml package. First check if you have the XML module already installed with

php -m

If you don't find it then you have to

sudo apt-get install php-pear

it will automatically install php-xml package. or you can just install xml like this (depending on the version of php you have)

sudo apt-get install php-xml php7.0-xml

If you find xml then you have remove it and reinstall it

sudo apt-get purge php*-xml
sudo apt-get autoremove php*-xml
sudo apt-get install php-xml php7.0-xml

If you have RPM as package manager you can use yum install php-xml and yum remove php-xml

Kalle Richter
  • 259
  • 6
  • 17
Taha EL BOUFFI
  • 441
  • 4
  • 4
8

Im using php5.6.

Many answers recommend to install php-xml, but its not working for me, when I type specific version like

sudo apt-get install php5.6-xml

and everything works, maybe it will help others.

4

you have to install php-xml package in order to fix "XML Extension not found" problem

kavehmb
  • 141
  • 3
2

Remove any PEAR RPMs completely, then rm -rf /usr/share/pear/ then install pear again and all your modules.

Florin Asăvoaie
  • 6,932
  • 22
  • 35
  • Followed that up with `yum erase php-pear` and now all is well! Thanks! :) – eComEvo Apr 18 '14 at 15:19
  • 1
    Please note using the php54w version of php pear when reinstalling (instead of the stock php-pear package). So in short 1.`yum erase php-pear` 2. `rm -rf /usr/share/pear/` 3. `yum install php54w-pear` – Attila Fulop Jun 22 '14 at 10:02
2

The following steps work for me.

1 step:

yum erase php-pear

2 step:

# rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

3 step:

 yum install --enablerepo=remi --enablerepo=remi-php56 php-pear
Gawain Lau
  • 121
  • 1
0

PHP7/Debian people (similar/same reason):

The reason for that, is, how mention above, the missing XML extension. Must of us will use dotdeb, and the have removed xml from being build-in to being a separate package:

source: https://www.dotdeb.org/2016/06/24/php-7-0-8-for-jessie/

Please also note that bcmatch, dba, mbstring, soap, xml and zip now have their separate dedicated packages.

This means, that

php -n

will no longer include xml, which PEAR package parser depends on (XML parser). Source pear.php.net/package/PEAR/download

PHP Extension: xml

Since it is critical for pecl to not use php.ini of the system, to guarantee functionionality in all cases ( even if the disfunctionality is the reason you run pecl .. ) it will not change using -n .. rather the dotdeb guys need to finally stop repackaging, restructuring and shifting arround packages without even slightly testing it.

There is also an issue with the shipped PEAR package, which needs to be upgraded with...not allowed to post anything else due to the current reputation

Eugen Mayer
  • 277
  • 1
  • 4
  • 15