Skip to content

Instantly share code, notes, and snippets.

@hostep
Last active February 4, 2025 10:24
Add composer v2 support to older Magento2 versions

Add composer v2 support to older Magento2 versions

Magento 2.3.7 and 2.4.2 ship with composer v2 support out of the box but as far as I can see the only thing that needs to happen is to use some more modern versions of certain composer plugins which are used by certain dependencies of Magento.

This means we should be able to add composer v2 support to older Magento2 versions as well if we get a bit creative.

See below for diffs of the composer.json files you can apply to your projects, be sure to keep a mental note of these things, they will need to maintained by yourself in case newer versions of these modules are released. And if one day you update to Magento 2.3.7 or 2.4.2 or higher, you can remove these changes again.

⚠️ Disclaimer: use these tricks at your own risk!

Instructions

  1. Apply the relevant changes to the composer.json file in your project
  2. Run the composer update command specified underneath the diff with composer v1
  3. Now you can switch to composer v2, and run composer update --lock so your composer.lock file uses the new v2 format
  4. Run composer dump-autoload -o, see if it mentions autoload problems, if they look important: try to figure out how to fix them, always end this step by running composer dump-autoload (because Magento running in developer mode doesn't like an optimised autoloader)
  5. Have yourself a piece of cake and enjoy the improved performance composer v2 brings!

Diff per Magento version range

Magento >= 2.4.0 < 2.4.2

diff --git a/composer.json b/composer.json
index 60dca91..70dc34b 100644
--- a/composer.json
+++ b/composer.json
@@ -12,6 +12,8 @@
     },
     "version": "2.4.1",
     "require": {
+        "laminas/laminas-dependency-plugin": "2.1.2 as 1.0.4",
+        "magento/inventory-composer-installer": "1.2.0 as 1.1.0",
         "magento/product-community-edition": "2.4.1",
         "magento/composer-root-update-plugin": "~1.0"
     },
@@ -39,7 +41,7 @@
     },
     "require-dev": {
         "allure-framework/allure-phpunit": "~1.2.0",
-        "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
+        "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
         "friendsofphp/php-cs-fixer": "~2.16.0",
         "lusitanian/oauth": "~0.8.10",
         "magento/magento-coding-standard": "*",
composer update magento/magento-composer-installer magento/composer-root-update-plugin laminas/laminas-dependency-plugin magento/inventory-composer-installer dealerdirect/phpcodesniffer-composer-installer

Magento >= 2.3.6 < 2.3.7

diff --git a/composer.json b/composer.json
index 03713a1..162dc43 100644
--- a/composer.json
+++ b/composer.json
@@ -12,6 +12,8 @@
     },
     "version": "2.3.6-p1",
     "require": {
+        "laminas/laminas-dependency-plugin": "2.1.2 as 1.0.4", <= use version 2.0.0 if your project uses PHP 7.2
+        "magento/inventory-composer-installer": "1.2.0 as 1.1.0",
         "magento/product-community-edition": "2.3.6-p1",
         "magento/composer-root-update-plugin": "~1.0"
     },
@@ -39,7 +41,7 @@
     },
     "require-dev": {
         "allure-framework/allure-phpunit": "~1.2.0",
-        "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
+        "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
         "friendsofphp/php-cs-fixer": "~2.14.0",
         "lusitanian/oauth": "~0.8.10",
         "magento/magento-coding-standard": "*",
composer update magento/magento-composer-installer magento/composer-root-update-plugin laminas/laminas-dependency-plugin magento/inventory-composer-installer dealerdirect/phpcodesniffer-composer-installer

Magento >= 2.3.5 < 2.3.6

diff --git a/composer.json b/composer.json
index 9513f4a..4742220 100644
--- a/composer.json
+++ b/composer.json
@@ -12,11 +12,13 @@
     },
     "require": {
         "magento/product-community-edition": "2.3.5",
+        "laminas/laminas-dependency-plugin": "2.1.2 as 1.0.4", <= use version 2.0.0 if your project uses PHP 7.2
+        "magento/inventory-composer-installer": "1.2.0 as 1.1.0"
     },
     "require-dev": {
         "allure-framework/allure-phpunit": "~1.2.0",
-        "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
+        "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
         "friendsofphp/php-cs-fixer": "~2.14.0",
         "lusitanian/oauth": "~0.8.10",
         "magento/magento-coding-standard": "*",
composer update magento/magento-composer-installer laminas/laminas-dependency-plugin magento/inventory-composer-installer dealerdirect/phpcodesniffer-composer-installer

Magento >= 2.3.0 < 2.3.5

diff --git a/composer.json b/composer.json
index 78fbeb2..2119e21 100644
--- a/composer.json
+++ b/composer.json
@@ -11,6 +11,7 @@
         "sort-packages": true
     },
     "require": {
+        "magento/inventory-composer-installer": "1.2.0 as 1.1.0",
         "magento/product-community-edition": "2.3.1"
     },
     "require-dev": {
@@ -63,7 +64,8 @@
composer update magento/magento-composer-installer magento/inventory-composer-installer

Magento >= 2.2.0 < 2.3.0

diff --git a/composer.json b/composer.json
index c6c4a84..560ea7f 100644
--- a/composer.json
+++ b/composer.json
@@ -9,7 +9,7 @@
     ],
     "require": {
         "magento/product-community-edition": "2.2.2",
-        "composer/composer": "@alpha"
+        "composer/composer": "1.10.26 as 1.4.1"
     },
     "require-dev": {
         "phpunit/phpunit": "~6.2.0",
@@ -53,7 +53,8 @@
composer update magento/magento-composer-installer composer/composer

Magento >= 2.1.0 < 2.2.0

TODO - not sure if possible, need to solve justinrainbow/json-schema dependency conflict

Magento >= 2.0.0 < 2.1.0

TODO - not tested yet ...

@hostep
Copy link
Author

hostep commented Oct 19, 2021

@lamasfoker: you are correct unfortunately. Until magento/composer#23 is solved, you'll need to handle it like that. Please don't forget to remove that exclude section again when upgrading to a newer version of Magento please, as it circumvents a security feature of composer2 🙂

@lamasfoker
Copy link

Thank you @hostep, I did not think about the security issue. I see that magento/composer#23 is closed, so now I can remove the exclude section once and for all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment