Home >Backend Development >PHP Tutorial >How to Fix the 'PHP Not Found' Error on macOS After Installing XAMPP
When macOS developers use XAMPP to set up a local development environment, they often encounter the frustrating "PHP not found" error. Even though XAMPP comes with PHP, the terminal may still not recognize the php command.
This article will guide you step by step to solve this problem and ensure that the system can find PHP.
Run the following command:
<code class="language-bash">php -v</code>
If you receive an error message like this:
<code>php not found</code>
This means that your system shell (such as zsh or bash) cannot find the PHP executable in its environment. This happens even though XAMPP contains its own PHP binary, because the directory containing PHP is not included in the shell's $PATH.
Let’s fix this problem!
XAMPP contains its own PHP installation, usually located in the following directory:
<code class="language-bash">/Applications/XAMPP/xamppfiles/bin/php</code>
To verify that the PHP executable exists in this location, run:
<code class="language-bash">ls /Applications/XAMPP/xamppfiles/bin/php</code>
If this command lists PHP files, you are on the right track.
To make the PHP executable globally available, you need to add the XAMPP PHP directory to your shell's $PATH.
Starting with macOS Catalina, zsh is the default shell. Please follow these steps to update your $PATH:
<code class="language-bash">nano ~/.zshrc</code>
<code class="language-bash">export PATH="/Applications/XAMPP/xamppfiles/bin:$PATH"</code>
Press Ctrl O, then Enter, and finally Ctrl X to save and close the file.
Apply changes immediately by running the following command:
<code class="language-bash">source ~/.zshrc</code>
If you are still using bash as your shell, edit .bash_profile:
<code class="language-bash">nano ~/.bash_profile</code>
Add the same line:
<code class="language-bash">export PATH="/Applications/XAMPP/xamppfiles/bin:$PATH"</code>
Save and apply changes using:
<code class="language-bash">source ~/.bash_profile</code>
After updating $PATH, test whether the php command works:
<code class="language-bash">php -v</code>
You should see the version of PHP that comes with XAMPP, for example:
<code>PHP 8.2.4 (cli) (built: Apr 6 2023 04:12:41) (NTS) Copyright (c) The PHP Group Zend Engine v4.2.4, Copyright (c) Zend Technologies</code>
If it works, congratulations! Your system now recognizes the php command.
If the above steps do not take effect immediately, please restart the terminal and try running php -v again. Sometimes changes to the shell configuration file require a terminal restart to take effect.
If you prefer a system-wide PHP installation rather than relying on the bundled version of XAMPP, you can install PHP using Homebrew:
<code class="language-bash">/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"</code>
<code class="language-bash">brew install php</code>
<code class="language-bash">php -v</code>
This will install the latest version of PHP and automatically configure your $PATH.
"PHP not found" errors can be quickly fixed once you understand how the shell's $PATH works. Whether you add XAMPP's PHP to your PATH or choose Homebrew, this guide will make sure you're up and running in no time. Now you can focus on what matters most – developing great apps!
Let us know in the comments if this guide was helpful, or share your tips for managing PHP on macOS!
The above is the detailed content of How to Fix the 'PHP Not Found' Error on macOS After Installing XAMPP. For more information, please follow other related articles on the PHP Chinese website!