In this Magento 2 Composer guide, we will explain how to use the application-level package manager with the most popular ecommerce platform. Being inspired by Node.jsnpm, Composer provides a unified format that simplifies dependency management of PHP software. The usage of Composer in various Magento 2 projects is inevitable, because it is a part of the platform. You can run the package manager through the command line, installing dependencies for your modules. Besides, Composer provides the ability to install Magento 2 extensions available on Packagist (the repo is described below). Another important feature introduced in Composer is a set of autoload capabilities for libraries that essentially simplifies the usage of third-party software solutions.
Magento 2 Composer Integration
You must install Composer if you use the Magento software. The package manager will be necessary to update Magento 2 and its components, even if you’ve installed the ecommerce solution from an archive.
The Magento 2 Composer combination offers the following features:
Provides the ability to use third-party libraries while you don’t have to bundle them with source code of Magento 2.
Offers a component-based architecture as well as reliable dependency management.
Reduces extension conflicts as well as various compatibility issues.
Streamlines your work with versioned dependencies.
Introduces semantic versioning.
Supports some useful standards, such as PHP Framework Interoperability.
In its turn Magento 2 relies on Composer while packaging different components as well as product editions. Since some third party components used by Magento are not presented in the code base, they exist in a form of dependencies in composer.jsonfiles. Below, we will shed light on them.
Magento 2 and composer.json
While talking about composer.json files, it is necessary to explain what is a package. A package is a directory which contains composer.json as well as other component files. It’s inevitable component of the Magento 2 Composer tandem There are such types of composer.json packages:
Root composer.json package includes the main composer.json file utilized for declaring dependencies on 3rd-party components and used as a template for other root composer.json files. It is located under composer.json, its naming convention is magento/magento2ce, its type is project.
CE project composer.json package represents Magento CE project and declares dependencies on the class autoloader and the magento product. It is located under composer.json, its naming convention is magento/project-community-edition, its type is project.
CE product composer.json package represents Magento CE product and declares dependencies on such Magento components as themes and modules as well as 3rd-party components. The package is located under composer.json, its naming convention is magento/project-community-edition, its type is metapackage.
base composer.json package represents Magento CE base package which includes files/directories (excluding components) as well as introduces the “require” section to list the latter. The package is located under composer.json, its naming convention is magento/magento2-base, its type is magento2-component.
module shows a fully qualified name of a module, which is broken down into vendor, suffixes that represent the rest of the words, and the mandatory prefix. The package is located under app/code/Magento/<Module>/composer.json, its naming conventions are magento/module-catalog-inventory and magento/module-checkout, its type is magento2-module.
theme belongs to areas, that’s why you should use the area name as the first suffix. The package is located under app/design/<area>/Magento/<theme>/composer.json, its naming conventions are magento/theme-frontend-blank and magento/theme-adminhtml-backend, its type is magento2-theme.
language packs package require the lowercase language identifier. The package is located under app/i18n/magento/<language>/composer.json, its naming conventions are magento/language-en_us, magento/language-de_de, etc, its type is magento2-language.
Magento framework/libraries contains only one package – magento/framework. Particular libraries are not distinguish as separate components. The package is located under lib/internal/Magento/Framework/composer.json, its naming convention is magento/framework, its type is magento2-library.
The Magento 2 Composer cooperation provides you with the ability to submit the following package types to Magento Connect:
magento2-module usually includes source files and the top composer.json.
metapackage is a placeholder that gathers a collection of packages.
magento2-theme package type is utilized with theme packages.
magento2-language usually contains a .csv file used for translating certain content.
Naming conventions in Magento 2 and Composer
The required format for Composer packages in Magento 2 is:
Please note that there is a requirement for all letters in the name in the Magento 2 Composer duo – they must be lowercase.
Hence, Magento packages are named according to the following format:
If a package name consists of several words, these word should be separated with dash according to the Composer specification. The Magento package name convention looks as follows:
type-prefixis a component type in a Magento-specific domain.
suffixis something that identifies the component within its type.
Each component of Magento 2 can be categorized into one of 8 types listed above. In case, you have a component that doesn’t fit into any specific category, you can generalize it to magento-component.
With the aid of identifier types related to each component, the Magento 2 system ranges both directories and files of each component and places them in correct locations according to the available directory structure of Magento 2.
How to Install Composer
The following section of our Magento 2 Composer guide describes how to install Composer. First of all, you should check whether the software solution is already installed or not. Open a command prompt and enter this command:
If the command displays the appropriate information, you’ve already installed Composer. If you see an error, perform the following actions:
Go to your Magento server and create a new empty there.
If you want to update your Magento 2 project to the latest available version, utilize this command:
git pull composer install
If you are going to deploy Magento 2 CE through Composer to the website root, which is /var/www/example.com/htdocs, there can be 2 different conditions: you can be inside or outside the deploying directory performing the appropriate Magento 2 Composer command. In the first case, run this one:
How to clone the Magento 2 repository via Composer
This Magento 2 Composer procedure is necessary if you are going to contribute to the codebase of the platform. You can choose between the masteror developbranches. The first one is more stable and you can clone it with the help of the optional [-b master]argument, while the second one is the latest and and it will be cloned by default. You can find the full description of the procedure here: (Contributor) Clone the Magento repository.
How to package a module in Magento 2
In Magento 2, Composer packages are used for distributing, installing, and upgrading modules. To package your module, it is necessary to:
create a composer.json file;
package and then publish a module on the Magento Connect.
Magento 2 Composer file
composer.jsondefines such basic information as name, version, and requirements. Use only a root directory of a module to place its composer.json file and pay attention to the Composer generic schema and the following restrictions:
name – requires using the <vendor-name>/module-<module-name>format as well as lowercase letters only. Dashes must be used to separate words in the <module-name>.
type – set this value to magento2-module for all your modules.
autoload – specifies important data to be loaded. For further information, check Composer Autoloading.
While talking about the Magento 2 Composer integration, it is necessary to pay special attention to metapackages, since they provide the ability to create an extension that consists of several packages, combining it into a cohesive system. A metapackage is a .zip file that contains the metapackage composer.json file only. If your extension uses more than 1 package, use a metapackage as its root package. Note that it is the only use case for this package type. For further information, check the official composer.json documentation.
An ordinary composer.json metapackage file looks as follows:
As for a composer.json file for a module, it has the following structure:
"description":"A Magento 2 module that creates a new page",
Packaging and publishing extensions
The Magento 2 Composer combination requires the following steps to be performed in order to package and publish your extensions:
First of all, create a package of your module via a zip operation. Unnecessary directories should be excluded. Use dashes to separate words and alphanumeric characters to set the package filename. Please note that it is important not to use whitespaces.
It is also worth mentioning that the Magento 2 system can retrieve your package from any valid URL of GitHub or other third-party repo.
GitHub and Packagist
If you are going to work with GitHub and Packagist, do the following things:
If you are planning to host your extension package on GitHub and Packagist, set up git on your machine.
Then, navigate to your module directory, it should have composer.json in the root, and turn it into a new git repository. For further information, check this GitHub documentation.
Now, when your module is pushed on GitHub, you can refer to it directly via Composer or do the same action through Packagist:
Create an account on Packagist.
Find the Submit Package button, click it, and paste your GitHub repository link there.
As for private repositories, they can be used for private code or development. At the same time, it is necessary to perform installation of such modules via a CLI. The Magento 2 Composer tandem sets the following requirements:
Use Satis or Toran to make your packaging repository.
Create a package and submit or register it on your repository.
Add the following code to your composer.json:
"url":[repository url here]
Now, you can use the requirefield to reference all your packages from the private repository.