zsh: command not found: composer

command-not-found

Published ยท Updated

zsh: command not found: composer means no folder on your PATH has a file called composer. On a Mac there are three usual reasons:

  1. Composer is not installed. It never shipped with macOS.
  2. You ran Composer's installer, which leaves composer.phar in the current folder, and did not move it anywhere.
  3. PHP is missing. macOS has not included PHP since macOS 12 Monterey, and Composer needs it.

Check first

command -v composer php
php --version
ls composer.phar 2>/dev/null

If php is not found either, fix that first: zsh: command not found: php.

Fix 1: install Composer with Homebrew

The simplest route on a Mac. Homebrew's composer formula depends on its php formula, so one command installs both:

brew install composer
rehash
composer --version

Both land in /opt/homebrew/bin on Apple silicon and /usr/local/bin on Intel Macs. If the install worked and composer is still not found, that folder is not on your PATH; add Homebrew's setup line to ~/.zprofile and open a new tab:

eval "$(/opt/homebrew/bin/brew shellenv)"

Fix 2: you already have composer.phar

Composer's download page gives a short PHP script that writes composer.phar into the folder you ran it from. That file works with php composer.phar install, but composer on its own will not find it. Move it onto PATH as a file called composer:

sudo mkdir -p /usr/local/bin
sudo mv composer.phar /usr/local/bin/composer
composer --version

On Apple silicon Macs without Homebrew, /usr/local/bin may not exist yet; mkdir -p creates it. /usr/local/bin is on the default macOS PATH through /etc/paths, so no shell change is needed. To avoid sudo, use a folder in your home instead and put it on PATH in ~/.zprofile:

mkdir -p ~/.local/bin
mv composer.phar ~/.local/bin/composer
# ~/.zprofile
export PATH="$HOME/.local/bin:$PATH"

Some projects keep their own composer.phar in the repository. In that case run php composer.phar ... as the README says instead of installing a second copy.

laravel, phpunit or another global tool is not found

composer works, but zsh: command not found: laravel after composer global require laravel/installer? Global packages put their commands in Composer's own vendor/bin folder, which is not on PATH by default. Ask Composer where it is and add that folder:

composer global config bin-dir --absolute
# usually /Users/you/.composer/vendor/bin on macOS

# ~/.zprofile
export PATH="$HOME/.composer/vendor/bin:$PATH"

Use the path the first command printed if it is different (for example when COMPOSER_HOME or XDG_CONFIG_HOME is set). Open a new tab and run laravel --version.

Do not fix it with sudo composer

If composer works as your user but not under sudo, leave it that way. sudo can run commands with a different PATH, and running Composer as root leaves root-owned files in vendor/ that your user cannot update later.

Why ~/.zprofile

Terminal.app, iTerm2 and the VS Code terminal start zsh as a login shell, which reads ~/.zprofile and then ~/.zshrc. Keeping every PATH change in ~/.zprofile gives you one place to look. The full order, and why a Mac migrated from Intel can pick the wrong PHP, is in zsh: command not found on macOS: how PATH works.

Sources

related.

Ruslan

Ruslan

About the author