Skip to content
FR
  1. LuzineDebug
  2. PHP & Laravel
  3. No application encryption key has been specified…
PHP & Laravel Beginner ≈ 5 min to fix

Fix "No application encryption key has been specified" in Laravel

Error message
No application encryption key has been specified.
  • Illuminate\Encryption\MissingAppKeyException: No application encryption key has been specified.
  • RuntimeException: No application encryption key has been specified.
  • Unsupported cipher or incorrect key length. Supported ciphers are: aes-128-cbc, aes-256-cbc, aes-128-gcm, aes-256-gcm.
In short

Laravel’s APP_KEY variable is empty or missing, so Laravel cannot encrypt cookies or sessions. Create the .env file if it is missing, run php artisan key:generate, then php artisan config:clear so the new key is picked up.

Be the first to confirm this fix

What this error means

Laravel encrypts cookies, the session and everything going through Crypt or encrypted casts with a secret key read from config('app.key'), which comes from the APP_KEY variable. The first time encryption is needed, often on the very first web request because of the session cookie, the encrypter is created. If the key is empty, Laravel throws MissingAppKeyException with this message. If the key exists but has the wrong length (32 bytes for aes-256-cbc, the default cipher) or lost its base64: prefix, you get "Unsupported cipher or incorrect key length". The key must be generated once per environment and never committed.

Common causes

  1. Project cloned without .env: the .env file is not versioned, and key:generate only runs automatically with composer create-project, not after a git clone.
  2. Empty APP_KEY: .env was copied from .env.example, where the key is intentionally empty.
  3. Cached configuration: php artisan config:cache ran before the key existed, and Laravel ignores .env until the cache is cleared.
  4. Variable missing in production or Docker: the platform (container, CI, Forge, Kubernetes) does not provide APP_KEY in its environment variables.
  5. Malformed key: base64: prefix removed, extra spaces or quotes, or a key truncated by a copy-paste.

Step-by-step fix

  1. Create the .env file

    At the project root, copy the template if there is no .env yet. Do not overwrite it if it already exists.

    bash
    # macOS / Linux
    cp -n .env.example .env
    
    # Windows (PowerShell)
    if (-not (Test-Path .env)) { Copy-Item .env.example .env }
  2. Generate the key

    The command creates a random key and writes it to APP_KEY=base64:... in .env. In production, it asks for confirmation: never regenerate a key that is already in use, or existing sessions, cookies and encrypted data become unreadable.

    bash
    php artisan key:generate
  3. Clear the config cache

    If the configuration was cached, Laravel still reads the old empty value. Clear the cache and reload the page.

    bash
    php artisan config:clear
    # or everything at once
    php artisan optimize:clear
  4. Production and Docker: provide the key through an environment variable

    Generate a key without changing any file with --show, then store it in your platform’s secrets (container, Forge or CI environment variables). Use the same key for every instance of the same application, then run php artisan config:cache again.

    bash
    php artisan key:generate --show
    # base64:3q2+7w...=  -> store it as the APP_KEY secret
  5. Check the key format

    The line must start with base64:, with no extra spaces or quotes. For aes-256-cbc, the part after base64: decodes to 32 bytes.

    ini
    APP_KEY=base64:REPLACE_WITH_THE_GENERATED_VALUE
    APP_CIPHER=AES-256-CBC
  6. Tests: add a key to the test environment

    If a .env.testing file exists, Laravel uses it instead of .env during tests: it needs an APP_KEY too. You can generate it directly for that environment.

    bash
    php artisan key:generate --env=testing

Did this fix work for you?

Confirm it in one click: it will rise for the next developers stuck on the same error.

Keep it from coming back

  • Add php artisan key:generate to the project setup script (README, composer.json or Makefile) for every new machine.
  • Store APP_KEY in a secrets manager in production and never commit .env.
  • To rotate the key without breaking sessions, use APP_PREVIOUS_KEYS (Laravel 11+), which lets Laravel decrypt with the old key during the transition.

Frequently asked questions

Can I regenerate APP_KEY in production?

Only if you accept logging out every user and losing data encrypted with the old key. Otherwise, put the old key in APP_PREVIOUS_KEYS before replacing it.

I ran key:generate but the error remains, why?

The configuration is probably cached: run php artisan config:clear. Also check that the web server reads the same .env (same folder, same container).

Should I commit APP_KEY to the repository?

No. The key lets anyone decrypt cookies and forge sessions. Each environment has its own key, kept out of the repository.

Useful tools for this error

More PHP & Laravel errors

All PHP & Laravel errors →