Byte Ebi's Logo

Byte Ebi 🍤

A Bit everyday A Byte every week

Introducing Laravel Sail and Basic Operations

Using Laravel Sail to launch the development environment with ease and joy.

Ray

In the context of Laravel development environment setup, both official and unofficial sources offer a plethora of approaches.
This time, we will be introducing a package introduced after Laravel 8: Laravel Sail .

In the past, to save beginners time in the initial setup of environments, there have been numerous tools for development environments.
For example, Laravel Homestead , built using virtual machines, was one option.
There were also Docker-based development environments like laradock .
Or creating a Laravel development environment using Docker , as I personally do.

In Laravel 8 and onwards, Laravel Sail has been integrated as a built-in package.
Compared to Laravel Homestead, it requires fewer resources as it utilizes Docker.
In contrast to laradock, which also operates within a Docker environment, Laravel Sail offers simpler configuration, and in some cases, even requires no configuration, achieving a plug-and-play experience.

You need to install Docker before start using Laravel Sail

Install Sail

In Laravel 9, Sail is now included as a built-in feature, allowing you to directly launch services through commands.

php artisan sail:install

For older versions of Laravel that do not have Sail, you can install it using Composer.

composer require laravel/sail --dev

Upon executing the command, an interactive interface will prompt you to select the desired services.

 Which services would you like to install? [mysql]:
  [0] mysql
  [1] pgsql
  [2] mariadb
  [3] redis
  [4] memcached
  [5] meilisearch
  [6] minio
  [7] mailhog
  [8] selenium
 > 0,3,7

Sail scaffolding installed successfully.

After making your selections, a docker-compose.yml file will be generated in the project’s root directory.
You can then use a command to launch the Sail service.

./vendor/bin/sail up

設定指令別名

If you’re feeling a bit lazy and don’t want to type out the entire command, you can set up an alias for it.
Open either vim ~/.bashrc or vim ~/.zshrc (depending on your terminal).
Add the following line to the file:

alias sail="./vendor/bin/sail"

After that, you can directly execute Sail commands within the project using the sail alias.
The following operations are performed assuming you’ve added the alias.

Sail command

The usage is similar to Docker commands:

  • sail up -d:Start and run in the background
  • sail stop:Stop
  • sail down:Stop and remove containers
  • sail build --no-cache:Rebuild containers, ignoring all caches for a complete rebuild.

Adjust docker image

The default image includes the basic Laravel environment.
If you need to make adjustments, such as installing additional PHP extensions, you will need to export the relevant configurations.

php artisan sail:publish

After executing the command, a docker folder will be added to the project, containing container settings and Dockerfiles.

Execute command

Sail provides a convenient way to call various commands by adding sail in front of them.
Essentially, you prepend sail to the desired commands.

Execute PHP command

sail php --version

Execute Composer command

sail composer install       # composer install

Execute Artisan command

sail artisan queue:work     # php artisan queue:work
sail artisan schedule:work  # php artisan schedule:work

Execute shell command

sail shell myShell.sh       # sh myShell.sh

By following these simple steps, you can easily set up a local development environment and execute commands as well.
Laravel Sail significantly reduces many of the development hassles!

Recent Posts

Categories

Tags