Laravel "No application encryption key has been specified" in CI
Laravel needs a base64 APP_KEY to build the encrypter that protects cookies and sessions. In CI the .env has no key yet, so bootstrapping throws "No application encryption key has been specified." Generate a key or set APP_KEY before any artisan or test command runs.
What this error means
The first artisan or test command fails with "Illuminate\Contracts\Encryption\... RuntimeException: No application encryption key has been specified." APP_KEY is empty because .env was copied fresh and key:generate never ran.
In EncryptionServiceProvider.php line 68:
No application encryption key has been specified.Common causes
APP_KEY is empty after copying .env.example
CI runs cp .env.example .env, which ships an empty APP_KEY=. Nothing generated a key, so the encrypter cannot be constructed.
The key is set in .env but config was cached with the old value
If php artisan config:cache ran before APP_KEY was written, the cached config still has an empty key and env changes are ignored.
How to fix it
Generate the key after preparing .env
- Copy the example env file so a writable .env exists.
- Run
php artisan key:generateto write a fresh base64 key into it. - Run migrations and tests only after the key exists.
- run: cp .env.example .env
- run: php artisan key:generate
- run: php artisan migrate --env=testing --force
- run: php artisan testSet APP_KEY from a secret or literal env
You can skip key:generate by providing a base64 key directly in the job env, which is deterministic across runs.
env:
APP_KEY: base64:AckfSECRETkeyGeneratedOnceForCiXXXXXXXXXXXXXXX=How to prevent it
- Run
php artisan key:generateright aftercp .env.example .env. - Do not run
config:cachebefore APP_KEY is written. - For a stable key, set APP_KEY once in the CI env instead of regenerating.