PromptHub
Laravel Beginner 14 views

Laravel Missing Application Encryption Key

The APP_KEY is not set or is invalid, preventing encryption operations.

Explanation

This error occurs when Laravel's APP_KEY environment variable is not set or is invalid. The encryption key is required for many core Laravel features including session encryption, CSRF token generation, cookie encryption, and queued job encryption. Without a valid key, most application functionality will fail with various errors. The key is generated using php artisan key:generate and should be a 32-character string prefixed with base64:. Never commit the APP_KEY to version control.

Common Causes

  • APP_KEY missing from .env file
  • Key not generated during setup
  • .env file not loaded correctly
  • Key changed without clearing cache
  • Docker container missing env variable

Solution

Run php artisan key:generate to generate a new encryption key. Ensure the APP_KEY value in your .env file is present and starts with base64:. Never change the key in production without clearing all sessions, cache, and re-encrypting data. After generating a new key, run php artisan config:clear. Verify the key is set by checking config('app.key'). If deploying to a new environment, copy the APP_KEY from production or generate a new one and clear cached configs.

Code Example

// Generate a new key
php artisan key:generate

// .env should contain:
APP_KEY=base64:SomeRandomStringOfExactly32Characters==

// Check key is set in tinker
>>> config('app.key');
// Should return the key string

// Common mistake: key missing from .env
// This causes: RuntimeException: No application encryption key has been specified

// After changing key, clear cache
php artisan config:clear
php artisan cache:clear

// For Docker/deployments, generate key in entrypoint
#!/bin/sh
php artisan key:generate --force
php artisan config:cache
php artisan migrate --force

Error Information

Language

php

Framework

laravel

Difficulty

Beginner

Views

14

Related Errors