Home > Backend Development > PHP Tutorial > How to Fix the 'PHP Not Found' Error on macOS After Installing XAMPP

How to Fix the 'PHP Not Found' Error on macOS After Installing XAMPP

DDD
Release: 2025-01-11 08:31:42
Original
211 people have browsed it

How to Fix the

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.

Understanding error

Run the following command:

<code class="language-bash">php -v</code>
Copy after login
Copy after login
Copy after login

If you receive an error message like this:

<code>php not found</code>
Copy after login

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!

Step 1: Find PHP in your XAMPP installation

XAMPP contains its own PHP installation, usually located in the following directory:

<code class="language-bash">/Applications/XAMPP/xamppfiles/bin/php</code>
Copy after login

To verify that the PHP executable exists in this location, run:

<code class="language-bash">ls /Applications/XAMPP/xamppfiles/bin/php</code>
Copy after login

If this command lists PHP files, you are on the right track.

Step 2: Add XAMPP’s PHP to your shell’s PATH

To make the PHP executable globally available, you need to add the XAMPP PHP directory to your shell's $PATH.

Zsh users (macOS default shell)

Starting with macOS Catalina, zsh is the default shell. Please follow these steps to update your $PATH:

  1. Open your .zshrc file:
<code class="language-bash">nano ~/.zshrc</code>
Copy after login
  1. Add the following line at the end of the file:
<code class="language-bash">export PATH="/Applications/XAMPP/xamppfiles/bin:$PATH"</code>
Copy after login
Copy after login
  1. Press Ctrl O, then Enter, and finally Ctrl X to save and close the file.

  2. Apply changes immediately by running the following command:

<code class="language-bash">source ~/.zshrc</code>
Copy after login

Bash users

If you are still using bash as your shell, edit .bash_profile:

<code class="language-bash">nano ~/.bash_profile</code>
Copy after login

Add the same line:

<code class="language-bash">export PATH="/Applications/XAMPP/xamppfiles/bin:$PATH"</code>
Copy after login
Copy after login

Save and apply changes using:

<code class="language-bash">source ~/.bash_profile</code>
Copy after login

Step 3: Verify PHP installation

After updating $PATH, test whether the php command works:

<code class="language-bash">php -v</code>
Copy after login
Copy after login
Copy after login

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>
Copy after login

If it works, congratulations! Your system now recognizes the php command.

Step 4: Restart the terminal (optional)

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.

Alternative: Install PHP using Homebrew

If you prefer a system-wide PHP installation rather than relying on the bundled version of XAMPP, you can install PHP using Homebrew:

  1. If you don’t have Homebrew installed yet, please install:
<code class="language-bash">/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"</code>
Copy after login
  1. Install PHP:
<code class="language-bash">brew install php</code>
Copy after login
  1. Verify installation:
<code class="language-bash">php -v</code>
Copy after login
Copy after login
Copy after login

This will install the latest version of PHP and automatically configure your $PATH.

Conclusion

"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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template