IntelliJ IDEA 2017.3 Help

Composer Dependency Manager

This feature is only supported in the Ultimate edition.

The following is only valid when PHP Plugin is installed and enabled!

IntelliJ IDEA integrates with the Composer Dependency Manager, which allows you to declare your project's dependencies and manage them right from the IDE.

IntelliJ IDEA provides a dedicated user interface for the common Composer commands:

When executing these commands, you can view the output via the Composer Log console.

To use the full range of Composer commands in a IntelliJ IDEA project, you need to configure and run it as an external command-line tool.

Downloading and installing Composer

You can download and install Composer manually as described in the Composer official documentation.

Alternatively, Composer can be downloaded and installed automatically when you create a new Composer project or initialize Composer in an existing IntelliJ IDEA project.

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:

  1. Choose File | New | Project or click Create New Project on the Welcome screen.

    The New Project dialog opens:

    ps new composer project
  2. 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 through the PHP interpreter, 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. The composer.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.

      Optionally, in the Command line parameters text box, type the additional command line parameters. For example, to have a package added to the require-dev section instead of the default require section, type --dev. For more information about using the Composer command line options during installation, refer to the Composer official documentation.
    • 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.
  3. 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 may contain other metadata.

To initialize Composer in an existing project:

  1. On the main menu, choose Tools | Composer | Init Composer.

    The Composer Settings Dialog opens:

    ps composer settings dialog
  2. 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 through the PHP interpreter, 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. The composer.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.

  3. Click OK to close the Composer Settings dialog. IntelliJ IDEA creates a stub of the composer.json file and opens it in the editor:

    ps composer json stub

    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.

  1. Click Open on the Welcome screen or choose File | Open on the main menu, then choose the folder where your Composer project is stored.

    IntelliJ IDEA will show a notification:

    ps_composer_notification_composer_json_detected.png
  2. Click Initialize Composer settings. In the Composer dialog that opens, specify the location of the composer.json file. If IntelliJ IDEA detects an existing file, the Path to composer.json field will be filled automatically:
    ps_composer_path_detected.png
  3. 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 Add packages as libraries checkbox selected. Otherwise, if you want to edit Composer packages under vendor/*/*, clear the Add packages as libraries checkbox.

    • Make sure the Synchronize IDE settings with composer.json checkbox is selected to automatically detect the PHP language level and configure project Source and Test roots based on the configuration from composer.json.

      IntelliJ IDEA is aware of PSR-0/PSR-4 source roots and of their namespace prefixes declared in the autoload and autoload-dev sections in composer.json. IntelliJ IDEA also detects the PHP language level based on the php setting in the require 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 autoload, 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.

  4. Set up Composer commands execution:
    • Do one of the following:
      • Open the Composer page (File | Settings | Languages and Frameworks | PHP | Composer for Windows and Linux or IntelliJ IDEA | Preferences | Languages and Frameworks | PHP | Composer for macOS).
      • If you have a single composer.json file in your project, choose Tools | Composer | Install 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 through the PHP interpreter, 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. The composer.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.

  5. Install the project dependencies:

    You can also open the composer.json file in the code editor and click the Install shortcut link on top of the editor panel.

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 Tools | Composer on the main menu.

To appoint a default composer.json:

  1. Choose File | Settings | Languages and Frameworks | PHP | Composer for Windows and Linux or IntelliJ IDEA | Preferences | Languages and Frameworks | PHP | Composer for macOS.
  2. Specify the location of the default composer.json. If you have a single Composer configuration file in your IntelliJ IDEA project, the Path to composer.json field is filled automatically.
  3. 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 Add packages as libraries checkbox selected.

    Otherwise, if you want to edit Composer packages under vendor/*/*, clear the Add packages as libraries checkbox.
  4. Make sure the Synchronize IDE settings with composer.json checkbox is selected to automatically detect the PHP language level and configure project Source and Test roots based on the configuration from composer.json.

    IntelliJ IDEA is aware of PSR-0/PSR-4 source roots and of their namespace prefixes declared in the autoload and autoload-dev sections in composer.json. IntelliJ IDEA also detects the PHP language level based on the php setting in the require 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 autoload, refer to the Composer official website. For examples and details on synchronizing settings, refer to the PhpStorm blog post.

Managing dependencies

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.

Learn more about adding dependencies on the Composer Official website.

Installing a specific package

  1. Do one of the following:
    • On the context menu of composer.json, choose Composer | Manage Dependencies.
    • To install a package from the default composer.json, choose Tools | Composer | Manage Dependencies on the main menu.
  2. In the Manage Composer Dependencies dialog that opens, select the required package from the Available Packages 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.

  3. Optionally, customize the package installation. Expand the Settings hidden area and specify the advanced installation options. 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 instead of the default require section, type --dev. For more information about using the Composer command line options during installation, refer to the Composer official documentation. Click Install to start installation. To exit the dialog, click Close.

When the installation is completed, IntelliJ IDEA creates a new subfolder under vendor, stores the new package in this subfolder, and adds the corresponding package record to the require or require-dev section of composer.json.

Installing all packages at once

Do any of the following:

  • On the context menu of composer.json, choose Composer | Install.
  • 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 Tools | Composer | Install 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

  1. Do one of the following:
    • On the context menu of composer.json, choose Composer | Manage Dependencies.
    • To update a package from the default composer.json, choose Tools | Composer | Manage Dependencies on the main menu.
  2. 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 Composer | Update.
  • 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 Tools | Composer | Install 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:

  1. Do any of the following:
    • On the context menu of composer.json, choose Composer | Manage Dependencies.
    • To uninstall a package listed in the default composer.json, choose Tools | Composer | Manage Dependencies on the main menu.
  2. 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.

Composer Log

When you execute a Composer-related action via the IntelliJ IDEA interface, the corresponding command's output is displayed in the dedicated Composer Log console:

ps composer log panel

To open Composer Log, do any of the following:

  • Open the composer.json file in the editor and click the Show Log shortcut link on top of the editor panel.
  • After executing a command, click the Show in log shortcut link in the Event Log notification message with the command execution result:
    ps composer event log message

Useful tips

  • Composer Log folds the details of successfully executed commands. To view the folded message, either click the plus icon in the left gutter to unfold it or hover them mouse over the ellipsis to view the execution details in a tooltip:
    ps composer folded message hover
  • To re-run a command, click the Run icon run 2x gutter in the left gutter.
  • 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 icon settings in the left gutter it to quickly navigate to the corresponding , PHP, or Test Frameworks settings pages.
  • Click the link to the composer.json file in the log to open it in the editor.

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:

  1. Choose File | Settings | Tools | Command Line Tool Support for Windows and Linux or IntelliJ IDEA | Preferences | Tools | Command Line Tool Support for macOS.

    On the Command Line Tool Support page that opens, click new.

  2. 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:
    ps cli tool composer

    Click OK to apply your changes.

  3. 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 the composer 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.

  4. 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?.

Last modified: 22 January 2018

See Also