Composer Dependency Manager
IntelliJ IDEA integrates with the Composer dependency manager, which allows you to declare packages from Packagist as your project's dependencies and manage them from the IDE.
IntelliJ IDEA provides a dedicated user interface for the common Composer commands:
- Creating a new project with Composer,
- Initializing Composer in an existing IntelliJ IDEA project,
- Managing project dependencies,
- Performing additional tasks.
When you run the above commands, IntelliJ IDEA displays their output via the Composer Log console.
If you wish to use the full range of Composer commands in a IntelliJ IDEA project, you can configure and run Composer as an external command-line tool.
Downloading and installing Composer
You can download and install Composer manually as a system-wide standalone tool. To do so, refer to the Composer official documentation.
Alternatively, IntelliJ IDEA will prompt you to download Composer automatically when you create a new Composer project, initialize Composer, or invoke other Composer commands in an existing project. In this case, Composer is run via the configured local PHP interpreter.
Creating a new Composer project
When you create a new Composer project, IntelliJ IDEA creates a project based on the Composer package of your choice, resolves the dependencies and installs them under the project's vendor
directory.
To create a new Composer project:
-
Choose Create New Project on the Welcome screen.
or clickThe New Project dialog opens:
- In the dialog, specify the project's parameters:
- Choose the project type and location:
- In the left-hand pane, click PHP from the list, then choose Composer Project in the right-hand pane, and then click Next.
- On the second page of the wizard, specify the project name and the folder where it will be created.
- Choose how the Composer commands will be executed:
- To run commands via a globally installed
composer
executable, choose 'composer' executable and specify its location in the text box. -
To run commands via the locally installed composer.phar archive, choose composer.phar. Then, either specify the location of the existing
composer.phar
file in the text box or select the Download composer.phar from getcomposer.org checkbox to download a new instance of the file. Thecomposer.phar
file will be saved under the project root folder specified in the Location text box.Then, choose one of the configured local PHP interpreters from the PHP interpreter list. Refer to Configuring Local PHP Interpreters for details.
require-dev
section of thecomposer.json
file instead of the defaultrequire
section, type--dev
. For more information about using the Composer command line options during installation, refer to the Composer documentation. - To run commands via a globally installed
- In the Package area, select the package from the list, and choose the relevant version from the Version to install list. To search for a package, use the Filter packages search field.
- Choose the project type and location:
-
Click Finish. The create-project Composer command will be invoked with the selected package. As a result, the Composer project will be created, whose configuration and structure depends on the selected package. After that, the created IntelliJ IDEA project opens.
Initializing Composer in an existing project
When you initialize Composer in a project, a composer.json
file is created. This file lists the project dependencies and other metadata.
To initialize Composer in an existing project:
-
On the main menu, choose
.The Composer Settings dialog opens:
- Choose how the Composer commands will be executed:
- To run commands via a globally installed
composer
executable, choose 'composer' executable and specify its location in the text box. -
To run commands via the locally installed composer.phar archive, choose composer.phar. Then, either specify the location of the existing
composer.phar
file in the text box or select the Download composer.phar from getcomposer.org checkbox to download a new instance of the file. Thecomposer.phar
file will be saved under the project root folder specified in the Location text box.Then, choose one of the configured local PHP interpreters from the PHP interpreter list. Refer to Configuring Local PHP Interpreters for details.
- To run commands via a globally installed
-
Click OK to close the Composer Settings dialog. IntelliJ IDEA creates a stub of the
composer.json
file and opens it in the editor:Complete the code or accept the generated values. For details on working with the
composer.json
file, refer to Composer.json: Project Setup.
Opening an existing Composer project in IntelliJ IDEA
When you open an existing Composer project, IntelliJ IDEA uses the information contained in the composer.json
file to automatically apply the certain configuration options, e.g. the project's content roots and PHP language level.
- Click Open on the Welcome screen or choose on the main menu, then choose the folder where your Composer project is stored.
IntelliJ IDEA will show a notification:
- Click Composer dialog that opens, specify the location of the
composer.json
file. If IntelliJ IDEA detects an existing file, the field will be filled automatically: . In the -
Configure the project settings:
-
By default, all packages under the
vendor/*/*
directory are excluded from the project and added as write-protected libraries. To protect them from editing, leave the checkbox selected. Otherwise, if you want to edit Composer packages undervendor/*/*
, clear the checkbox. -
Make sure the
checkbox is selected to automatically detect the PHP language level and configure project Source and Test roots based on the configuration fromcomposer.json
.IntelliJ IDEA is aware of PSR-0/PSR-4 source roots and of their namespace prefixes declared in the
autoload
andautoload-dev
sections incomposer.json
. IntelliJ IDEA also detects the PHP language level based on thephp
setting in therequire
section.Because
composer.json
contains the most up-to-date information about the project configuration, this automatic synchronization ensures that the Source and Test folder exactly match the project structure and the correct PHP language level is set automatically.To learn more about PSR and autoloading, refer to the Composer official website. For examples and details on synchronizing settings, refer to the PhpStorm blog post.
Click OK to apply the specified project settings and close the dialog.
-
- Set up Composer commands execution:
- Do one of the following:
- Open the Settings/Preferences dialog (Ctrl+Alt+S) and navigate to .
- If you have a single
composer.json
file in your project, choose on the main menu to open the Composer Settings dialog.
- Choose how the Composer commands will be executed:
- To run commands via a globally installed
composer
executable, choose 'composer' executable and specify its location in the text box. -
To run commands via the locally installed composer.phar archive, choose composer.phar. Then, either specify the location of the existing
composer.phar
file in the text box or select the Download composer.phar from getcomposer.org checkbox to download a new instance of the file. Thecomposer.phar
file will be saved under the project root folder specified in the Location text box.Then, choose one of the configured local PHP interpreters from the PHP interpreter list. Refer to Configuring Local PHP Interpreters for details.
- To run commands via a globally installed
- Do one of the following:
-
Install the project dependencies:
- If you have a single
composer.json
file in your project, choose on the main menu. - Otherwise, if you have several
composer.json
files, choose from the context menu of the relevant one. See also Appointing a default composer.json in a IntelliJ IDEA project.
composer.json
file in the code editor and click the Install shortcut link on top of the editor panel. - If you have a single
Appointing a default composer.json in a IntelliJ IDEA project
You can have several composer.json
files in one IntelliJ IDEA project. For each composer.json
, actions are invoked from its context menu in the editor or in the Project view, or using the shortcut links on top of the editor panel. You can also appoint the default composer.json for your IntelliJ IDEA project. Composer actions for it are invoked from on the main menu.
To appoint a default composer.json
:
- Open the Settings/Preferences dialog (Ctrl+Alt+S) and navigate to .
- Specify the location of the default
composer.json
. If you have a single Composer configuration file in your IntelliJ IDEA project, the field is filled automatically. -
By default, all packages under the
Otherwise, if you want to edit Composer packages undervendor/*/*
directory are excluded from the project and added as write-protected libraries. To protect them from editing, leave the checkbox selected.vendor/*/*
, clear the checkbox. - Make sure the
composer.json
.IntelliJ IDEA is aware of PSR-0/PSR-4 source roots and of their namespace prefixes declared in the
autoload
andautoload-dev
sections incomposer.json
. IntelliJ IDEA also detects the PHP language level based on thephp
setting in therequire
section.Because
composer.json
contains the most up-to-date information about the project configuration, this automatic synchronization ensures that the Source and Test folder exactly match the project structure and the correct PHP language level is set automatically.To learn more about PSR and autoloading, refer to the Composer official website. For examples and details on synchronizing settings, refer to the PhpStorm blog post.
checkbox is selected to automatically detect the PHP language level and configure project Source and Test roots based on the configuration from
Installing dependencies
You can install specific packages individually, and IntelliJ IDEA will update your composer.json
automatically. Alternatively, you can list the packages you need in the require
and require-dev
sections of the composer.json
and then install them all at once.
If the composer.lock
file is present in the current directory, the exact packages versions listed in it will be installed. Otherwise, if the composer.lock
file is missing, Composer will create it after installing the packages, thus locking your project to their specific versions.
Learn more about adding dependencies on the Composer Official website.
Installing a specific package
- Do one of the following:
- On the context menu of
composer.json
, choose . - To install a package from the default composer.json, choose on the main menu.
- On the context menu of
- In the Manage Composer Dependencies dialog that opens, select the required package from the list. To find a package, use the search field. The list shows all the available packages, the packages that are already installed are marked with a tick.
Choose the relevant version from the Version to install list.
- Optionally, customize the package installation. In the Command line parameters text box, type the additional command line parameters. For example, to have a dependency added to the
require-dev
section of thecomposer.json
file instead of the defaultrequire
section, type--dev
. For more information about using the Composer command line options during installation, refer to the Composer documentation. - Click Install to start installation. To exit the dialog, click Close.
When the package is installed, IntelliJ IDEA creates a new subfolder under vendor
and stores the new package in this subfolder. The corresponding package records are added to the require
or the require-dev
section of the composer.json
file as well as to the composer.lock file.
Installing all packages at once
Do any of the following:
- On the context menu of
composer.json
, choose . - Open
composer.json
in the editor the and click the Install shortcut link on top of the editor panel. - To install all packages listed in the default composer.json, choose on the main menu.
IntelliJ IDEA installs all the packages listed in the require
and require-dev
sections of the composer.json
file.
Updating dependencies
You can update specific packages to their latest versions individually, and IntelliJ IDEA will update your composer.json
automatically. Alternatively, you can update all the packages listed in the require
and require-dev
sections of the composer.json
file at once.
Updating a specific package
- Do one of the following:
- On the context menu of
composer.json
, choose . - To update a package from the default composer.json, choose on the main menu.
- On the context menu of
- Select the required package from the Available Packages list among the packages marked with a tick and click Update.
Updating all packages at once
- On the context menu of
composer.json
, choose . - Open
composer.json
in the editor the and click the Update shortcut link on top of the editor panel. - To update all packages listed in the default composer.json, choose on the main menu.
IntelliJ IDEA updates all the packages listed in the require
and require-dev
sections of the composer.json
file.
Uninstalling dependencies
To uninstall a package:
- Do any of the following:
- On the context menu of
composer.json
, choose . - To uninstall a package listed in the default composer.json, choose on the main menu.
- On the context menu of
- In the Available Packages list, select the package to uninstall among the packages marked with a tick and click Remove.
When the package is uninstalled, IntelliJ IDEA updates the require
or the require-dev
section of composer.json
automatically.
Viewing the Composer Log
When you execute a Composer-related command via the IntelliJ IDEA interface, the command's output is displayed in the dedicated Composer Log console:
To open Composer Log, do any of the following:
- Having executed any Composer command for the first time, click the Show in log shortcut link in the Event Log notification message:
- Open the
composer.json
file in the editor and click the Show Log shortcut link on top of the editor panel.
If a Composer command has been executed successfully, Composer Log will fold its execution details.
To view the folded message, do any of the following:
- Click the plus icon in the left gutter to unfold the message.
- Hover the mouse over the ellipsis to view the message in a tooltip:
When appointing a default composer.json in a project, you can enable IntelliJ IDEA's settings synchronization with composer.json
. Composer Log will highlight such a change in the settings as it occurs. Click the Settings icon in the left gutter it to quickly navigate to the corresponding Directories, PHP, or Test Frameworks settings pages.
Running Additional Composer Commands
IntelliJ IDEA provides the user interface for a set of additional Composer commands. To run any of them, do one of the following:
- On the context menu of
composer.json
, choose . - Open
composer.json
in the editor, click on top of thecomposer.json
editor panel, and choose the desired command from the drop-down list. - To run a command for the default composer.json, choose on the main menu.
- To run commands via a globally installed
composer
executable, choose 'composer' executable and specify its location in the text box. -
To run commands via the locally installed composer.phar archive, choose composer.phar. Then, either specify the location of the existing
composer.phar
file in the text box or select the Download composer.phar from getcomposer.org checkbox to download a new instance of the file. Thecomposer.phar
file will be saved under the project root folder specified in the Location text box.Then, choose one of the configured local PHP interpreters from the PHP interpreter list. Refer to Configuring Local PHP Interpreters for details.
Validate
Run this command to perform several validity checks against the composer.json
file, which is recommended before committing composer.json
to a VCS. Validation results are displayed in the Composer Log console.
The equivalent Composer CLI command is validate
.
Before executing the command, choose the desired options in the Validation Settings dialog:
- Allow unbound version constrains: If selected, version constraints without an upper bound (e.g.
*
,>3.1
) will be permitted. Note that this will allow updates to any future version of a dependency, which may cause backward compatibility issues in the case of unexpected changes in dependencies.The equivalent option of the Composer CLI command is
--no-check-all
. - Check if composer.lock is up-to-date: If selected, the version constraints listed in
composer.lock
will be checked against the ones listed incomposer.json
. If the version constraints do not match, it is recommended to fix this by updating packages. After the packages are updated to the required versions, thecomposer.lock
and thecomposer.json
files will be synchronized.The equivalent option of the Composer CLI command is
--no-check-lock
. - Check if the package is Packagist-ready: If selected, the package's
composer.json
will be checked against the Packagist publishing requirements. Generally, for a package to be Packagist-ready, thename
,description
, andrequire
sections in itscomposer.json
file must be specified. Refer to the Packagist documentation for details on publishing packages.The equivalent option of the Composer CLI command is
--no-check-publish
. - Validate package dependencies: If selected, the
composer.json
files of all installed dependencies of the current package will also be validated.The equivalent option of the Composer CLI command is
--with-dependencies
.
Dump Autoloader
Run this command to generate the list of autoloaded classes. If the list already exists, it will be generated anew. This can be useful in situations when you introduce a new class in a project and need to include it to the list of autoloaded classes. Refer to the Composer documentation for more details on autoloading classes with Composer.
The equivalent Composer CLI command is dump-autoload
.
Before executing the command, choose the desired options in the Autoloader Generation Settings dialog:
- Skip scripts execution: If selected, the scripts defined in the
scripts
section of thecomposer.json
file are not executed.The equivalent option of the Composer CLI command is
--no-scripts
. - Optimize autoloader (Convert PSR-0/4 autoloader to a classmap): If selected, Composer will scan the given PSR-0/4 autoloading directories/files for the
php
andinc
files and generate a singleautoload_classmap
key-value array to speed up autoloading. Note that if the number of classes is large, running this command can be time consuming.The equivalent option of the Composer CLI command is
--optimize
. - Autoload classes from the classmap only: If selected, only the classes listed in the class map will be autoloaded. Composer will not search for fallback PSR-4 directories for classes not listed in the class map. Since this naturally requires an existing class map, Composer will also execute the Optimize autoloader (Convert PSR-0/4 autoloader to a classmap) command to generate one, which can be a time consuming process.
The equivalent option of the Composer CLI command is
--classmap-authoritative
. - Use APCu for caching found/not-found classes: If selected, caching via APCu will be enabled. When a class is found (or not found) in the class map, the corresponding record will be cached in APCu, so that it can be quickly retrieved on the next request.
The equivalent option of the Composer CLI command is
--apcu
. - Disable autoload-dev dependencies: If selected, the autoloaded namespaces and files listed in the
autoload-dev
section are not included into the autoloaded classes list.The equivalent option of the Composer CLI command is
--no-dev
.
Check for Changes in Dependencies
Run this command to check if you have any local changes in the code of the package dependencies. The equivalent Composer CLI command is status
.
Clear Cache
Run this command to delete the Composer's cache directory, which contains the downloaded packages. By default, the cache directory is set toC:\Users\<user>\AppData\Local\Composer
on Windows, and to ~/.composer/cache
on Linux and macOS. Refer to the Composer documentation for details. The equivalent Composer CLI command is clear-cache
.
List Licenses
Run this command to view the name, version, and license type of every installed package. The equivalent Composer CLI command is license
.
Diagnose
Run this command to perform automated checks for the most common Composer problems (e.g. connectivity issues, Composer/PHP version used, disk space available,composer.json
validity). If the problem is not identified, refer to the Composer troubleshooting guide. The equivalent Composer CLI command is diagnose
.
Self-Update
Run this command to update Composer itself (i.e. thecomposer.phar
file) to the latest version. The equivalent Composer CLI command is self-update
.
Running Composer from the command line
Command-line mode provides you with the full range of Composer commands. To use Composer in this mode, you need to configure it as an external command-line tool.
To integrate Composer with IntelliJ IDEA as an external command-line tool:
- Choose
On the Command Line Tool Support page that opens, click .
for Windows and Linux or for macOS. - In the Command Line Tools dialog that opens, choose Composer from the list. Using the Visibility option button, specify whether the tool will be available globally, i.e. in any IntelliJ IDEA project, or in the current project only:
Click OK to apply your changes.
- In the Command Line Tool Support: Composer dialog that opens, specify how you want to launch Composer by choose one of the options: composer.phar or php script or composer executable. Depending on your choice, specify the paths to the PHP installation folder and
composer.phar
or to thecomposer
executable file.IntelliJ IDEA parses the contents of the specified
.phar
archive or executable file for Composer commands. When the file analysis is completed, IntelliJ IDEA returns to the Command Line Tools Support page where the specified file is added to the list of command line tools available in IntelliJ IDEA.Click OK to apply your changes. The Composer tool will be added to the list of tools on the Command Line Tools Support page.
- Select the Enable checkbox next to the added tool, specify the alias to use in calls, customize the command set if necessary, and choose where to show the Input pane for running commands. Refer to How do I customize a tool? and Input pane location for details.
The Composer command-line tool is now added, and you can run commands with it. Open the Input pane (Tools | Run Command) and type <alias> (c by default) and press Ctrl+Space to invoke completion. The command execution result is displayed in the Output tab with the name of the command. Learn more from How do I run a command?.