How to Install PHP 8 on a Mac with Homebrew

How to Install PHP 8 on a Mac with Homebrew

Table of contents

No heading

No headings in the article.

PHP is a popular programming language used to create dynamic websites and web applications. In this article, we’ll guide you through the process of installing PHP 8 on a Mac using Homebrew, a popular package manager for macOS.

Before we start, it’s important to note that the following steps assume you have Homebrew installed on your system. If you haven’t installed it yet, you can do so by following the instructions on the Homebrew website.

Step 1: Update Homebrew

The first step is to update Homebrew to the latest version. Open the Terminal app and run the following command:

brew update

During the update process, you may encounter fatal errors such as “homebrew-core is a shallow clone” or “fatal: — unshallow on a complete repository does not make sense.” These errors occur because Homebrew clones a subset of the repository to speed up the update process. To fix this, run the following command:

git -C “$(brew — repo homebrew/core)” fetch — unshallow

This command fetches the complete repository and fixes the “shallow clone” error.`

Step 2: Install PHP 8

Now that Homebrew is up to date, we can install PHP 8. Run the following command:

brew install php

This command installs PHP 8.x and its dependencies. You may encounter errors such as “The formula built, but is not symlinked into /usr/local” or “Error: Could not symlink include/php/TSRM/TSRM.h.” These errors occur because the formula is not symlinked correctly. To fix this, run the following commands:

brew unlink php@7.3 brew link — overwrite php

The first command unlinks the old version of PHP (7.3 in this case), while the second command forces the link and overwrites all conflicting files.

Step 3: Verify the Installation

To verify that PHP 8 is installed correctly, run the following command:

php -v
This command should output the version number of PHP 8.1 installed on your system.

Step 4: Install PHP Extensions

PHP 8.1 comes with some built-in extensions, but you may need to install additional ones for your specific needs. You can install extensions using Homebrew. For example, to install the gd extension, run the following command:

brew install php-gd
This command installs the gd extension for PHP 8.x

Step 5: Configure PHP

By default, PHP 8.1 uses the built-in PHP server, but you can also use other web servers such as Apache or Nginx. To configure PHP with a web server, you’ll need to edit the server’s configuration files. The exact steps depend on the web server you’re using and the specific configuration you need.

Conclusion

In this article, we’ve shown you how to install PHP 8 on a Mac using Homebrew. We’ve also covered some of the common errors you may encounter during the installation process and how to fix them. With PHP 8 installed, you can now start building dynamic websites and web applications on your Mac.