17

I would like to offer users of my composer package an interface to select and install any of the suggested packages. There seems to be no command line option, and in the API I can only find a getSuggests() method that lists the suggested packages.

Is there any way (native or with a third party installer), to give the user the choice to select suggested packages?

7 Answers 7

24
composer suggests | xargs -i composer require {}
6
  • I assume this is for linux only and a quick test proved it doesn't work on windows. Windows doesn't know xargs
    – davejal
    Feb 13, 2017 at 0:29
  • Unix, not Linux only. May 4, 2017 at 15:19
  • In my case I was installing global packages, so I used: composer global suggests | xargs -I '{}' composer global require '{}'
    – robrecord
    Feb 8, 2018 at 11:55
  • [LogicException] No lockfile found. Unable to read locked packages suggests [--by-package] [--by-suggestion] [--no-dev] [--] [<packages>]...
    – John Dee
    May 26, 2018 at 10:58
  • @davejal options inlcude installing Bash on Windows 10 from Developer Features, and installing cygwin on any version of windows.
    – user151841
    May 9, 2019 at 13:35
8
+50

As far as I know, there is no way to achieve this with common composer functionality.

You can write your own scripts to be executed at post-package-install. But Scripts are only executed if defined in the root package's composer.json (more information on how to use scripts see here). Scripts defined in dependencies are not executed for security concerns (there was a discussion on the github some time ago about this).

But maybe a composer-plugin fits your needs. Plugins are used to expand composer's functionality (more information about plugins see here).

As a simple alternative way, I suggest to define a suggested package message like, if you need XY functionality run: php composer.phar require vendor/package:2.* and the user then can use this command to install it. Not as comfortable as you requested but still easy enough for most users I think.

8

Try

composer suggests

Add the flag -v to make it verbose.

This won't install anything, it will just list out all the suggestions. However, you can pipe it to composer require and get the desired output.

8
composer suggests | xargs -L 1 composer require

Should work from windows git bash.

6

This one did it for me :)

composer suggests | xargs -I '{}' composer require '{}'

Hope it will help :)

2

I had to add the --list flag on composer suggests otherwise there were errors piping to xargs

composer suggests --list | xargs -i composer require {}
1

windows

for /F  %a in ('composer suggests --all --list') do @echo composer require %a

then mark lines and copy paste into commandline

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.