ddev

How can I run composer with ddev?


I need to run composer on my ddev project and don't have it on my Windows machine. For example, the project requires a composer install before startup. How can I use composer in this environment, especially on Windows?


Solution

  • Updated 2023-08-07 with ddev composer command

    There are several ways to run composer for your project, see docs

    1. DDEV provides the ddev composer and ddev composer create commands. These run composer inside the container, so you're guaranteed to get composer behavior that matches the in-container hosting environment.
      • ddev composer require swiftmailer/swiftmailer
      • ddev composer update
      • ddev composer install
      • ddev composer create drupal/recommended-project

    Note that ddev composer create is not exactly the same as composer create-project so you don't have to understand complexities of the underlying filesystem. There are many examples in the docs.

    Nothing here prevents you from using any composer technique that you're comfortable with, but this is a great way to get predictable on-linux in-container composer builds. It should be hugely important for people using traditional Windows, where composer is less available and has some unpredictable behavior.

    1. All the normal composer behavior has always been installed inside your web container, so you can use that whether or not you have composer on your host computer. For example: ddev composer install -d /var/www/html will do a composer install in the root of your repository, exactly the same as ddev composer install. You can also do ddev ssh and operate on the command line in the container.
    2. Try this hooks approach to running composer install inside the container (on the mounted partition) every time your project starts:
        hooks:
          post-start:
          - composer: install