IntelliJ IDEA 2017.2 Help

Composer Dependency Manager

This feature is supported in the Ultimate edition only.

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

IntelliJ IDEA integrates with the Composer Dependency Manager so you can install PHP-related frameworks right from the IDE.

IntelliJ IDEA provides a dedicated interface for initializing a project and adding dependencies. Note that in this mode, you can use Composer only in the current project.

To run any Composer command in any IntelliJ IDEA project, use the tool in the command-line mode, see How do I run Composer from the command line in IntelliJ IDEA? below.

Check Composer Support in PhpStorm for step-by-step guidance and examples.

Where do I get Composer from?

Option 1: Download Composer
Download composer.phar from the Composer Official website.

Option 2: Update Composer to the latest version
On the main menu, choose Tools | Composer | Self-Update. Learn more from the Composer Official website.

How do I initialize Composer in a IntelliJ IDEA project?

  1. Run Init Composer
    On the main menu, choose Tools | Composer | Init Composer. The Composer Settings Dialog opens.
  2. Specify the PHP interpreter to use
    Choose one of the configured local PHP interpreters from the list in the Execution area. See Configuring Local PHP Interpreters for details.
  3. Specify the composer.phar to use
    In the Execution area, specify the path to composer.phar. If you have not downloaded it yet, click the Download link and specify the folder to store composer.phar in.
  4. Complete the generated composer.json
    When you click OK, the Composer Settings dialog is closed, IntelliJ IDEA creates a stub of the composer.json file and opens it in the editor. Complete the code or accept the default values. For more details, see Composer.json: Project Setup.

How do I set up an external Composer project in IntelliJ IDEA?

  1. Open your project
    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 shows a notification:
    ps_composer_notification_composer_json_detected.png
  2. Initialize Composer
    Click Initialize. IntelliJ IDEA shows the Composer dialog where you need to specify the location of composer.json. If IntelliJ IDEA detects a configuration file, the Path to composer.json field is already filled in:
    ps_composer_path_detected.png
  3. Complete configuration for executing Composer commands
    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).

    Alternatively, if you have one composer.json in your project, choose Tools | Composer | Install on the main menu. IntelliJ IDEA shows the following dialog:

    ps_composer_complete_init_specify_php_composer_phar.png
    In either case, specify the PHP interpreter and the composer.phar to use. See How do I initialize Composer in a IntelliJ IDEA project? above.

  4. Install the dependencies

How do I appoint 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. 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. Open the Composer page
    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 only one Composer configuration file in your IntelliJ IDEA project, the Path to composer.json field is filled in automatically.
  3. Configure the open-for-edit status of Composer packages
    To protect packages under vendor/*/* against editing, leave the Add packages as libraries check box selected (this is the default setting).
    If you want to edit Composer packages under vendor/*/*, clear the Add packages as libraries check box.
  4. Enable settings synchronization
    Make sure the Synchronize IDE settings with composer.json check box 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. Learn more about PSR and autoload from the Composer official website. For examples and details in synchronizing settings, see PhpStorm blog.

How do I install dependencies?

You can install specific packages one-by-one, IntelliJ IDEA will update your composer.json automatically. Alternatively, you can list the packages you need in the require and require-dev sections and then install them all at once.
Learn more about adding dependencies from the Composer Official website.

Option 1: Install packages one-by-one

  1. Open the Manage Composer Dependencies dialog
    On the context menu of composer.json, choose Composer | Manage Dependencies.
  2. Choose the package to add
    Select the required package from the Available Packages list, possibly using 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 Composer command line options during installation, see https://getcomposer.org/doc/03-cli.md.
    Click Install to start installation. To leave 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 new package to the require or require-dev section of composer.json.

Option 2: Install all dependencies at once
On the context menu of composer.json, choose Composer | Install. IntelliJ IDEA installs all the packages listed in the require and require-dev sections.

How do I update dependencies?

You can update specific packages to the latest version one-by-one, IntelliJ IDEA will update your composer.json automatically. Alternatively, you can update all the packages listed in the require and require-dev sections at once.

Option 1: Update packages one-by-one

  1. Open the Manage Composer Dependencies dialog
    On the context menu of composer.json, choose Composer | Manage Dependencies.
  2. Choose the package to update
    Select the required package from the Available Packages list among the packages marked with a tick and click Update.

Option 2: Update all dependencies at once
On the context menu of composer.json, choose Composer | Update. IntelliJ IDEA updates all the packages listed in the require and require-dev sections.

How do I uninstall a dependency?

  1. Open the Manage Composer Dependencies dialog
    On the context menu of composer.json, choose Composer | Manage Dependencies.
  2. Choose the package to uninstall
    In the Available Packages list, select the package to uninstall among the packages marked with a tick and click Remove.
    When the uninstallation is completed, IntelliJ IDEA updates the require or require-dev section of composer.json automatically.

How do I generate a Composer project stub?

  1. Open the New Project dialog
    Choose File | New | Project or click Create New Project on the Welcome screen.
  2. 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.
  3. Choose composer.phar to use
    • To use commands from a previously downloaded composer.phar, choose Use existing composer.phar and specify its location in the text box.
    • To download a new instance of Composer, choose Download composer.phar from getcomposer.org. The composer.phar file will be saved under the project root folder specified in the Location text box.
  4. Specify PHP interpreter to use
    Choose one of the configured local PHP interpreters from the list, see Configuring Local PHP Interpreters for details.
  5. Specify the package to install during the project creation
    Select the package to add from the Available Packages list, possibly using the search field, and choose the relevant version from the Version to install list.
  6. 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 Composer command line options during installation, see https://getcomposer.org/doc/03-cli.md.

When you click Finish, the create-project command is invoked with the selected package. This results in creating a Composer project whose configuration and structure depends on the selected package, see https://getcomposer.org/doc/03-cli.md for details. After that a IntelliJ IDEA project opens.

How do I run Composer from the command line in IntelliJ IDEA?

In the command-line mode the full range of Composer command is at your disposal. To use Composer in this mode, you need to configure it as an external command-line tool.

Step 1: Integrate Composer with IntelliJ IDEA as an external command-line tool

  1. Open the Composer dialog
    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 and choose Composer from the list.
  2. Specify how you want to launch Composer
    Choose one of the options composer.phar or php script or composer executable. Depending on your choice, specify the paths to PHP installation folder and composer.phar or to composer executable file.

    IntelliJ IDEA parses the contents of the specified .phar archive or executable file for Composer commands. When the file analyses 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.

  3. Complete Composer configuration
    On the Command Line Tools Support page where Composer is added to the list of tools, select the Enable check box, specify the alias to use in calls, customize the command set if necessary, and choose where to show the Input pane for running commands. See How do I customize a tool? and Input pane location for details.

Step 2: Initialize a Composer project
Open the Input pane (Tools | Run Command), type с init at the command prompt, and answer the questions of the wizard. Learn more from the Composer Official website.

Step 3: Run Composer commands
Open the Input pane (Tools | Run Command) and type <alias> (c by default) and press Ctrl+Space to invoke completion. The result of command execution is shown in the Output tab with the name of the command. Learn more from How do I run a command?.

Last modified: 29 November 2017

See Also