Different output from WP-CLI in bash vs PHP exec()

According to Alain Schlesser in wordpress slack:

The folder that the packages are installed into is set to use your server user’s home folder. When you try to execute this command from PHP, however, it is being executed by the webserver’s user, which probably has a different home folder, and the package is not installed in there.

You can either load the package manually as part of the system (requiring it in the site’s Composer stack, for example), or set the package folder to a location that is shared by both your user and your webserver (and reinstall it afterwards).

You can set the packages folder with the WP_CLI_PACKAGES_DIR environment variable: https://make.wordpress.org/cli/handbook/config/#environment-variables