4

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?

rfay
  • 9,963
  • 1
  • 47
  • 89

2 Answers2

5

Updated 2023-08-07 with ddev composer command

There are several ways to run composer for your project.

  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 Windows OS, 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 exec 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
rfay
  • 9,963
  • 1
  • 47
  • 89
0

To expand on the accepted answer, DDEV now has a composer-specific hook.

hooks:
  post-start:
  - composer: install -d /var/www/html

The reason for using this instead of exec, I assume, is that there are also pre-composer and post-composer hooks, so maybe this also executes those hooks. I'm not sure of that or the actual difference, though.

mbomb007
  • 3,788
  • 3
  • 39
  • 68
  • The 2nd link is dead. The answer doesn't expand it, it's just copying the same code. – kenorb Jul 25 '23 at 15:00
  • @kenorb I updated the link to point to the current list of supported hooks. However, since the accepted answer is by rfay (a maintainer of DDEV), his answer is most likely to be the best-maintained one. – mbomb007 Jul 25 '23 at 15:37