What is Laravel Sail, and how to use it?

Razet · · 12586 Views
What is Laravel Sail, and how to use it?

Laravel Sail is a light-weight command-line interface for communicating with Laravel's default Docker development environment. This means you won't have to utilize Docker to make different containers manually, yet you can simply use Laravel Sail to do that for you.

By using Laravel Sail and you will get a completely working local development environment.

Laravel Sail utilizes the docker-compose.yml file and the sail script that stored in the vendor folder of your project at vendor/bin/sail. The sail script gives a CLI with convenient methods for communicating with the Docker containers defined by the docker-compose.yml file. 

In this post, you will learn how to install and begin with Laravel Sail.

Prerequisites

To begin with Laravel Sail, all that you require is to have Docker and Docker Compose installed on your system.

Installing & Setting Up Laravel Sail

The fasted approach to begin with Laravel Sail is running the following command after you have Docker installed:

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

This will install a new Laravel application in a my-app directory. Note that you can change the my-app with the name of the directory that you might want to use.

New installations of Laravel will automatically include a vendor/bin/sail CLI script you can use to start, stop, and deal with your Laravel application in an easy-to-use Docker environment.

So, to make life simpler, how about we configure the Bash alias that permits us to execute Sail's commands quicker.

alias sail='bash vendor/bin/sail'

What this does, it sets sail to point to the vendor/bin/sail script, so that you can execute sail commands without having to type vendor/bin every time.

Laravel Sail Commands

One important thing to remember is that before starting it, ensure that no other web servers or databases are not running on your system.

Run the following command on the terminal to start Docker containers:

# Run docker containers `docker-compose up` 
sail up

This will begin pulling the required Docker images, and it will set up a full development environment on your machine.

Something else that you could do is to start the Docker containers in the background by adding -d (-d stands for detached) after the up command. 

# Run docker containers in the background 
# `docker-compose up -d`
sail up -d

Note: If you run this at first, it will require some time to get everything prepared. After that, it should be a lot faster.

Once you've run the sail up command, you can visit your server IP address or localhost if you are running it on your local system, and you will see a fresh new Laravel installation.

If you want to stop the running containers, run the following command on the terminal:

# Stop containers and remove containers, networks, etc. 
sail down

Sail comes with NPM, composer, and all the tools you're used to using locally, all packaged up neatly inside Docker containers. Here are a few examples you'd commonly use:

# Runs php artisan queue:work in the container
sail artisan queue:work

# Run PHP CLI commands and return output
sail php --version

# Require a composer package
sail composer require laravel/sanctum

# Node and NPM
sail node --version
sail npm install

To get started, check out the official Laravel Sail documentation!

Taylor also did a live stream demonstrating Laravel Sail and gives some background on the project:

Credit

  1. https://devdojo.com/bobbyiliev/what-is-laravel-sail-and-how-to-get-started

1

Please login or create new account to add your comment.

1 comment
You may also like:

Building a Real-Time Chat App with Laravel Reverb and Nuxt 3

Building a real-time chat application is a great way to understand the power of WebSockets and real-time communication. In this tutorial, we will walk through creating a Real-Time (...)
Harish Kumar

How to Set Up Nuxt 3 Authentication with Laravel Sanctum (Step-by-Step Guide)

In modern web development, securing your application’s authentication process is a top priority. For developers building Single Page Applications (SPA) or Server-Side Rendered (...)
Harish Kumar

Laracon US 2024: Laravel 11 Minor Features That Enhance Performance

At Laracon US 2024, Taylor Otwell and the Laravel team introduced a series of "minor" features for Laravel 11 that are anything but minor. These enhancements, while not headline-grabbing (...)
Harish Kumar

PHP OPCache: The Secret Weapon for Laravel Performance Boost

OPCache, a built-in PHP opcode cache, is a powerful tool for significantly improving Laravel application speed. This guide will demonstrate how to effectively utilize OPCache to (...)
Harish Kumar

How to Use DTOs for Cleaner Code in Laravel, Best Practices and Implementation Guide

When developing APIs in Laravel, ensuring your responses are clear, concise, and consistent is crucial for creating a maintainable and scalable application. One effective way to (...)
Harish Kumar

Data Type Validation in Laravel Collections with the `ensure()` Method

Before moving on to the ensure() method, let us first know what Laravel Collections actually are. These are wrappers of PHP arrays, offering a fluent and helpful interface in interacting (...)
Harish Kumar