Skip to content

Instantly share code, notes, and snippets.

@ProcessEight
Last active December 19, 2022 02:50
Show Gist options
  • Save ProcessEight/18adf3a7b0a2ea2a0fb3f52424673136 to your computer and use it in GitHub Desktop.
Save ProcessEight/18adf3a7b0a2ea2a0fb3f52424673136 to your computer and use it in GitHub Desktop.
Composer cheatsheet: Common Composer tasks

Composer cheatsheet

Table of Contents

Created by gh-md-toc

Require new module

composer require <vendor_name>/<package_name> <desired_version>

Require developer-only module

composer require --dev <vendor_name>/<package_name> <desired_version>

Add a new repository

# composer config <flags> repositories.<repo_identifier> <type> <url>
composer config -g repositories.firegento composer https://packages.firegento.com

Add auth keys for repository

# composer config <flags> <auth_method>.<composer_repository_url> <username> <password>
composer config http-basic.dist.aheadworks.com ABC123HYGKBGIG DEF456F76FGYBUOHNJKL

Use a git repository as a dependency

The git repo must have a composer.json (like this one: https://github.com/ProjectEight/AvS_FastSimpleImport/blob/master/composer.json). Use the name field from that file as the vendor_name/package_name combination.

  • Then add the git repo as a new composer repository:
composer config repositories.avs git git@github.com:ProjectEight/AvS_FastSimpleImport.git
  • Or just add it to composer.json manually:
{
    "repositories": {
        "avs": {
            "type": "git",
            "url": "git@github.com:ProjectEight/AvS_FastSimpleImport.git"
        }
    }
}

Then require the dependency in the usual way:

composer require vendorname/magento2-module-storefinder

If the extension doesn't have a version, you can use a git branch as the version. Run composer require as usual, but with the branch name prefixed with dev- as the version constraint:

# composer require --dev <vendor_name>/<module_name>:dev-<branch_name>
composer require --dev avstudnitz/fast-simple-import:dev-master

Use a local folder as a repository

Use the path repository type:

composer config <flags> repositories.<repo_identifier> <type> <path>
composer config repositories.vendorname path "extensions/vendorname/*"

The path is relative to the composer.json file. The path can include glob() directives, e.g. * as a wildcard character. In the above example, any Magento extension in the path <magento_base_dir>/extensions/vendorname/* which has a composer.json can be installed using composer.

The include the package as normal:

composer require <vendor_name>/<module_name>:<version_string>
composer require projecteight/module:1.0

Full article: https://medium.com/@barryvdh/using-local-repositories-to-easily-install-private-magento-extensions-with-composer-7eb966dec23e

Package a module for Composer

Packaging a module involves creating composer.json and registration.php files, then choosing a deployment method.

  • Create the composer.json file in $MODULE_ROOT/composer.json:
{
  "name": "projecteight/magento2-tpp-training1-freegeoip",
  "description": "Magento 2 Trained Partner Program Unit One Task One",
  "require": {
    "php": "7.0.2|7.0.4|~7.0.6|~7.1.0"
  },
  "autoload": {
    "files": [
      "registration.php"
    ],
    "psr-4": {
      "Training1\\FreeGeoIp\\": ""
    }
  },
  "type": "magento2-module",
  "version": "0.1.0",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ]
}
  • Validate the module using the Validation Tool
  • Your $MODULE_ROOT should look something like this:
$ ll magento2-tpp-training1-freegeoip/
total 76
drwxrwxr-x  6 zone8 zone8  4096 Feb 24 09:49 ./
drwxr-xr-x 74 zone8 zone8  4096 Feb 24 09:49 ../
-rw-rw-r--  1 zone8 zone8   417 Feb 24 09:49 composer.json
drwxrwxr-x  3 zone8 zone8  4096 Feb 24 09:49 etc/
drwxrwxr-x  8 zone8 zone8  4096 Feb 24 09:49 .git/
-rw-rw-r--  1 zone8 zone8    53 Feb 24 09:49 .gitignore
-rw-rw-r--  1 zone8 zone8 35147 Feb 24 09:49 LICENSE
drwxrwxr-x  3 zone8 zone8  4096 Feb 24 09:49 Model/
drwxrwxr-x  2 zone8 zone8  4096 Feb 24 09:49 Observer/
-rw-rw-r--  1 zone8 zone8   301 Feb 24 09:49 README.md
-rw-rw-r--  1 zone8 zone8   167 Feb 24 09:49 registration.php

A packaged module must have, at a minimum, composer.json and registration.php files.

With Packagist

Git is just used as a convenient way to send the package files to Packagist. It is not required to package a module.

  • Create a git repo in $MODULE_ROOT. Both the composer.json and .git directory must be in $MODULE_ROOT. Do not wrap the module in the $VENDOR_NAME/$MODULE_NAME directories. Composer will create these when the package is required.
  • Login to https://packagist.org/. Specify your public GitHub/BitBucket/etc repo URL. Packagist will pull in your files automatically and create the package by reading the composer.json.
  • You can now install the extension in the normal way using composer require projecteight/magento2-tpp-training1-freegeoip
  • You don't need to add a new repository to the project composer.json because the package is listed on the public Packagist, which is where Composer will look first when searching for the package.

Without Packagist

You can package and install a module without using git.

You can either create an archive of your module and tell composer where to find it, or you can just give composer the path to your files.

To archive your module:

zip -r vendor-name_package-name-1.0.0.zip package-path/ -x 'package-path/.git/*'

Then you will need to add an artifact repository like so:

{
    "repositories": [
        {
            "type": "artifact",
            "url": "path/to/directory/with/zips/"
        }
    ],
    "require": {
        "vendor_name/package_name": "1.0.0"
    }
}
  • You can now install the extension in the normal way using composer require vendor_name/package_name

To tell Composer to require a package from your local filesystem, you will need to add a path repository like so:

{
    "repositories": [
        {
            "type": "path",
            "url": "../../packages/vendor_name-package_name"
        }
    ]
}
  • You can now install the extension in the normal way using composer require vendor_name/package_name

Notes

If the package is a local VCS repository, the version may be inferred by the branch or tag that is currently checked out. Otherwise, the version should be explicitly defined in the package's composer.json file. If the version cannot be resolved by these means, it is assumed to be dev-master.

The local package will be symlinked if possible, in which case the output in the console will read Symlinked from ../../packages/my-package. If symlinking is not possible the package will be copied. In that case, the console will output Mirrored from ../../packages/my-package.

Instead of default fallback strategy you can force to use symlink with "symlink": true or mirroring with "symlink": false option. Forcing mirroring can be useful when deploying or generating package from a monolithic repository.

Sources: Magento 2 Dev Docs: Package a component, Composer: Repositories

Troubleshooting

Your requirements could not be resolved to an installable set of packages.

Problem 1
- Root composer.json requires vendorname/magento2-checkout == 1.0.4.0, found vendorname/magento2-checkout[1.0.4] in the lock file but not in remote repositories, make sure you avoid updating this package to keep the one from the lock file.

This maybe due to Composer not being able to query Packagist. If you're using a package proxy like Repman and you have the repository.packagist false setting enabled, try removing the setting. Setting packagist to false forces Composer to not query Packagist and only query any configured proxies or repositories.

Could not find a matching version of package vendorname/magento2-faqs. Check the package spelling, your version constraint and that the package is available in a stability which matches your minimum-stability (alpha)

  • Verify there are no typos in the dependency name (the dependency name and the git repository name may be different)
  • If the dependency is being pulled in from a git repo, verify there is a repository of type vcs/git/bitbucket (as appropriate) setup in composer.json for this dependency
  • If Packagist or Repman (or similar) is being used, verify it has been configured and that there is a repository for the service in the repositories section of composer.json.

Cannot require packages, Failed to decode response, Failed to decode zlib stream, appears broken, and returned an empty 200 response

- Installing amasty/module-banners-lite (1.0.18): Downloading (100%)         
  Failed to decode response: Failed to decode zlib stream
  Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info
  Downloading (100%)         
  Downloading (100%)         
  Downloading (100%)

[Composer\Downloader\TransportException]                                                                                            
"https://composer.amasty.com/community/packages/module-banners-lite-1.0.18.zip" appears broken, and returned an empty 200 response

Fixed it by adding the Amasty Composer repository keys to the global auth.json.

Can not require core Magento 2 packages

$ mlnbphp /usr/local/bin/composer require vendorname/magento2-storefinder
PHP 7.4.30 (cli) (built: Aug  1 2022 15:06:03) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies
    with blackfire v1.80.0~linux-x64-non_zts74, https://blackfire.io, by Blackfire
Warning from https://repo.packagist.org: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2. See https://blog.packagist.com/deprecating-composer-1-support/
Info from https://repo.packagist.org: #StandWithUkraine
Using version ^1.0 for vendorname/magento2-storefinder
./composer.json has been updated
No patches supplied.
Loading composer repositories with package information
Warning from https://repo.packagist.org: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2. See https://blog.packagist.com/deprecating-composer-1-support/
Info from https://repo.packagist.org: #StandWithUkraine
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - ocramius/proxy-manager 2.10.2 requires composer-runtime-api ^2.0.0 -> no matching package found.
    - ocramius/proxy-manager 2.10.2 requires composer-runtime-api ^2.0.0 -> no matching package found.
    - ocramius/proxy-manager 2.10.2 requires composer-runtime-api ^2.0.0 -> no matching package found.
    - Conclusion: remove laminas/laminas-code 3.5.1
    - Conclusion: don't install laminas/laminas-code 3.5.1
    - friendsofphp/proxy-manager-lts v1.0.10 requires laminas/laminas-code ~3.4.1|^4.0 -> satisfiable by laminas/laminas-code[3.4.1, 4.0.0, 4.1.0, 4.2.0, 4.2.1, 4.2.2, 4.3.0, 4.4.0, 4.4.1, 4.4.2, 4.4.3, 4.5.0, 4.5.1, 4.5.2, 4.6.0].
    - friendsofphp/proxy-manager-lts v1.0.11 requires laminas/laminas-code ~3.4.1|^4.0 -> satisfiable by laminas/laminas-code[3.4.1, 4.0.0, 4.1.0, 4.2.0, 4.2.1, 4.2.2, 4.3.0, 4.4.0, 4.4.1, 4.4.2, 4.4.3, 4.5.0, 4.5.1, 4.5.2, 4.6.0].
    - friendsofphp/proxy-manager-lts v1.0.12 requires laminas/laminas-code ~3.4.1|^4.0 -> satisfiable by laminas/laminas-code[3.4.1, 4.0.0, 4.1.0, 4.2.0, 4.2.1, 4.2.2, 4.3.0, 4.4.0, 4.4.1, 4.4.2, 4.4.3, 4.5.0, 4.5.1, 4.5.2, 4.6.0].
    - friendsofphp/proxy-manager-lts v1.0.2 requires laminas/laminas-code ~3.4.1|^4.0 -> satisfiable by laminas/laminas-code[3.4.1, 4.0.0, 4.1.0, 4.2.0, 4.2.1, 4.2.2, 4.3.0, 4.4.0, 4.4.1, 4.4.2, 4.4.3, 4.5.0, 4.5.1, 4.5.2, 4.6.0].
    - friendsofphp/proxy-manager-lts v1.0.3 requires laminas/laminas-code ~3.4.1|^4.0 -> satisfiable by laminas/laminas-code[3.4.1, 4.0.0, 4.1.0, 4.2.0, 4.2.1, 4.2.2, 4.3.0, 4.4.0, 4.4.1, 4.4.2, 4.4.3, 4.5.0, 4.5.1, 4.5.2, 4.6.0].
    - friendsofphp/proxy-manager-lts v1.0.4 requires laminas/laminas-code ~3.4.1|^4.0 -> satisfiable by laminas/laminas-code[3.4.1, 4.0.0, 4.1.0, 4.2.0, 4.2.1, 4.2.2, 4.3.0, 4.4.0, 4.4.1, 4.4.2, 4.4.3, 4.5.0, 4.5.1, 4.5.2, 4.6.0].
    - friendsofphp/proxy-manager-lts v1.0.5 requires laminas/laminas-code ~3.4.1|^4.0 -> satisfiable by laminas/laminas-code[3.4.1, 4.0.0, 4.1.0, 4.2.0, 4.2.1, 4.2.2, 4.3.0, 4.4.0, 4.4.1, 4.4.2, 4.4.3, 4.5.0, 4.5.1, 4.5.2, 4.6.0].
    - friendsofphp/proxy-manager-lts v1.0.6 requires laminas/laminas-code ~3.4.1|^4.0 -> satisfiable by laminas/laminas-code[3.4.1, 4.0.0, 4.1.0, 4.2.0, 4.2.1, 4.2.2, 4.3.0, 4.4.0, 4.4.1, 4.4.2, 4.4.3, 4.5.0, 4.5.1, 4.5.2, 4.6.0].
    - friendsofphp/proxy-manager-lts v1.0.7 requires laminas/laminas-code ~3.4.1|^4.0 -> satisfiable by laminas/laminas-code[3.4.1, 4.0.0, 4.1.0, 4.2.0, 4.2.1, 4.2.2, 4.3.0, 4.4.0, 4.4.1, 4.4.2, 4.4.3, 4.5.0, 4.5.1, 4.5.2, 4.6.0].
    - friendsofphp/proxy-manager-lts v1.0.8 requires laminas/laminas-code ~3.4.1|^4.0 -> satisfiable by laminas/laminas-code[3.4.1, 4.0.0, 4.1.0, 4.2.0, 4.2.1, 4.2.2, 4.3.0, 4.4.0, 4.4.1, 4.4.2, 4.4.3, 4.5.0, 4.5.1, 4.5.2, 4.6.0].
    - friendsofphp/proxy-manager-lts v1.0.9 requires laminas/laminas-code ~3.4.1|^4.0 -> satisfiable by laminas/laminas-code[3.4.1, 4.0.0, 4.1.0, 4.2.0, 4.2.1, 4.2.2, 4.3.0, 4.4.0, 4.4.1, 4.4.2, 4.4.3, 4.5.0, 4.5.1, 4.5.2, 4.6.0].
    - Can only install one of: laminas/laminas-code[4.5.0, 3.5.1].
    - Can only install one of: laminas/laminas-code[4.5.1, 3.5.1].
    - Can only install one of: laminas/laminas-code[4.5.2, 3.5.1].
    - Can only install one of: laminas/laminas-code[3.4.1, 3.5.1].
    - Can only install one of: laminas/laminas-code[4.0.0, 3.5.1].
    - Can only install one of: laminas/laminas-code[4.1.0, 3.5.1].
    - Can only install one of: laminas/laminas-code[4.2.0, 3.5.1].
    - Can only install one of: laminas/laminas-code[4.2.1, 3.5.1].
    - Can only install one of: laminas/laminas-code[4.2.2, 3.5.1].
    - Can only install one of: laminas/laminas-code[4.3.0, 3.5.1].
    - Can only install one of: laminas/laminas-code[4.4.0, 3.5.1].
    - Can only install one of: laminas/laminas-code[4.4.1, 3.5.1].
    - Can only install one of: laminas/laminas-code[4.4.2, 3.5.1].
    - Can only install one of: laminas/laminas-code[4.4.3, 3.5.1].
    - Can only install one of: laminas/laminas-code[4.6.0, 3.5.1].
    - Installation request for laminas/laminas-code (locked at 3.5.1) -> satisfiable by laminas/laminas-code[3.5.1].
    - Installation request for ocramius/proxy-manager (locked at 2.10.2) -> satisfiable by friendsofphp/proxy-manager-lts[v1.0.10, v1.0.11, v1.0.12, v1.0.2, v1.0.3, v1.0.4, v1.0.5, v1.0.6, v1.0.7, v1.0.8, v1.0.9], ocramius/proxy-manager[2.10.2].

Potential causes:
 - A typo in the package name
 - The package is not available in a stable-enough version according to your minimum-stability setting
   see <https://getcomposer.org/doc/04-schema.md#minimum-stability> for more details.
 - It's a private package and you forgot to add a custom repository to find it

Read <https://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.

Try using Composer 2 instead of Composer 1.

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