猫でもわかるWebプログラミングと副業

本業エンジニアリングマネージャー。副業Webエンジニア。Web開発のヒントや、副業、日常生活のことを書きます。

aws/aws-sdk-php で Your requirements could not be resolved to an installable set of packages.

f:id:yoshiki_utakata:20220222001238j:plain

composer ライブラリの依存エラー

composer で aws/aws-sdk-php のライブラリをインストールしたところ、以下のようなエラーに遭遇しました。

$ composer require aws/aws-sdk-php
Using version ^3.209 for aws/aws-sdk-php
./composer.json has been updated
Running composer update aws/aws-sdk-php
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires aws/aws-sdk-php ^3.209 -> satisfiable by aws/aws-sdk-php[3.209.0, ..., 3.209.27].
    - aws/aws-sdk-php[3.209.0, ..., 3.209.27] require guzzlehttp/psr7 ^1.7.0|^2.0 -> found guzzlehttp/psr7[dev-master, 1.7.0, ..., 1.x-dev, 2.0.0-beta1, ..., 2.2.x-dev (alias of dev-master)] but the package is fixed to 1.6.1 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.

Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.
You can also try re-running composer require with an explicit version constraint, e.g. "composer require aws/aws-sdk-php:*" to figure out if any version is installable, or "composer require aws/aws-sdk-php:^2.1" if you know which you need.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

このエラー、読みづらくて分かりづらいのですが、要するに、ライブラリの依存解決でエラーが発生したことを意味しています。

エラーの見かた

めちゃめちゃ読みづらいエラーなのですが、丁寧に読みましょう。注目すべきは、 Problem 1 の2行目の部分です。

改行をいれて読みやすくしています。

aws/aws-sdk-php[3.209.0, ..., 3.209.27] 
require guzzlehttp/psr7 ^1.7.0|^2.0 
-> found guzzlehttp/psr7
   [dev-master, 1.7.0, ..., 1.x-dev, 2.0.0-beta1, ..., 2.2.x-dev (alias of dev-master)] 
but the package is fixed to 1.6.1 (lock file version) by a partial update 
and that version does not match. 
Make sure you list it as an argument for the update command.



まず、 aws/aws-sdk-php[3.209.0, ..., 3.209.27] この部分を見ます。

今回実行したコマンドは composer require aws/aws-sdk-php なのですが、最新の aws/aws-sdk-php のバージョンは 3.209.27 でした。

composer require は、バージョンを指定しない場合、 aws/aws-sdk-php:^3.209 になります。

つまり、 3.209.0 〜 3.209.27 のどれかをインストールすることになります。それが aws/aws-sdk-php[3.209.0, ..., 3.209.27] 表記の意味です。

3.209.0 〜 3.209.27 のうちどれがインストールされるかは、他のライブラリとの依存関係によります。他のライブラリとの依存関係をクリアするなかで、最も最新のバージョンがインストールされます。



つづいて、1行目と2行目をあわせて読みます。

aws/aws-sdk-php[3.209.0, ..., 3.209.27] 
require guzzlehttp/psr7 ^1.7.0|^2.0 

これはつまり、 aws/aws-sdk-php:^3.209 は、 guzzlehttp/psr7 の、1.7.X 系以上、または、2.X 系が必要ということを意味しています(と、思ったのですが、1.8.X でも大丈夫でした。1.X 系、かつ 1.7.0 以上であればいいのかも。)。



しかし、こう書かれています。

-> found guzzlehttp/psr7[dev-master, 1.7.0, ..., 1.x-dev, 2.0.0-beta1, ..., 2.2.x-dev (alias of dev-master)] 
but the package is fixed to 1.6.1 (lock file version) by a partial update 
and that version does not match. 

注目すべきは but the package is fixed to 1.6.1 で、すでに guzzlehttp/psr7 の 1.6.1 がインストールされているのでエラー、とのことです。



つまり、 aws/aws-sdk-php 3.209 をインストールするには、 guzzlehttp/psr7 の 1.7 以上が必要なのに、今 1.6.1 がインストールされているのでダメ、ということです。

解決方法

composer update guzzlehttp/psr7 とすると、 guzzlehttp/psr7 のバージョンが上がってインストールできました。

詳細な解決方法

私は、 composer.json に guzzlehttp/psr7 を書いていないので、composer.json に書かれているなにかのライブラリが、guzzlehttp/psr7 に依存していることになります。

当時の composer.json の require はこんな感じ

    "require": {
        "php": "^7.2.5",
        "fideloper/proxy": "^4.2",
        "fruitcake/laravel-cors": "^2.0",
        "guzzlehttp/guzzle": "^6.3",
        "laravel/framework": "^7.24",
        "laravel/slack-notification-channel": "^2.4",
        "laravel/tinker": "^2.0"
    },

明らかに guzzlehttp/guzzle が依存していそうです。



パッケージの依存関係を見るには、 composer.lock ファイルを見ます。この仲で、 guzzlehttp/psr7 を検索してみます。すると、こうなっていました。一部不要な部分は省略しています。

        {
            "name": "guzzlehttp/guzzle",
            "version": "6.5.5",
            "require": {
                "ext-json": "*",
                "guzzlehttp/promises": "^1.0",
                "guzzlehttp/psr7": "^1.6.1",
                "php": ">=5.5",
                "symfony/polyfill-intl-idn": "^1.17.0"
            },
        },
        {
            "name": "guzzlehttp/psr7",
            "version": "1.6.1",
        },

私の環境には guzzlehttp/guzzle の 6.5.5 がインストールされています。guzzlehttp/guzzle の 6.5.5 は、^1.6.1"guzzlehttp/psr7 が必要で、実際にインストールされているバージョンは 1.6.1 でした。

composer update guzzlehttp/psr7

では、この状態から composer update guzzlehttp/psr7 するとどうなるのでしょうか。

こうなりました。

        {
            "name": "guzzlehttp/guzzle",
            "version": "6.5.5",
            "require": {
                "ext-json": "*",
                "guzzlehttp/promises": "^1.0",
                "guzzlehttp/psr7": "^1.6.1",
                "php": ">=5.5",
                "symfony/polyfill-intl-idn": "^1.17.0"
            },
        },
        {
            "name": "guzzlehttp/psr7",
            "version": "1.8.3",
        },

"guzzlehttp/psr7": "^1.6.1", の条件を満たすなかで最も最新の 1.8.3 がインストールされました。

これで、aws/aws-sdk-php の要求する guzzlehttp/psr7 ^1.7.0|^2.0 の条件はみたしたので、無事にインストールできます。

guzzlehttp/promises

もう一回 aws/aws-sdk-php をインストールしようとしたら以下のエラーがでました。

$ composer require aws/aws-sdk-php
Using version ^3.209 for aws/aws-sdk-php
./composer.json has been updated
Running composer update aws/aws-sdk-php
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires aws/aws-sdk-php ^3.209 -> satisfiable by aws/aws-sdk-php[3.209.0, ..., 3.209.27].
    - aws/aws-sdk-php[3.209.0, ..., 3.209.27] require guzzlehttp/promises ^1.4.0 -> found guzzlehttp/promises[dev-master, 1.4.0, ..., 1.5.x-dev (alias of dev-master)] but the package is fixed to v1.3.1 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.

Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.
You can also try re-running composer require with an explicit version constraint, e.g. "composer require aws/aws-sdk-php:*" to figure out if any version is installable, or "composer require aws/aws-sdk-php:^2.1" if you know which you need.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.



これも結局同じで、 composer update guzzlehttp/promises したら解決しました。

最後に

最終的には無事にインストールができました。一応ライブラリのバージョンが上がっているので、動作確認はしっかり行ってください。

composer require aws/aws-sdk-php
Using version ^3.209 for aws/aws-sdk-php
./composer.json has been updated
Running composer update aws/aws-sdk-php
Loading composer repositories with package information
Updating dependencies
Lock file operations: 3 installs, 0 updates, 0 removals
  - Locking aws/aws-crt-php (v1.0.2)
  - Locking aws/aws-sdk-php (3.209.27)
  - Locking mtdowling/jmespath.php (2.6.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
  - Downloading mtdowling/jmespath.php (2.6.1)
  - Downloading aws/aws-crt-php (v1.0.2)
  - Downloading aws/aws-sdk-php (3.209.27)
  - Installing mtdowling/jmespath.php (2.6.1): Extracting archive
  - Installing aws/aws-crt-php (v1.0.2): Extracting archive
  - Installing aws/aws-sdk-php (3.209.27): Extracting archive
3 package suggestions were added by new dependencies, use `composer suggest` to see details.
Package phpunit/php-token-stream is abandoned, you should avoid using it. No replacement was suggested.
Generating optimized autoload files
Class Tests\Legacy\Secure\Payment\Lib\NiwavidePaymentDbTest located in ./tests/Secure/Payment/Lib/NiwavidePaymentDbTest.php does not comply with psr-4 autoloading standard. Skipping.
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/slack-notification-channel
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
2 packages you are using are looking for funding.
Use the `composer fund` command to find out more!