dev-resources.site
for different kinds of informations.
How to Fix the "PHP Not Found" Error on macOS After Installing XAMPP
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!
Featured ones: