Skip to content
Latchkey

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.

php artisan
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

  1. Copy the example env file so a writable .env exists.
  2. Run php artisan key:generate to write a fresh base64 key into it.
  3. Run migrations and tests only after the key exists.
.github/workflows/ci.yml
- run: cp .env.example .env
- run: php artisan key:generate
- run: php artisan migrate --env=testing --force
- run: php artisan test

Set 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.

.github/workflows/ci.yml
env:
  APP_KEY: base64:AckfSECRETkeyGeneratedOnceForCiXXXXXXXXXXXXXXX=

How to prevent it

  • Run php artisan key:generate right after cp .env.example .env.
  • Do not run config:cache before APP_KEY is written.
  • For a stable key, set APP_KEY once in the CI env instead of regenerating.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →