Visual Studio Code: Unable to locate phpcs
When you encounter the error "Unable to locate phpcs" in Visual Studio Code, the environment variable warning is a clear signal that your shell cannot find the executable. This often happens when you install PHP_CodeSniffer via Composer within a project directory but then try to run it from outside that specific context. The error message explicitly suggests two paths forward: adding phpcs to your global path or using Composer to install it locally.
To resolve this, you must understand how VS Code executes external commands. Unlike a terminal session where you might have activated a virtual environment globally, the editor launches new instances of its processes in isolated contexts. If phpcs is only installed as a dependency for a specific project and not globally accessible, the VS Code task runner or linting provider cannot locate it unless the path is explicitly configured.
The most robust solution depends on your workflow requirements. If you need to run PHPCS across multiple projects without managing dependencies in every folder, installing it globally is the standard approach. Open your terminal within VS Code and run:
composer global require squizlabs/php_codesniffer
This command installs the tool into a directory managed by Composer's global vendor bin folder, typically located at ~/.config/composer/vendor/bin on Linux or ~/.composer/vendor/bin on Windows. Once installed, you should be able to run phpcs --version in any terminal session. However, simply having it installed globally does not always mean VS Code can see it immediately. You may need to ensure the Composer autoload script is generated to populate the bin directory correctly:
composer global dump-autoload --classmap-authoritative
If you prefer to keep your development environment clean and avoid polluting the global path with CLI tools, the local installation method is often superior. In this scenario, you run composer require --dev squizlabs/php_codesniffer inside your project's root directory. This places the binary in vendor/bin. To make VS Code aware of this local executable without manually editing configuration files every time, you must add a specific entry to your .vscode/settings.json.
Create or update the settings file with the following JSON structure:
{
"phpcs.executableFromPath": "vendor/bin/phpcs",
"phpcs.standard": "PSR12"
}
This configuration tells the PHPCS extension to look inside the vendor folder relative to the workspace root. It is a critical distinction: without this setting, the extension defaults to searching the system PATH first. If your global installation exists but the local one does not, you might see the error if the extension prioritizes the wrong location or if the global binary has permission issues.
A common pitfall involves Composer's autoloading behavior. When using local dependencies, ensure that vendor/autoload.php exists and that the bin directory is populated. If you recently updated Composer or PHP versions, the executable path might have shifted, causing the "Unable to locate" error even if the tool is installed. Re-running composer install or composer update in the project root usually resolves stale references.
For teams adopting strict containerization practices like Docker, the solution shifts again. You must ensure that the Docker image you are using has Composer and the necessary PHP extensions pre-installed. Running the Composer command inside your build script guarantees that vendor/bin/phpcs exists before VS Code attempts to invoke it.
If you find yourself constantly toggling between global and local setups, consider standardizing on a specific approach for your organization. Local installation via Composer is generally preferred in modern PHP development because it ensures version consistency across team members and prevents conflicts with system-wide tools.
