How to Update Laravel Sail to PHP v8.2

In Laravel on 4~6 minutes
How to Update Laravel Sail to PHP v8.2

Laravel Sail added PHP 8.2 support to its v1.16.0 release. Let’s update Laravel Sail to the latest available version to use PHP 8.2. If you have been looking for the old PHP 8.1 update, it is here.

composer update laravel/sail

Update docker-compose.yml

Open the docker-compose.yml file and replace the 8.1 in the context: ./vendor/laravel/sail/runtimes/8.1 line to 8.2. In addition to that, replace the 8.1 in the image: sail-8.1/app to 8.2. Now the docker-compose.yml file should looks like this.

version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.2
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.2/app

Now build the PHP 8.2 sail-8.2/app Docker image with the following command. It will take few minutes to build the image.

./vendor/bin/sail build --no-cache

Start Laravel Sail with PHP 8.2 in detached mode.

./vendor/bin/sail up -d

Once you built the sail-8.2/app Docker image, you do not need to rebuild it for other Laravel development environments. Just only update the docker-compose.yml file of that Laravel application.

Update:
MailHog image has been replaced with Mailpit in Laravel Sail v1.19.0 release. However, if you have been using MailHog from the previous config, still you will be able to continue using it without any issue.


PHP 8.1 Update with Laravel Sail v1.12.0 (Old)

PHP 8.1 support added to Laravel Sail starting from v1.12.0. Meanwhile, they have replaced the mysql:8.0 Docker image with mysql/mysql-server:8.0 image in this pull request. Keeping all these things in mind, let’s update the Laravel Sail to the latest version to use PHP 8.1.

First of all, update the Laravel Sail Composer package.

composer update laravel/sail

Update The docker-compose.yml File

Since Laravel Sail changed its MySQL Docker image, you can decide whether use it or continue with the mysql:8.0 image that you already have. If you decided not to continue with the updated image, follow Method 1 or otherwise, follow Method 2 as described below.

Method 1: PHP 8.1 Update Only

If you have valuable data in your development database and willing to continue using the mysql:8.0 Docker image without taking any risk or just want to use PHP 8.1, open the docker-compose.yml file and replace the 8.0 in the context: ./vendor/laravel/sail/runtimes/8.0 line to 8.1. Also, replace the 8.0 in the image: sail-8.0/app line to 8.1. So the final docker-compose.yml file should be similar to this.

version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.1
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.1/app

Method 2: PHP 8.1 + mysql/mysql-server:8.0 Image Update

If you do not have any valuable data in the development database or they can be easily regenerated using backups or seeds, run the following command to re-create the entire docker-compose.yml file with the services. Following services are available to use at this moment.

  • mysql
  • pgsql
  • mariadb
  • redis
  • memcached
  • meilisearch
  • minio
  • mailhog
  • selenium

However, only mention the services that you need using the --with.

php artisan sail:install --with mysql,mailhog,redis

Build The sail-8.1 Docker Image

Once you prepared the docker-compose.yml file, remove existing containers with the following command.

./vendor/bin/sail down

Build the sail-8.1 image.

./vendor/bin/sail build --no-cache

Start the application in detached mode.

./vendor/bin/sail up -d

If you followed Method 2, you might not be able to start the MySQL container as it tries to use the old MySQL volume created with the mysql:8.0 image. Under that situation, remove the existing containers along with all the volumes.

./vendor/bin/sail down -v

Now start the application as usual.

./vendor/bin/sail up -d

Run the following commands to see whether the update is successful.

Print the PHP version.

./vendor/bin/sail php -v

PHP 8.1.0 (cli) (built: Nov 25 2021 20:22:22) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.0, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.0, Copyright (c), by Zend Technologies
    with Xdebug v3.1.1, Copyright (c) 2002-2021, by Derick Rethans

Print the Composer version.

./vendor/bin/sail composer --version

Composer version 2.1.14 2021-11-30 10:51:43

Print the NodeJS version.

./vendor/bin/sail node -v

v16.13.1

Print the NPM version.

./vendor/bin/sail npm -v

8.1.4