Search code examples
phplaravelcomposer-phpapple-m1laravel-sail

Larvel Sail: Installing Composer Dependencies For Existing Applications on Apple Silicon M1 Fails


I cloned an existing application on a machine with Apple's M1 processor. Then used following command to bring it (Ref).

docker run --rm \
    -u "$(id -u):$(id -g)" \
    -v $(pwd):/opt \
    -w /opt \
    laravelsail/php74-composer:latest \
    composer install --ignore-platform-reqs

Then also tried:

docker run --rm --platform linux/amd64 \
    -u "$(id -u):$(id -g)" \
    -v $(pwd):/opt \
    -w /opt \
    laravelsail/php74-composer:latest \
    composer install --ignore-platform-reqs

Both of these commands fail with same message:

> @php artisan package:discover --ansi

   Doctrine\DBAL\Driver\PDO\Exception

  could not find driver

  at vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDO/Exception.php:18
     14▕ final class Exception extends PDOException
     15▕ {
     16▕     public static function new(\PDOException $exception): self
     17▕     {
  ➜  18▕         return new self($exception);
     19▕     }
     20▕ }
     21▕

      +1 vendor frames
  2   [internal]:0
      Illuminate\Foundation\Application::Illuminate\Foundation\{closure}(Object(App\Providers\AppServiceProvider))

      +18 vendor frames
  21  app/Providers/AppServiceProvider.php:94
      Illuminate\Support\Facades\Facade::__callStatic("listen")
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1

However, I can execute php artisan package:discover --ansi command from both host machine and inside container:

./vendor/bin/sail artisan package:discover --ansi
["select * from `data_types`",[],79.14]
Discovered Package: arrilot/laravel-widgets
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: fruitcake/laravel-cors
Discovered Package: intervention/image
Discovered Package: kreait/laravel-firebase
Discovered Package: larapack/doctrine-support
Discovered Package: larapack/voyager-hooks
Discovered Package: laravel/sail
Discovered Package: laravel/tinker
Discovered Package: laravel/ui
Discovered Package: lexxyungcarter/chatmessenger
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Discovered Package: tcg/voyager
Package manifest generated successfully.

Any help with this issue is appreciated.


Solution

  • When you use the following command, only one pretty specific container is started:

    docker run --rm \
        -u "$(id -u):$(id -g)" \
        -v $(pwd):/opt \
        -w /opt \
        laravelsail/php74-composer:latest \
        composer install --ignore-platform-reqs
    

    The container contains everything that is needed to run Composer - but not much more. It might not contain all PHP extensions needed to run your application, nor will the command start other containers that provide dependencies for your application, like a database container.

    If you solely run the steps needed to install your application's dependencies, everything will work. But if you also use Composer's script functionalities, this can cause problems, as some scripts might expect that any PHP extension is installed in the runtime environment, or any depending container is also started.

    In your case, at least one PHP extension to run your application is missing:

    > @php artisan package:discover --ansi
    
       Doctrine\DBAL\Driver\PDO\Exception
    
      could not find driver
    

    That script tries to open a database connection, but can not find the extension that is needed for this connection, as laravelsail/php74-composer does not contain any database driver.


    Solution: run composer install with the additional flag --no-scripts.