When I use Composer to run composer require ext-gmp
, without a composer.json
file or composer.lock
file in the current directory, I get the following error:
Could not find a matching version of package ext-gmp. Check the package spelling,
your version constraint and that the package is available in a stability which
matches your minimum-stability (stable).
This makes sense to me based on my understanding of PHP extensions and Composer so far. You wouldn’t use Composer to install extensions. They’re installed globally on the system. Composer is only for PHP packages.
However, I noticed that if I repeat the previous steps and instead run composer require ext-json
, I get the following output:
./composer.json has been created
Running composer update ext-json
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
No installed packages - skipping audit.
And at that point, I have a composer.json
file that looks like this:
{
"require": {
"ext-json": "*"
}
}
And I have a composer.lock
file that looks like this:
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "93d2b466348eca908189088d43f9fb1a",
"packages": [],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"ext-json": "*"
},
"platform-dev": [],
"plugin-api-version": "2.3.0"
}
I find this strange. It looks like Composer created entries in composer.json
and composer.lock
that are correct for when you want to indicate that your project requires the ext-json
installed on the system. So far, I haven’t experimented with other extensions besides ext-json
and ext-gmp
to uncover more extensions that Composer will allow you to notate in composer.json
and composer.lock
as system requirements, like for ext-json
.
What’s special about ext-json
that makes it work this way with Composer?
When I searched for whether the question was already answered, I found this question, where the asker wanted to know why Composer could not install an extension for them. This is different from my situation. Here, I understand that Composer is not the tool one would use to install a PHP extension, and therefore I understand what the error message from composer require ext-gmp
is telling me. But I want to know why Composer makes an exception for ext-json
and behaves differently for it.
System info:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.2 LTS
Release: 22.04
Codename: jammy
$ php --version
PHP 8.1.2-1ubuntu2.11 (cli) (built: Feb 22 2023 22:56:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.11, Copyright (c), by Zend Technologies
$ composer --version
Composer version 2.5.1 2022-12-22 15:33:54
2
Answers
There is nothing special about the ext-json extension compared to other extensions that allows it to be listed in
composer.json
andcomposer.lock
whereas others aren't.I was unaware that
composer require
does more than just manage your local project. It also checks your system while you're using it to add a dependency. In my case, when I rancomposer require ext-json
, it checked my system to see whether I already had the extension installed. Because I did, it didn't error out and it continued to fulfill its local project management task (editingcomposer.json
andcomposer.lock
to add it). But, when I rancomposer require ext-gmp
, it saw that I did not already have that extension installed, and it errored out early without doing anything related to local project management.I didn't actually need this system checking functionality, but if I did, the solution would have been to install the extension. I was able to test this by running
sudo apt-get install php8.1-gmp
. After that, runningcomposer require ext-gmp
did not error out and resulted in mycomposer.json
andcomposer.lock
files being edited to include it in the list of extensions required.So, in summary, my solution involved now understanding that Composer does at least two types of tasks:
And I have to keep this 2nd point in mind as I develop because I'm deploying PHP code to a cloud platform, so my computer's PHP installation has nothing to do with the PHP installation that will ultimately be running my project's code.
there is two concepts of adding functionality to your php application
PHP is written in C and have ability to extand core language functionality. this extensions usually is written in C and you can count them as "plugin" or "module" for language. you neeed to recompile PHP to make them work
there are some extensions out of the box in php – like json extension you mentioned. it allows php to work with jsons
and there are many more. for example to make PHP work with images or zip files. or to use debugger (xdebug – you should definetly try it!)
composer is php packages manager. it can’t change PHP itself. so you cant add extensions via composer. only PHP packages
but composer can verify that you PHP has extensions your application needs. this is line
"ext-json": "*"
will tell composer to warn you if this extension is missing in your PHP. butext-json
is already in any modern PHP version. so this check is uselessi never tried
ext-gmp
but i guess it’s not included in core PHP. so you should install it additionally. and add"require": "ext-gmp"
just to be sure you or your coworkers will not forget install that extension on their own