Start Using PHP 8

Roberto Butti
Nov 4, 2020 · 4 min read
Using PHP8 with Docker

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.

Install Docker

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

Docker status
Docker Desktop is running

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
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.

echo PHP_VERSION;
echo 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”:

<?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.

Modules

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:

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

The Startup

Get smarter at building your thing. Join The Startup’s +792K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Roberto Butti

Written by

I’m technophile. Vuejs and Laravel enthusiast! #vuejs #laravel. I love #coding

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store