PHP7:安装 ext-dom 问题

329

我在Ubuntu 16.04服务器上使用PHP7运行laravel 5.4,尝试安装cviebrock/eloquent-sluggable包时出现错误:

pish@let:/home/sherk/ftp/www$ sudo composer require cviebrock/eloquent-sluggable
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Using version ^4.2 for cviebrock/eloquent-sluggable
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - phpunit/php-code-coverage 4.0.7 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/php-code-coverage 4.0.7 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - Installation request for phpunit/php-code-coverage (installed at 4.0.7) -> satisfiable by phpunit/php-code-coverage[4.0.7].

  To enable extensions, verify that they are enabled in those .ini files:
    - /etc/php/7.0/cli/php.ini
    - /etc/php/7.0/cli/conf.d/10-mysqlnd.ini
    - /etc/php/7.0/cli/conf.d/10-opcache.ini
    - /etc/php/7.0/cli/conf.d/10-pdo.ini
    - /etc/php/7.0/cli/conf.d/20-calendar.ini
    - /etc/php/7.0/cli/conf.d/20-ctype.ini
    - /etc/php/7.0/cli/conf.d/20-exif.ini
    - /etc/php/7.0/cli/conf.d/20-fileinfo.ini
    - /etc/php/7.0/cli/conf.d/20-ftp.ini
    - /etc/php/7.0/cli/conf.d/20-gd.ini
    - /etc/php/7.0/cli/conf.d/20-gettext.ini
    - /etc/php/7.0/cli/conf.d/20-iconv.ini
    - /etc/php/7.0/cli/conf.d/20-json.ini
    - /etc/php/7.0/cli/conf.d/20-mbstring.ini
    - /etc/php/7.0/cli/conf.d/20-mcrypt.ini
    - /etc/php/7.0/cli/conf.d/20-mysqli.ini
    - /etc/php/7.0/cli/conf.d/20-pdo_mysql.ini
    - /etc/php/7.0/cli/conf.d/20-phar.ini
    - /etc/php/7.0/cli/conf.d/20-posix.ini
    - /etc/php/7.0/cli/conf.d/20-readline.ini
    - /etc/php/7.0/cli/conf.d/20-shmop.ini
    - /etc/php/7.0/cli/conf.d/20-sockets.ini
    - /etc/php/7.0/cli/conf.d/20-sysvmsg.ini
    - /etc/php/7.0/cli/conf.d/20-sysvsem.ini
    - /etc/php/7.0/cli/conf.d/20-sysvshm.ini
    - /etc/php/7.0/cli/conf.d/20-tokenizer.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

Installation failed, reverting ./composer.json to its original content.

我在本地版本的应用程序上安装这个软件包没有任何问题。


最佳方式是 "sudo apt-get install php7.4-mysql php7.4-mbstring php7.4-xml"。 - hassan zarghami
4个回答

709
首先,请仔细阅读警告!它说不要以root身份运行composer! 其次,您可能在本地使用的是Xammp,其中默认具有所需的PHP库。
但在服务器上,您缺少ext-dom。 php-xml具有您需要的所有相关软件包。因此,您可以通过运行以下命令来简单地安装它:
sudo apt-get update
sudo apt install php-xml

很可能您还缺少mbstring。如果您遇到错误,请运行以下命令安装该包:

sudo apt-get install php-mbstring

然后运行:

composer update
composer require cviebrock/eloquent-sluggable

3
谢谢。如果我不使用sudo运行composer,会出现以下错误:无法读取/home/pish/.composer/auth.json文件 file_get_contents(/home/pish/.composer/auth.json):无法打开流:权限被拒绝 - alex
4
这是一个权限问题,请搜索chmod和chown命令。更改该文件夹的权限和所有者,你就会解决问题。 - Anar Bayramov
@rodrane,你的安装php-mbstring包的代码中有一个拼写错误,应该是sudo apt-get。除此之外,一切都很完美。谢谢! - Robin B
1
或者如果你使用的是7.2.x版本,可以运行sudo apt-get install php7.2-xml安装。 - cucu8
1
有没有地方可以将缺少的扩展名与实际软件包对应起来? 我在尝试(并失败)安装php7.3-dom、php7.3-extdom、php7.3-ext-dom之后,最终通过谷歌找到了正确的软件包php7.3-xml。 - Jon Freynik
显示剩余7条评论

111

sudo apt install php-xml 可以工作,但问题是它会下载最新 PHP 版本的插件。

如果你的 PHP 版本不是最新的,那么你可以在命令中添加版本号:

# PHP 7.2:
sudo apt install php7.2-xml

# PHP 7.3
sudo apt install php7.3-xml


# PHP 7.4
sudo apt install php7.4-xml

# PHP 8
sudo apt install php8.0-xml

# PHP 8.2 (latest)
sudo apt install php-xml

3
我正在使用7.3版本,有人说要安装“php-xml”,所以我安装了,但问题仍然存在。一旦你看到它有多简单,就会很容易理解,但在看到这个答案之前我完全没想到。安装php7.3-xml解决了我的问题。 - Andrew Fox
1
谢谢,这就是解决方案。由于错误安装了 PHP 8 扩展,我该如何卸载它? - MrEduar
4
您可以尝试使用命令 sudo apt-get remove php-xml - Player1

10

对于CentOS、RHEL、Fedora:

$ yum search php-xml
============================================================================================================ N/S matched: php-xml ============================================================================================================
php-xml.x86_64 : A module for PHP applications which use XML
php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol
php-xmlseclibs.noarch : PHP library for XML Security
php54-php-xml.x86_64 : A module for PHP applications which use XML
php54-php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol
php55-php-xml.x86_64 : A module for PHP applications which use XML
php55-php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol
php56-php-xml.x86_64 : A module for PHP applications which use XML
php56-php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol
php70-php-xml.x86_64 : A module for PHP applications which use XML
php70-php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol
php71-php-xml.x86_64 : A module for PHP applications which use XML
php71-php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol
php72-php-xml.x86_64 : A module for PHP applications which use XML
php72-php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol
php73-php-xml.x86_64 : A module for PHP applications which use XML
php73-php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol

然后选择与您的 PHP 版本相匹配的 php-xml 版本:

# php -v
PHP 7.2.11 (cli) (built: Oct 10 2018 10:00:29) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

# sudo yum install -y php72-php-xml.x86_64

-1

你只需要更新本地的composer文件:

  • 首先,你需要从你的应用程序中删除"composer.lock"文件

  • 然后,在你的bash中执行以下命令:

composer update --ignore-platform-req=ext-curl
  • 所以请再试一次
composer require cviebrock/eloquent-sluggable

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接