phplaraveldockerphpredis

Laravel Redis class not found in sail installation


I discovered an issue with our Laravel project during a fresh installation. When I cloned the entire project and created the env file, I proceeded to install Sail containers and the existing Composer package file by executing the following command as per the official Sail documentation:

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

After running the above command, I encountered this error message:

Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

   Error

  Class "Redis" not found

  at vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:81
     77▕      * @throws \LogicException
     78▕      */
     79▕     protected function createClient(array $config)
     80▕     {
  ➜  81▕         return tap(new Redis, function ($client) use ($config) {
     82▕             if ($client instanceof RedisFacade) {
     83▕                 throw new LogicException(
     84▕                     extension_loaded('redis')
     85▕                         ? 'Please remove or rename the Redis facade alias in your "app" configuration file in order to avoid collision with the PHP Redis extension.'

      +19 vendor frames

  20  [internal]:0
      Illuminate\Foundation\Application::Illuminate\Foundation\{closure}(Object(Laravel\Telescope\TelescopeServiceProvider))
      +5 vendor frames

  26  artisan:35
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1

I suspect that laravelsail/phpxx-composer image does not have the Redis extension installed, but I'm unsure how to address this issue.

For reference, the Laravel version is v10, we are using phpredis, and our PHP version is v8.2.


Solution

  • Your .env file breaks the installation. They intended this command to be run on clear repo clone where .env file should not be committed as a good practice. To install vendor packages you don't need this configuration, just like --ignore-platform-reqs skips all extensions checks on Laravel side config cannot rely on them (here Redis) either.

    IMO simplest fix is to add -e "CACHE_DRIVER=file" \ to the docker command that you took from Laravel docs:

    docker run --rm \
        -e "CACHE_DRIVER=file" \
        -u "$(id -u):$(id -g)" \
        -v "$(pwd):/var/www/html" \
        -w /var/www/html \
        laravelsail/php82-composer:latest \
        composer install --ignore-platform-reqs
    

    Your millage may vary. If you use other config options / deps, they may need additional configuration resets. With long list of those you may consider --env-file docker options to provide an empty file