Introduction

Thinking of getting started with Laravel? Then this article is for you! In this article we’ll create a fresh application using laravel sail which in my opinion is one of the easiest ways to get started with Laravel.

Laravel is one of the most popular PHP frameworks today, known for its elegant syntax, ease of use and powerful tools such as laravel Sanctum, Jetstream and Telescope. You can read more about the popularity of laravel in a blog post I wrote here.

In this article, we’ll create a fresh laravel application using laravel sail which in my opinion is one of the easiest ways for getting started with laravel.

The example set up covered here will be for Mac OS, and the version of Laravel I’ll be using is Laravel 11. for windows and linux installations please consult the laravel installation documentation, a link can be found at the bottom of this article.

Already have a Laravel installation? The you can read this post here, it will show you how to install sail to an existing application.

What is Laravel Sail?

Laravel Sail is an official development environment for Laravel, built on top of Docker. It allows developers to quickly get up and running with a containerized development setup, without needing to manually configure Docker images or services. By providing an out-of-the-box solution, Sail simplifies the often complex task of managing dependencies like MySQL, Redis, and PHP versions.

 

Key Benefits of Laravel Sail:

  • Simplicity: You don’t need to install PHP, MySQL, or other services locally. Sail handles everything through Docker.
  • Consistency: By using Docker containers, you ensure that your development environment matches your production setup, reducing bugs due to configuration discrepancies.
  • Customization: You can easily tweak Sail’s configuration to meet the specific needs of your application, such as adding Redis or changing the PHP version.

Prerequisites

Before setting up the Laravel application, make sure you have the following installed:

  • Docker Desktop: Is required for running the containerized environment. If you haven’t installed Docker yet, head to Docker’s official site and follow the setup instructions for your OS.

It’s also recommended to have some basic understanding of PHP and Docker and terminal commands.

Installing Laravel with Sail

Let’s walk through setting up a new Laravel project using Laravel Sail.

Step 1: Getting Started with Laravel – Install Laravel using the the curl command

To begin, open your terminal and navigate to the directory where you want to install the Laravel project. I normally have a folder called Development in my home directory for all my projects. Once you’re in the directory you can use the following curl command to install Laravel without needing Composer directly on your machine:

curl -s "https://laravel.build/my-app" | bash

Replace my-app with your project name.

Breakdown of the command:

  • https://laravel.build/my-app: This URL dynamically creates a fresh Laravel project with the given name, in this case, my-app.
  • The | bash  part pipes the result of the curl command to your terminal, where it executes the script to download and configure the Laravel project.

After running the command, a new folder called my-app will be created, and all necessary files will be downloaded. This process could take a few minutes to complete, so don’t worry if you think it’s taking too long!

Getting Started with Laravel after docker installation

You should see output like above after successfully installing Laravel via the curl command. You may have to enter your system password for laravel to make some permission changes for your application.

Step 2: Getting Started with Laravel – Navigate to your project

Once the Laravel project is set up, you can navigate into the directory using:

cd my-app

Step 3: Getting Started with Laravel – Start Laravel Sail

Laravel Sail is a lightweight command-line interface for interacting with Laravel’s Docker environment. To start the Sail environment, run the following command:

./vendor/bin/sail up

This will pull the necessary Docker containers and start your development environment, including services like PHP, MySQL, and Redis. You will also be able to see all the logs for the runnings services in your teminal.

Getting Started with Laravel after starting docker

Tip: If you don’t want to see all the logs for the running services – run in detached mode.

This will run the conatiners in the background and return you to the terminal without having to open a new terminal window.

./vendor/bin/sail up -d

Step 4: Getting Started with Laravel Sail –  Running Artisan Commands in Sail

With Sail running, you can execute Laravel Artisan commands by prefixing them with Sail, like so:

./vendor/bin/sail artisan migrate

Run this command to generate the initail database schema.

Running Artisan, Composer, or NPM Commands

Laravel Sail provides easy access to common Laravel commands like Artisan, Composer, and NPM:

./vendor/bin/sail artisan migrate
./vendor/bin/sail composer install
./vendor/bin/sail npm run dev

This allows you to run Laravel commands without needing to directly access the Docker containers.

List all of the artisan commands

You can view all of the available artisan commands by running:

./vendor/bin/sail artisan list

Step 5: Getting Started with Laravel – Access Your Application

After running Sail, and runnig the artisan migration command your application will be accessible on http://localhost. You can open this URL in your browser to verify that the Laravel installation is successful. You should see screen like the one below:

Laravel Installation page after starting laravel sail

Step 6: Getting Started with Laravel Sail – Shutting Down the Sail Environment

When you’re done working, you can stop the Docker containers with the following command:

./vendor/bin/sail down

This will stop the containers but keep your data and configuration intact.

Using Sail In Development

Once Sail is up and running, you can use it just like any other development environment. Some common use cases include:

  • Developing New Features: Since Sail provides all necessary services (database, caching, etc.), you can easily develop and test features without worrying about local environment issues.
  • Team Collaboration: Sail ensures that everyone on your team has the same development environment, reducing the “works on my machine” problem.

If performance becomes a concern, especially on slower machines, consider configuring Docker to use more resources or limiting the number of services running at once.

Deploying after using Sail

It’s important to note that Laravel Sail is designed for local development and not for production environments. When you’re ready to deploy, you’ll need to move away from Sail and deploy your app using other tools, such as Laravel Forge, Docker Compose, or managed platforms like AWS or DigitalOcean.

For small projects, you might even use Docker Compose in production, but for most production environments, it’s better to use a dedicated deployment solution.

Troubleshooting and Common Issues

While Sail is easy to use, there are some common issues you might encounter:

  • Docker Not Running: Make sure Docker is installed and running on your system.
  • Port Conflicts: If you have other services running on the default ports (e.g., MySQL), you might need to adjust the port numbers in the docker-compose.yml file.
  • Performance Issues: Docker can be resource-intensive, especially on machines with limited memory. Consider adjusting the resource limits in Docker’s settings.

If you run into any issues, check the Sail official documentation or reach out to the community for support.

Conclusion

Congratulations! You’ve successfully created a fresh Laravel application using the Laravel Sail curl command. You now have a Dockerized Laravel environment ready for development. This setup provides a clean, isolated environment that mimics production and makes development easy and reliable.

 

Additional Resources

Looking for a Laravel Developer? Contact Me to Discuss Your Project!

I’d be interested in hearing from you if you need a web developer to work on your upcoming project. Whether you require a front-end developer to create a stunning user interface, a back-end developer to build server-side logic, or a full-stack developer to handle everything from start to finish, I have the knowledge and experience needed to produce excellent results. Therefore, don’t be afraid to email me at contact@dalehudson.co.uk if you’re interested in working together or have any questions about my services. I would be delighted to talk with you about potential projects and assist you in realising your visions.