If you’re a developer working on macOS and you’ve installed XAMPP to set up your local development environment, encountering the “PHP not found” error can be frustrating. Even though XAMPP includes PHP, your terminal might still not recognize the php
command.
This article walks you through how to resolve this issue step by step and ensure your system knows where to find PHP.
Understanding the Error
When you run the following command:
php -v
and get an error message like this:
php not found
it means your system’s shell (like zsh
or bash
) cannot locate the PHP executable in its environment. This happens because the directory containing PHP is not included in your shell’s $PATH
, even though XAMPP includes its own PHP binary.
Let’s fix that!
Step 1: Locate PHP in Your XAMPP Installation
XAMPP includes its own PHP installation, which is usually located in the following directory:
/Applications/XAMPP/xamppfiles/bin/php
To verify that the PHP executable exists in this location, run:
ls /Applications/XAMPP/xamppfiles/bin/php
If this command lists the PHP file, you’re on the right track.
Step 2: Add XAMPP’s PHP to Your Shell’s PATH
To make the PHP executable accessible globally, you need to add the XAMPP PHP directory to your shell’s $PATH
.
For Zsh Users (macOS Default Shell)
Starting with macOS Catalina, zsh
is the default shell. Follow these steps to update your $PATH
:
1.Open your .zshrc
file:
nano ~/.zshrc
2.Add the following line at the end of the file:
export PATH="/Applications/XAMPP/xamppfiles/bin:$PATH"
3.Save and close the file by pressing Ctrl+O
, then Enter
, and Ctrl+X
.
4.Apply the changes immediately by running:
source ~/.zshrc
For Bash Users
If you’re still using bash
as your shell, edit the .bash_profile
instead:
nano ~/.bash_profile
Add the same line:
export PATH="/Applications/XAMPP/xamppfiles/bin:$PATH"
Save and apply changes using:
source ~/.bash_profile
Step 3: Verify PHP Installation
After updating your $PATH
, test if the php
command now works:
php -v
You should see the PHP version that comes with XAMPP, for example:
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
If this works, congratulations! Your system now recognizes the php
command.
Step 4: Restart Terminal (Optional)
If the above steps don’t work immediately, restart your terminal and try running php -v
again. Sometimes, changes to the shell configuration file require a terminal restart to take effect.
Alternative: Use Homebrew for PHP
If you prefer a system-wide installation of PHP instead of relying on XAMPP’s bundled version, you can install PHP using Homebrew:
1.Install Homebrew if you haven’t already:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
2.Install PHP:
brew install php
3.Verify the installation:
php -v
This will install the latest version of PHP and automatically configure your $PATH
.
Conclusion
The “PHP not found” error can be a quick fix once you understand how your shell’s $PATH
works. Whether you’re adding XAMPP’s PHP to your PATH or opting for Homebrew, this guide ensures you’ll be up and running in no time. Now, you can focus on what matters most—developing great applications!
Let us know in the comments if this guide helped you, or share your tips for managing PHP on macOS!
Top comments (0)