Question

Fix “No Application Encryption Key Has Been Specified” in Laravel

phplaravellaravel-5laravel-artisan

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 .env file yet.
  • .env exists, but APP_KEY is blank.
  • The configuration cache still contains an old empty key.
  • The server environment has not been given an APP_KEY value.

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.

Take Quiz

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
  1. cp .env.example .env creates the local environment file Laravel expects. The .env.example file is a template and should not contain the real secret key.
  2. php artisan key:generate creates a secure random value and places it in APP_KEY in .env.
  3. php artisan config:clear removes any cached configuration that might still contain an empty APP_KEY.
  4. php artisan serve starts PHP's local development server.
  5. 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.

Take Quiz

Real Codebase Usage

In a real Laravel codebase, developers usually follow these practices:

  • Keep .env out of Git by listing it in .gitignore.
  • Commit .env.example with placeholder values so teammates know which variables are required.
  • Generate a unique key for each new local installation:
php artisan key:generate
  • Set APP_KEY as 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_KEY on 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 .env value. Clear it:

php artisan config:clear

Then, if appropriate for production, rebuild it:

php artisan config:cache

Regenerating a production key to “fix” the error

Do not casually run php artisan key:generate on an existing production system. A new key can make existing encrypted data unreadable. Restore or configure the original key when possible.

Comparisons

ItemPurposeShould it be committed to Git?
.envReal environment-specific values, including secretsNo
.env.exampleSafe template listing required variablesYes
APP_KEYLaravel's application encryption keyNo
php artisan key:generateGenerates and stores a new local application keyUse for new installations
php artisan config:clearRemoves cached configurationUse after environment/config changes
php artisan config:cacheBuilds a configuration cache for deployment performanceUsually production only

php artisan serve and php artisan key:generate solve different problems:

  • serve starts a local web server.
  • key:generate configures the encryption key Laravel needs while handling requests.

Starting the server successfully does not prove that all required application configuration is present.

Take Quiz

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 .env and APP_KEY secret.
  • 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.

Should I commit .env to Git?

No. .env often contains secrets. Commit .env.example with placeholder values instead.

Take Quiz

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.

Take Quiz

Keep learning

Related questions

View more