How to install Composer on a Mac?
When attempting to bootstrap a Laravel project or set up a Symfony application, the first hurdle is often getting Composer running on your local machine. If you are seeing the error -bash: php: command not found followed by curl: (23) Failed writing body, the issue isn't with the installation script itself; it's that the PHP runtime environment is missing or misconfigured in your shell session.
The command curl -sS https://getcomposer.org/installer | php relies on two distinct system components working in sequence. First, curl must successfully fetch the installer script from the remote server. Second, the shell must locate and execute the php binary to process that script. The error message explicitly indicates that the second step failed because the system cannot find the PHP executable in your current PATH.
On macOS, PHP is rarely installed by default. Even if you have Homebrew installed, the path to the PHP binary often defaults to /usr/local/bin/php or /opt/homebrew/bin/php depending on whether you are using Intel or Apple Silicon hardware. If your shell environment variables haven't been updated to include these directories, or if PHP was uninstalled recently without cleaning up stale symlinks, the command will fail immediately.
To resolve this, you must first verify the existence of PHP and ensure it is accessible. Run the following command to check the current path resolution:
which php
If this returns nothing, PHP is not in your search path. You need to install it via Homebrew if you haven't already:
brew install php
Once installed, verify that the version number appears. If which php still fails after a successful brew install, you may need to restart your terminal application or run the following to refresh your shell configuration variables:
source ~/.zshrc
# or for bash users
source ~/.bash_profile
Assuming PHP is now detected correctly, you can attempt the standard Composer installation again. The script downloads a standalone executable and creates a file named composer.phar.
curl -sS https://getcomposer.org/installer | php
If this completes without error, the next step is crucial for long-term usability: making the command available globally without the .phar extension. Run the following commands to move the installer into your local bin directory and add it to your PATH:
mv composer.phar /usr/local/bin/composer
# Note: On Apple Silicon (M1/M2/M3) machines, use /opt/homebrew/bin/composer instead
Alternatively, if you prefer not to touch system-wide paths, you can move the file to a user-local bin directory that is already included in your shell's PATH, such as ~/.local/bin. This approach isolates Composer from potential conflicts with system PHP installations. After moving the file, ensure the command works by running:
composer --version
A common pitfall occurs when users try to install Laravel directly without realizing that Composer is just a dependency manager, not the framework itself. Once Composer is functional, you can proceed with creating your project:
composer create-project laravel/laravel myapp
If you previously installed Composer for Symfony and deleted it, ensuring that the PHP binary path remains consistent across different projects is vital. Sometimes, switching between Xcode Command Line Tools and Homebrew PHP versions causes PATH conflicts. Always ensure that the php command invoked by the installer matches the version of PHP you intend to use for your Laravel application.
Finally, if you encounter permission errors when moving the file to /usr/local/bin, using sudo is generally discouraged on macOS due to security implications. Instead, relying on user-local bins like ~/.local/bin or ensuring your home directory's bin folder is exported in your shell profile is the robust, modern approach for managing development tools.
