Join my Laravel for REST API's course on Udemy 👀

Generate a new Laravel APP_KEY

April 23, 2022  ‐ 1 min read

One of the more important variables you'll find in your .env file after creating a new Laravel project is the APP_KEY. The APP_KEY is a random character string that is used, amongst other things, to encrypt cookies. It is not used to encrypt your passwords by the way, Laravel uses Hashing for this.

When you clone a repository, or when you deploy your Laravel project you'll probably find yourself in need to generate a new APP_KEY. Luckily there is an artisan that will generate a new APP_KEY for you and automatically set it in your .env file:

$ php artisan key:generate
Application key set successfully.

If you prefer to copy the key yourself somewhere you can use the --show flag to print the key to your terminal.

$ php artisan key:generate --show
base64:9IhKvpWXZ1tuhpN3ENFDLurQpEj4fngQvVr5KWu4rFo=

If you are on Heroku you can use this --show flag as well to directly set a environment variable.

$ heroku config:set APP_KEY=$(php artisan --no-ansi key:generate --show)
Setting APP_KEY and restarting ⬢ ... done, v13
APP_KEY: base64:9IhKvpWXZ1tuhpN3ENFDLurQpEj4fngQvVr5KWu4rFo=