How to Install Laravel 9 Without PHP 8 Using Docker

In laravel on 2~5 minutes
How to Install Laravel 9 Without PHP 8 Using Docker

We all like to use the latest features of the latest versions. But what happens when your system package manager does not allow you to install the latest versions. For example, if you are using Ubuntu 20.04 LTS in your PC, the latest PHP version available in the system package manager is PHP 7.4.3. If you forcefully tried to install Laravel 9 on such a PC, you will get an error message similar to the following. Because PHP 8 is the minimum version that can be used for Laravel 9.

[InvalidArgumentException]
Could not find package laravel/laravel with version v9.0.0 in a version
installable using your PHP version, PHP extensions and Composer version.

If that was your personal computer, of course, fortunately, you have a couple of options.

  • Install PHP 8 from a third-party repository like ondrej/php.
  • Download the source and compile it by yourself.
  • Upgrade the Operating System to Ubuntu 21.04 or any other latest.

But what happens if this happened to a PC at work, most of the time you will not be able to make system wide changes like installing and uninstalling new applications. So this is where our lifesaver, Docker comes into the rescue.

Now, instead of using the old Composer installed in the system, we are using the latest Composer from the Docker Hub. Run the following command to create a new Laravel 9 project.

  • -it - Interactive pseudo-TTY mode allows interacting with the container using a bash shell.
  • --rm - Automatically remove the container on exit.
  • -u $(id -u):$(id -g) - Runs the commands as your host PC user and group to avoid file permission issues.
  • -v $(pwd):/app - Binds the current directory into the container’s /app directory.
  • laravel9 - Laravel project name.
docker run -it --rm -u $(id -u):$(id -g) -v $(pwd):/app composer:latest create-project --prefer-dist laravel/laravel laravel9

Change into the newly created project.

cd laravel9

Check whether Laravel 9 is installed or not by running the following command.

docker run -it --rm -u $(id -u):$(id -g) -v $(pwd):/app composer:latest php artisan --version

It should print a message similar to the following message.

Laravel Framework 9.0.0

Setup Laravel Sail by running the following command.

docker run -it --rm -u $(id -u):$(id -g) -v $(pwd):/app composer:latest php artisan sail:install --with mysql

The sail command in vendor/bin is a Bash script, so you can directly run it without using the Composer container. So, the following sail up command will start the Laravel 9 application with Laravel Sail.

./vendor/bin/sail up -d

You can stop Laravel Sail by running the sail down command.

./vendor/bin/sail down

Open http://localhost in the browser. There will be something similar to Laravel v9.0.0 (PHP v8.1.2) at the right bottom of the page.