If you want to try the new release of PHP 8 and you want to install it on your local machine without having side effects on your current PHP projects and your current PHP 7 stack, I have some suggestion for you.
I think that the safest and quickest way to install and use PHP 8 is running PHP in an isolated environment. To achieve that, you can use a Docker image with all things needed by PHP runtime avoiding negative impact or conflicts with your current local PHP 7 installation.
In order to install Docker , if you have a Mac device with macOS, you could install it via Homebrew:
brew cask install docker
Once the execution is completed, you have install Docker Desktop in: “/Applications/Docker.app”.
Docker Desktop is useful to launch all “internal” services needer by docker.
So, launch Docker Desktop. In your status bar , you will se the docker icon
With Docker, you have also a command line tool named “docker” very useful to launch, manage, install images.
Install and launch PHP 8 environment
Our goal is to use an official image that ships PHP 8.
In order to download PHP 8 image and launch it, you can run:
docker run -it --rm php:8.0-cli-alpine
If you see “php >” prompt, everything is fine.
In order to verify that you are using PHP 8, you could show the magic constant PHP_VERSION.
PHP 8 environment and your local source code
The previous example is useful to verify if your PHP 8 environment is setup correctly.
In a real case scenario, probably you have your PHP files/script in your local directory (on your filesystem) and you want that your new PHP 8 installation uses these files.
So, in your local directory create e new simple PHP file “test.php”:
echo "Welcome to PHP" . PHP_EOL;
echo "--- " .PHP_VERSION . PHP_EOL;
echo "--------------" . PHP_EOL;
Typically to execute that PHP script, you should run “php test.php”. In this way you are using your local PHP installation (you are not using the PHP 8 inside your new docker image). In order to execute your script, using new PHP8 stack, use this docker command:
docker run -it -v "$PWD":/usr/src/app -w /usr/src/app php:8.0-cli-alpine php test.php
Essentially we are mapping the current local directory (“$PWD”) with a internal directory of the docker image (/usr/src/app), executing the image “php:8.0-cli-alpine” and running “php test.php”:
- docker run -it: it executes docker image;
- -v “$PWD”:/usr/src/app : map your current local directory (“$PWD”) with the /usr/src/app directory of your PHP docker image;
- -w /usr/src/app : make the /usr/src/app the current directory once you launch the docker image (and remember that /usr/src/app is mapped with your current local directory, so if you have a file in your current local directory, you can access to it from your docker image in /usr/src/app directory)
- php:8.0-cli-alpine : your PHP8 image
- php test.php : the command that you want to run. With the previous options/argument you are running php from your new docker image and using test.php that is accessible via /usr/src/app directory.
I think this is the quickest way if you want to build a kind of playground where you can try new functionalities of PHP8.
There are a lot of article that covers new PHP8 features, my suggestion are:
- video tutorials: https://spatie.be/videos/front-line-php/the-match-expression
If you are curious about which PHP modules are include with this image you could execute:
docker run -it php:8.0-cli-alpine php -m
In the next post I will cover how to customize your PHP 8 image with new modules.
Upgrade the image
If you want to update your image, you can update it via:
docker pull php:8.0-cli-alpine