Composer require phpoffice/phpspreadsheet doesn't work
When you run sudo composer require phpoffice/phpspreadsheet and hit a wall with the "Installation failed" error, the culprit is almost never the Composer package itself. The error message is explicit: Your requirements could not be resolved to an installable set of packages. Specifically, it points out that version 1.0.0 of the library requires the ext-dom extension, which your current environment does not have enabled.
The core issue here is a mismatch between what the Composer dependency resolver expects and what your PHP runtime actually provides. The error log explicitly lists /etc/php/7.0/cli/php.ini as the configuration file being used. If you are running this command in a terminal to install dependencies for a CLI script or a web server context, you must ensure the specific PHP version installed on your system has the necessary extensions loaded.
Enabling the Missing Extension
The error message provides a direct path forward by listing the .ini files where extensions can be enabled. To fix this without reinstalling PHP, you need to edit the configuration file for the CLI interface.
On Debian-based systems or similar distributions, you typically locate the file at /etc/php/7.0/cli/php.ini (or a corresponding conf.d directory). You have two primary options depending on your comfort level with system administration:
- Manual Editing: Open the file and ensure there is no
;comment character before the lineextension=dom. If the extension is listed but commented out, uncomment it. - Distro Package Management (Recommended): On Ubuntu or Debian, you can install the specific extension package without upgrading PHP:
sudo apt-get update
sudo apt-get install php7.0-dom
After installing the extension, you must restart your web server (if running a web application) to ensure the new extensions are loaded into memory. If you are strictly working in CLI mode, simply reloading the configuration or restarting the PHP-FPM service might be necessary:
sudo systemctl restart php7.0-fpm
The Version Mismatch Trap
The error output reveals a critical detail often overlooked by beginners: phpoffice/phpspreadsheet ^1.0 -> satisfiable by phpoffice/phpspreadsheet[1.0.0]. You are attempting to install version 1.x of the library.
This is a significant pitfall. Version 1.x of PHPSpreadsheet relies heavily on the legacy Excel file formats (.xls) and requires the dom extension for parsing XML-based ZIP files used internally by the library. Modern versions (2.x and later) have dropped support for .xls and generally require PHP 7.4 or higher, along with updated dependencies like maennchen/zipstream-php.
If your goal is to process modern Excel files (.xlsx) in a new project, forcing an installation of version 1.0 is likely the wrong architectural choice. You should instead specify a modern constraint:
composer require phpoffice/phpspreadsheet:^2.0
By updating the constraint, Composer will pull a version that aligns with current PHP standards and may not strictly require the dom extension depending on the specific sub-dependencies it pulls in for ZIP handling (though checking your specific PHP version's built-in capabilities is still wise).
Avoiding sudo in Development
Finally, the use of sudo in the command sudo composer require... suggests you might be running Composer as a system administrator rather than within the context of your specific application user. In production environments or when using tools like Docker or Vagrant, running Composer as root can lead to permission issues where the library files are installed but not readable by the web server user (e.g., www-data) or the PHP-FPM user.
Unless you are managing a system-wide utility that every user needs access to, it is best practice to run Composer commands within your project directory without elevated privileges. This ensures that the autoloader and vendor directories respect the file permissions set during your deployment process. If you must use sudo, ensure you know which PHP binary version sudo invokes, as multiple versions of PHP might be installed on a single server.
