How to properly set PHP environment variable to run commands in Git Bash
Attempting to run curl -s http://getcomposer.org/installer | php inside Git Bash on a Windows machine often results in a "php is not recognized" error. This frustration is common because the user is trying to execute a command that relies on an executable located outside the current working directory. The core issue isn't necessarily about installing Composer; it's about how the shell finds executables.
When you run a command like php, your operating system searches for the executable by consulting the PATH environment variable. It does not look for a file named php.exe in your current folder, nor does it assume you want to execute every .exe file found in your root directory. Instead, it scans the directories listed in the PATH variable sequentially until it finds a match.
The user's proposed solution involves setting an environment variable with the name php and a value pointing directly to an installation directory or binary. This approach is fundamentally flawed for two reasons. First, Windows interprets environment variables as key-value pairs where the value represents the data associated with that key. If you create a variable named php and set it to C:\wamp\bin\php\php5.3.8, your shell will look for an environment variable literally called php before executing commands. However, most command-line tools, including Git Bash, do not automatically expand the command name php into the value of a variable named php. They expect the system to find the executable via the PATH mechanism.
Second, and more critically, setting an environment variable to a full path does not add that path to your search list. You are not telling Windows "use this folder," you are telling it "the value for the key 'php' is this string." To make php runnable, you must modify the system's PATH variable to include the directory containing the PHP binaries.
In a WAMP installation, the PHP executable usually resides in a directory like C:\wamp\bin\php\php5.3.8. Note that while the user mentioned version 5.3.8, this is significantly outdated and lacks support for modern PHP versions and Composer packages. If you are starting fresh, ensure you are pointing to a supported version of PHP. Regardless of the version, the directory structure remains similar.
To fix the issue in Git Bash without permanently altering system-wide settings (which can affect other applications), you should append the specific WAMP PHP directory to your PATH. You can do this by running the following command within Git Bash:
export PATH="$PATH:/wamp/bin/php/php5.3.8"
This command takes the existing PATH value, appends the new directory path (converted to forward slashes for Unix-style shells), and saves it for the current session. If you want this change to persist across reboots or new terminal windows, you should add this line to your shell configuration file, such as .bashrc or .bash_profile located in your user home directory.
It is important to verify that you are pointing to a directory, not a specific file. The PATH variable must contain directories; it cannot contain individual files like C:\wamp\bin\php\php.exe. If you set the path incorrectly, or if the directory contains spaces or special characters without proper quoting in your configuration, the shell may fail to parse the command correctly.
After adding the directory to your PATH, try running the original Composer installation command again:
curl -s http://getcomposer.org/installer | php
If the script executes successfully, you can verify the PHP version by simply typing php -v. If you still encounter errors, check that Git Bash is actually using the updated environment variables. Sometimes, applications spawn new processes with a snapshot of the environment at launch time. Restarting your terminal session is often necessary to pick up changes made to global or user-level environment files.
While this manual configuration works for local development environments like WAMP, it adds unnecessary complexity if you are deploying to a production server. In a standard Linux or Windows Server environment, PHP is typically installed globally via package managers (like apt, yum, or choco) which automatically configure the PATH correctly upon installation. For local Windows setups, tools like Laravel Vapor or specific deployment strategies might manage these dependencies differently, so consider whether a global install is truly necessary for your workflow.
If you are working on a project that requires a specific PHP version without modifying your global PATH, consider using a version manager like phpenv or php-build. These tools allow you to switch between PHP versions seamlessly without cluttering your system paths or breaking existing installations. This is particularly useful when maintaining compatibility with legacy codebases while still accessing modern Composer repositories.