🎃

Composerの依存パッケージを削除する

2021/11/06に公開

最近Composerの依存パッケージを削除する機会があったのでその時の手順をメモとして残しておきます。

手順

その1

composer remove パッケージA --no-update

オプションに--no-updateを付けて実行しているので、composer.jsonから指定したパッケージの記述だけを削除します。

その2

composer u --dry-run
Loading composer repositories with package information
Updating dependencies
Lock file operations: 0 install, 2 updates, 3 removals
  - Removing パッケージD
  - Removing パッケージB
  - Removing パッケージA
  - Upgrading パッケージE
  - Upgrading パッケージC

オプションに--dry-runを付けることで、実際には削除や更新を行わないようにしています。
composer.jsonから記述が消えたパッケージとそのパッケージが依存しているパッケージが消えることを確認します。

その3

composer u パッケージA パッケージB パッケージD
Loading composer repositories with package information
Updating dependencies
Lock file operations: 0 installs, 0 updates, 3 removals
  - Removing パッケージD
  - Removing パッケージB
  - Removing パッケージA
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 0 updates, 3 removals
  - Removing パッケージD
  - Removing パッケージB
  - Removing パッケージA

その2でRemovingと表示されたパッケージを直接指定してcomposer uを実行します。
これで他のパッケージが意図しないバージョンアップを防ぐことができます。

参考

https://qiita.com/ngyuki/items/0015ce15e8ca6c2609f1

Discussion