Question
Fix “No Application Encryption Key Has Been Specified” in Laravel
Question
I started the Laravel development server with the following Artisan command:
php artisan serve
Artisan reports that the server is running:
Laravel development server started: http://127.0.0.1:8000
However, opening http://127.0.0.1:8000 in a browser produces this error:
RuntimeException
No application encryption key has been specified.
What causes this Laravel error, and how can it be fixed? I am using Laravel 5.5-dev.
Short Answer
Laravel requires an application encryption key, stored as APP_KEY, to securely encrypt and decrypt values such as cookies and session data. You will learn how to generate the key, ensure Laravel reads it from .env, and handle configuration caching safely.
Concept
Laravel uses a single application key to encrypt sensitive application data. This key is normally stored in the project's .env file:
APP_KEY=base64:...
When Laravel boots, it reads APP_KEY from the environment. If the value is missing or empty, Laravel cannot safely encrypt or decrypt data, so it throws:
No application encryption key has been specified.
This commonly happens when:
- A newly cloned project has no
.envfile yet. .envexists, butAPP_KEYis blank.- The configuration cache still contains an old empty key.
- The server environment has not been given an
APP_KEYvalue.
The standard fix for a new local project is:
php artisan key:generate
This creates a cryptographically secure key and writes it to .env. The key must remain stable after an application is in use, because changing it prevents Laravel from decrypting values created with the previous key.
Mental Model
Think of APP_KEY as the master key for a locked filing cabinet.
Laravel puts protected information—such as encrypted cookies—into the cabinet and locks it using this master key. Later, it needs the same key to open the cabinet again. If no master key was supplied, Laravel cannot safely lock or unlock anything, so it stops rather than continuing insecurely.
For a new application, generating a key gives it its own master key. For an existing application, replacing the key is like replacing the cabinet lock: old items locked with the previous key may no longer open.
Syntax and Examples
Generate the key for the current Laravel application:
php artisan key:generate
A successful command usually reports:
Application key set successfully.
Laravel updates the APP_KEY line in .env:
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:exampleGeneratedKeyValue
APP_DEBUG=true
APP_URL=http://localhost
If the project has no .env file, create one from Laravel's example file first:
cp .env.example .env
php artisan key:generate
On Windows Command Prompt, use:
copy .env.example .env
php artisan key:generate
On PowerShell, use:
Copy-Item .env.example .env
php artisan key:generate
The base64: prefix is expected. Do not remove it or manually invent a short password-like key.
Step by Step Execution
Consider this local setup process:
cp .env.example .env
php artisan key:generate
php artisan config:clear
php artisan serve
cp .env.example .envcreates the local environment file Laravel expects. The.env.examplefile is a template and should not contain the real secret key.php artisan key:generatecreates a secure random value and places it inAPP_KEYin.env.php artisan config:clearremoves any cached configuration that might still contain an emptyAPP_KEY.php artisan servestarts PHP's local development server.- When a browser requests the application, Laravel loads the configuration, finds
APP_KEY, initializes its encryption service, and can process encrypted cookies and session values.
If step 2 is skipped, Laravel reaches its encryption setup without a key and throws the runtime exception.
Real World Use Cases
APP_KEY supports features that need trusted encryption or signing in Laravel applications:
- Session cookies: Laravel can protect session-related cookie data from being read or altered by users.
- Remember-me authentication: Authentication cookies require secure protection.
- Encrypted values: Applications may use Laravel's encryption tools for tokens, credentials, or other secrets.
- Signed URLs and data integrity: Framework features can rely on application secrets to detect tampering.
- Multiple environments: Local, staging, and production deployments should each receive their own securely managed environment configuration.
For local development, a generated .env key is sufficient. For production, provide the key through secure deployment configuration or a secrets manager.
Real Codebase Usage
In a real Laravel codebase, developers usually follow these practices:
- Keep
.envout of Git by listing it in.gitignore. - Commit
.env.examplewith placeholder values so teammates know which variables are required. - Generate a unique key for each new local installation:
php artisan key:generate
- Set
APP_KEYas a protected environment variable in production rather than committing it to source control. - Clear and rebuild configuration caches during deployments when environment values change:
php artisan config:clear
php artisan config:cache
- Treat key rotation carefully. Changing
APP_KEYon a running application can invalidate encrypted cookies, sessions, and any stored encrypted data. Plan migrations or user reauthentication when rotation is necessary.
A typical deployment check is to fail early when required environment values, including APP_KEY, are absent. This prevents an application from starting in an insecure or broken state.
Common Mistakes
Running key:generate before creating .env
If .env does not exist, Laravel may not have a file to update. Create it from the example first:
cp .env.example .env
php artisan key:generate
Editing .env.example instead of .env
This does not configure the running application. Laravel reads .env, while .env.example is only a shareable template.
Leaving an empty key
This is invalid:
APP_KEY=
Generate a proper key rather than adding a predictable string.
Manually copying a key with extra quotes or spaces
Avoid unnecessary formatting such as:
APP_KEY= "base64:..."
Use the exact generated line. If you must set an environment variable in a hosting dashboard, copy the value carefully according to that platform's instructions.
Forgetting configuration cache
Laravel may use cached configuration instead of a newly edited value. Clear it:
Comparisons
| Item | Purpose | Should it be committed to Git? |
|---|---|---|
.env | Real environment-specific values, including secrets | No |
.env.example | Safe template listing required variables | Yes |
APP_KEY | Laravel's application encryption key | No |
php artisan key:generate | Generates and stores a new local application key | Use for new installations |
php artisan config:clear | Removes cached configuration | Use after environment/config changes |
php artisan config:cache | Builds a configuration cache for deployment performance |
Cheat Sheet
# New clone or fresh Laravel project (macOS/Linux)
cp .env.example .env
php artisan key:generate
php artisan serve
# If Laravel still reports a missing key
php artisan config:clear
# Typical production cache workflow after environment variables are set
php artisan config:cache
# Required in .env or the hosting environment
APP_KEY=base64:generatedSecureValue
- Generate the key with
php artisan key:generate. - Keep
.envandAPP_KEYsecret. - Do not commit the real key to version control.
- Do not change an established production key without planning for existing encrypted data.
- Clear configuration cache after changing environment configuration.
FAQ
Why does Laravel need APP_KEY?
Laravel uses it to encrypt and decrypt protected application values, including cookies and other encrypted data.
What command fixes “No application encryption key has been specified”?
For a new installation, run:
php artisan key:generate
If .env is missing, create it from .env.example first.
Where is the Laravel application key stored?
Usually in the APP_KEY entry of the project's .env file. In production, it may be supplied by the hosting platform as an environment variable.
Why does the error remain after I added APP_KEY?
Cached configuration may still contain the old value. Run:
php artisan config:clear
Can I use the same APP_KEY in local and production environments?
It is safer to use separate keys for separate environments. Each environment should have its own securely managed configuration.
Is it safe to run php artisan key:generate in production?
Not casually. Replacing the key can invalidate sessions, cookies, and stored encrypted data. Prefer restoring the existing production key if it was accidentally omitted.
Mini Project
Description
Set up a newly cloned Laravel application so it has local environment configuration and can start without the missing encryption key exception. This mirrors a common onboarding task when joining a Laravel project.
Goal
Create a local .env file, generate a valid Laravel APP_KEY, clear stale configuration, and start the development server.
Requirements
Create .env from the repository's .env.example template.
Generate an application encryption key using Artisan.
Confirm that .env contains a non-empty APP_KEY value.
Clear cached configuration before starting the server.
Do not add .env or its key to version control.
Keep learning
Related questions
Are PDO Prepared Statements Enough to Prevent SQL Injection in PHP?
Learn how PDO prepared statements prevent SQL injection in PHP, what they protect, and the mistakes that still leave MySQL apps vulnerable.
Can You Bind an Array to an IN Clause in PHP PDO?
Learn how PDO handles placeholders in IN() clauses, why arrays cannot be bound directly, and the safe PHP pattern to build dynamic queries.
Choosing the Right MySQL Collation for PHP and UTF-8
Learn how MySQL character sets and collations work with PHP, and how to choose a practical UTF-8 setup for web applications.