Before you can get started with PHP, you will need to install it. Homebrew is a popular choice on macOS, since it provides the ability to easily switch between different PHP versions. In this lesson, we'll install PHP with Homebrew on macOS.
Mark Shust: [0:00] Let's go to the official PHP docs at php.net and click the documentation tab at the top, and then click English to view the online documentation. If we scroll down, we will see a link for installation and configuration.
[0:17] We'll see install docs for Linux or Unix, Mac, as well as Windows. This tutorial assumes you are on a Mac, but if you have another OS, feel free to install the instructions for that specific OS.
[0:33] PHP used to be included on macOS, but Apple removed that binary from all versions of macOS, starting with Monterey. This is no problem. I actually never much liked the built-in PHP version because it always seemed that when newer versions were released, it was always hard to switch back and forth between the older and newer versions.
[0:54] Under Installation on macOS, let's click this Using Packages link. Within here, we will see a reference to Homebrew. Let's go ahead and follow this link to brew.sh.
[1:08] This is the homepage for Homebrew, and this is just a great package manager for macOS. Homebrew provides the ability to easily install software packages on macOS that would otherwise be a bit difficult to install, like PHP.
[1:23] Let's install Homebrew by going ahead and copying this line to the clipboard, open up your terminal prompt, and paste it in. The Homebrew install script will ask for your system password. This is completely normal, so let's go ahead and type this in.
[1:42] We'll see that Homebrew installs a lot of files at the /opt/homebrew directory. This is totally OK. Let's go ahead and tap Enter again. Homebrew will download and install a ton of different packages from different repositories. Homebrew will also tap specific repositories, meaning that it makes it available to install packages within those specific repositories.
[2:08] It looks like Homebrew is now installed, but we will see this warning that says /opt/homebrew/bin is not in our PATH. Instructions on how to configure your shell are in the next steps below, which is right here. We need to execute these two commands to add Homebrew to our PATH.
[2:28] This /opt/homebrew/bin/brew script is a special script which adds the /opt/homebrew/bin directory to this system PATH. This makes all binaries installed with brew available at your terminal prompt. Let's copy this first line and paste it into our terminal window, and we will do the same with this second line.
[2:54] After these two commands are executed, brew should be completely installed. We can confirm this by typing, which brew. This will give us the path of the brew binary. We can also execute brew and we should see some output that looks just like this.
[3:14] Our next step is to install PHP. We can do this with brew by running brew install php. This will install the most recent version of PHP, which at the time of this video is PHP 8.1.
[3:31] It will take a little bit of time to download all of these packages, so just be patient. Any other dependencies that are needed to run PHP are also downloaded and installed at this time.
[3:43] Once the install script completes, we have information of how to find the php.ini file, as well as how to restart the PHP services. For most, if not all of these lessons, we will just be using the PHP binary. This doesn't rely on a service and is accessible by just typing in php in the command line.
[4:09] We can pass any sort of flags to this PHP command, including -v, which tells us the version of PHP that is installed. If you see output like this, it means we're all set and we're off to the races to start coding up some PHP.