This tutorial provides all the steps required to create the first project in Laravel using Laravel Sail and Docker Engine on Ubuntu 20.04 LTS. Laravel Sail is a lightweight command-line interface that can be used to interact with Laravel's default Docker configuration. Laravel Sail is a built-in solution to run Laravel projects using Docker Engine. You can also follow How To Install Docker Engine on Ubuntu 20.04 LTS.
Notes: It's Bandwith Intensive operation for the first time, hence make sure that you are on a good internet connection and also followed How To Install Docker Engine on Ubuntu 20.04 LTS to avoid filling your root drive. The Sail's application containers will be built on your machine for the first time.
Install Docker Engine
Install Docker Engine on your Ubuntu system to continue with this tutorial. You can follow How To Install Docker Engine on Ubuntu 20.04 LTS.
Also, install Docker Compose if not done yet.
# Install Docker Compose
sudo apt install docker-compose
Create Laravel Project
Launch Terminal and create the Laravel project in your preferred directory using the command as shown below.
# Create Project
curl -s https://laravel.build/helloworld | bash
In case you have started Docker Engine using the sudo command, it might throw the error - Docker is not running as shown in Fig 1.
Make sure that you are running Docker without root privileges as a non-root user as mentioned in the last section of How To Install Docker Engine on Ubuntu 20.04 LTS.
Using sudo prefix - You may also check the alternate way to use Laravel Sail with sudo prefix as mentioned in the next section of this tutorial. In this way, we can use sudo to create and launch the project using Laravel Sail. The rest of the steps should be the same.
It will start creating the Laravel project within the current directory as shown in Fig 2.
It will download all the applications and dependencies and shows a success message as shown in Fig 3.
Launch Laravel Project
We can launch the newly installed Hello World project in Laravel using Sail using the commands as shown below.
cd helloworld
./vendor/bin/sail up
It will take some time for the first launch as shown in Fig 4. You might be required to run this command several times in case it fails to install some of the Ubuntu repositories. The subsequent launches will be faster. On successfully launching the required service, the output should be similar to Fig 5.
On the successful start of the application's Docker containers, we can access the application using the preferred web browser at: http://127.0.0.1. It might show the permission errors as mentioned below and as shown in Fig 6.
UnexpectedValueException
The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied
We can add appropriate permission based on the scenario. In my case, I have switched to the root user and added the write permission to the storage directory as shown below.
# Storage Permission
sudo chmod o+w storage/ -R
Now open the browser and reload or refresh the page. The default page should be similar to Fig 7.
We can press Ctrl + C to exit and stop all the containers as shown in Fig 8.
Create Laravel Project - With sudo
We can also install Laravel using Composer and run the Laravel Sail with sudo prefix on the systems where we do not want to add a user and add it to the Docker group. You may follow the below-mentioned commands to achieve the same. You can also follow How To Install Composer On Ubuntu 20.04.
# Create Project
composer create-project laravel/laravel helloworld
# Change Directory
cd helloworld
# Install Sail
php artisan sail:install
# Start Sail
sudo ./vendor/bin/sail up
Summary
This tutorial provided the links to useful resources and the steps required to install and launch your first Laravel Application using Laravel Sail and Docker Engine on Ubuntu 20.04 LTS.