Create a Laravel 8 application from scratch

Create a Laravel 8 application

In this tutorial, we will explore how to create a Laravel 8 application from scratch and then prepare your application for development. However, the overall process changed from the previous version (Laravel 7), improving the developer experience with a fresh set of tools. Now, the new Laravel 8 provides Sail, a built-in solution for running your Laravel project using Docker.

System Requirements

Docker Desktop

To create a Laravel 8 application, you will need a system capable of running Docker Desktop for development. The basic requirements for Windows are:

  • Windows 10 64-bit: Pro, Enterprise, or Education (Build 16299 or later). For Windows 10 Home, see Install Docker Desktop on Windows Home.
  • 64 bit processor with Second Level Address Translation (SLAT)
  • 4GB system RAM
  • BIOS-level hardware virtualization support must be enabled in the BIOS settings. For more information, see Virtualization.

For MacOs, you need:

  • Mac hardware (2010 or newer model) with an Intel processor and support for virtualization.
  • macOS version 10.14 or newer (Mojave, Catalina, or Big Sur). The last version with support for macOS 10.13 (High Sierra) was 2.3.0.5
  • At least 4 GB of RAM.
  • VirtualBox version 4.3.30 or newer.

Windows Subsystem for Linux 2 (WSL2)

For Windows, you need WSL installed and enabled. WSL allows you to run Linux binary executables natively on Windows 10. Information on how to install and enable WSL2 can be found in Microsoft’s developer environment documentation.

After installing WSL2, you need to configure Docker Desktop to use the WSL2 backend.

You will need the Windows Terminal installed to run console commands, like in a MacOs/Linux terminal.

Create a Laravel 8 application

Development environment setup 15 minutes.

The first step to create a Laravel 8 application is to run a command from the terminal.

  1. Open your Windows or macOS terminal/console

    For windows, use the Windows Terminal

  2. (Optional) Move your working directory to your project folders

    If you want to create your project in a specific folder run cd /path/to/my/projects/folder/ using the path you want.

  3. Run this command to create the project

    curl -s https://laravel.build/example-app | bash
    Where example-app is the name of your application. You can use any other name, a folder with this name will be created. In this example I’ve used laravel8-app:
    Laravel8 Command Line view

  4. Follow the instructions and prompts

    During the first execution, some subsystems will be downloaded to work with Laravel 8 projects, including a Docker image of the Laravel8/PHP and development tools (Sail, Facade, Tinker, Doctrine, PHP-Unit). At the end of the process, you may be prompted to enter an administrator password to make some system changes.
    Laravel 8 installation admin prompt

  5. Enter to the Laravel 8 project folder

    Run the command: cd [PROJECT_NAME], using your recently created project name. In this example I used laravel8-app:
    cd laravel8-app

  6. Run sail to start your project

    ./vendor/bin/sail up
    This command will spin up the Laravel8 docker virtual environment. The first time will also configure and install the system tools to work with Laravel, including MySQL. So, it will take some time.Laravel 8 sail up

  7. Running again, if you find any issues:

    After fixing any issue running the command for the first time, run the command ./vendor/bin/sail up again until all services are up successfully:
    laravel8 sail up running
    You can stop the server by pressing Ctrl+C from the same command window.

  8. Access the development server URL

    Point your browser to http://localhost to see the starter Laravel 8 Application Home.
    Laravel 8 starter Home

Recommended Books

Possible Issues

After your first attempt, you could get a couple of issues, depending on your current system configuration. In other words, this could be a typical case if you already have installed previously MySQL, or a packaged web development like XAMPP, WAMP, MAMP (Apache/Mysql/Php)

Laravel HTTP service can’t start

If the console shows an error message for the laravel service:

ERROR: for laravel8-app_laravel.test_1  Cannot start service laravel.test: Ports are not available: listen tcp 0.0.0.0:80: bind: address already in use
ERROR: for laravel.test  Cannot start service laravel.test: Ports are not available: listen tcp 0.0.0.0:80: bind: address already in use
ERROR: Encountered errors while bringing up the project

It means you already have a running webserver (Apache, Nginx, Node) in your default port (80). Locate the service and then stop it from the command line or from the XAMPP/WAMP/MAMP interface if you installed it there.

MySQL container can’t start

If you get a message like this:

ERROR: for laravel8-app_mysql_1 Cannot start service mysql: Ports are not available: listen tcp 0.0.0.0:3306: bind: address already in use
ERROR: for mysql Cannot start service mysql: Ports are not available: listen tcp 0.0.0.0:3306: bind: address already in use

This message means you already have a mysql server running locally in your system, listening on port 3306. You will need to stop your local MySQL server before running Laravel sail. Try locating the command mysqladmin and run /path/to/mysql/bin/mysqladmin shutdown [-u root -p]

Or try one of this commands to shut down the server:

On MacOS, Mysql running from MAMP

/Applications/MAMP/Library/bin/mysqladmin shutdown

For MacOS, Mysql running from a Homebrew installation (version could vary)
/usr/local/Cellar/mysql/5.5.20/bin/mysqladmin shutdown

On Windows, running Mysql as a service (service name could vary to something like MySQL5, MySQL53 or something similar.

net stop MySQL

Other services can’t start

If you find another service not running properly for the same cause (address already in use), you need to stop the service using that port before running the sail up command again.

Other methods to create a Laravel 8 Application

If you already have PHP (>=7.3) and Composer installed, and you prefer to use these tools instead of using Sail and the virtualized envirnment. You can create the Laravel 8 application via Composer.

For instruction about how to Install PHP and Composer, please refer to Configure PHP and Laravel Framework

First of all, check your PHP version (7.3 or newer) running in your system:

MacBookPro laravel8-app $ php -v
PHP 7.3.11 (cli) (built: Jun  5 2020 23:50:40) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies

Also, check you have Composer installed:

MacBookPro laravel8-app $ composer --version
Composer version 2.0.8 2020-12-03 17:20:38

Finally, check if the composer path is included in your PATH (generally, the path of composer files is $HOME/composer/vendor/bin (where $HOME is your home directory, like /Users/username (macOS/Windows) or /home/username in other systems.

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/username/.composer/vendor/bin

Install laravel from Composer

  • Run this command to download the Laravel application installer
    composer global require laravel/installer
  • Run laravel new app-name using the app name you want. In this example, laravel-app:
    laravel new laravel-app
  • Change to the Laravel app folder using the name of the previous step:
    cd laravel-app
  • Run the artisan command to start the Laravel service:
    php artisan serve
  • Now you can access the development URL at http://localhost:8000
    Laravel 8 starter Home

Startup Options for Laravel

Most of these options works for both sail and local application installation.

Environment variables

You can find the default environment variables in the file .env (which is created based on the example file .env.example) used to setup the running environment, for example:

  • Application startup configuration (APP_NAME, APP_URL, APP_DEBUG)
  • Logging configuration (LOG_CHANNEL, LOG_LEVEL)
  • Database configuration (DB_CONNECTION, DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD)
  • Session configuration (SESSION_DRIVER, SESSION_LIFETIME)
  • Mail sending configuration (MAIL_MAILER, MAIL_HOST, MAIL_PORT, MAIL_FROM_ADDRESS, MAIL_FROM_NAME)
  • AWS access key (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION, AWS_BUCKET)
  • Redis configuration (REDIS_HOST, REDIS_PORT, REDIS_PASSWORD)
  • Push notifications with Pusher (PUSHER_APP_ID, PUSHER_APP_KEY, PUSHER_APP_SECRET)

Basic startup options for sail

The sail command is a lightweight command line interface. It’s runs in the same way of docker-compose command (for more information, go to the Docker compose documentation page)

The most common call is to run the service containers and see the container logs inline. You can stop the execution of those services just pressing Ctrl+C.

./vendor/bin/sail up

Daemon mode

If you prefer to run the services in the background, you can use the -d option (daemon mode) for sail.

./vendor/bin/sail up -d

The command prompt will return and you will not see the logs, only a quick state of the service status until they are up.

$ ./vendor/bin/sail up -d
Creating network "laravel8-app_sail" with driver "bridge"
Creating laravel8-app_redis_1 … done
Creating laravel8-app_mysql_1 … done
Creating laravel8-app_mailhog_1 … done
Creating laravel8-app_laravel.test_1 … done

If you want to stop the services, just run the sail stop command:

./vendor/bin/sail stop

If you want to stop and remove the created containers/networks, use sail down:

./vendor/bin/sail down

Change the Laravel public path

In some cases, you will need to change the Laravel public path to a different name. The configuration for the public path in Laravel 8 (default public/) has been simplified to these steps:

  1. Stop the Laravel service (Ctrl+C or running sail stop)
  2. Move/rename the /public folder to another name (for example, public_html, commonly used in many user-based web hostings). You can use this command line:
    mv public public_html
  3. Modify the file server.php to change the public path to your new path:
    if ($uri !== '/' && file_exists(DIR.'/public_html'.$uri)) {
    return false;
    }
    require_once DIR.'/public_html/index.php';
  4. Modify the file bootstrap/app.php to overwrite the publicPath() method:
    class MyApp extends IlluminateFoundationApplication{
     public function publicPath(){
      return $this->basePath.DIRECTORY_SEPARATOR.'public_html';
     }
    }
    $app = new MyApp(
     $_ENV['APP_BASE_PATH'] ?? dirname(DIR)
    );
  5. Start the Laravel service again running sail up [-d]

As you can see in this article: How to change the Laravel public folder path, previous Laravel versions needed more changes to get the same result.