How To Update Laravel Sail To PHP v8.1
Tags: laravel, sail, php, update
Published: 2022-01-08 18:28:24

PHP 8.1 support added to Laravel Sail starting from [v1.12.0](https://github.com/laravel/sail/pull/254/files). Meanwhile, they have replaced the [mysql:8.0](https://hub.docker.com/_/mysql) Docker image with [mysql/mysql-server:8.0](https://hub.docker.com/r/mysql/mysql-server) image in [this pull request](https://github.com/laravel/sail/pull/272). 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
```

Feel free to visit [devtonight.com](https://devtonight.com?utm_source=dev&utm_medium=questions&utm_campaign=how-to-update-laravel-sail-to-php-v81) for more related content.