PHP error guide
PHP shebang bad interpreter: meaning and fix
Error summary
Point the shebang to an existing stable PHP CLI path, use Unix line endings, and keep the script executable only where direct execution is intended.
What it means
Point the shebang to an existing stable PHP CLI path, use Unix line endings, and keep the script executable only where direct execution is intended.
What the error means
The kernel cannot execute the interpreter path from the first line, often because the path is wrong or ends with a hidden carriage return.
Why PHP produces it
Direct script execution delegates to a shebang interpreter that does not exist exactly as encoded.
PHP version notes
The behavior described for PHP shebang bad interpreter applies to PHP 7.4–8.4 unless a narrower version is stated; exact wording can vary by SAPI and patch release.
Most common causes
- PHP is installed under a versioned path.
- The shebang has CRLF line endings and resolves as php\r.
- The script moved between hosts with different interpreter layouts.
Minimal examples
BAD — reproduces the problem
#!/usr/local/bin/php-that-does-not-exist
<?php
FIXED — safer pattern
#!/usr/bin/env php
<?php
Step-by-step diagnosis
- Inspect the first line as bytes to detect carriage returns.
- Verify the interpreter path with test -x.
- Run the interpreter explicitly to separate shebang and PHP errors.
Fixes
Use a valid portable shebang where policy permits
Choose an explicit managed path or env lookup with a controlled PATH.
#!/usr/bin/env php
<?php
declare(strict_types=1);
Common mistakes when fixing it
- Using env with an untrusted PATH.
- Fixing permissions when the interpreter path is the failure.
- Leaving CRLF on the first line.
How to prevent it
- Validate executable entry points during deployment.
- Enforce line endings in version control.
- Pin the binary explicitly in security-sensitive jobs.
Web server / environment notes
cli, linux. The operating system processes the shebang before PHP starts.
Tags: cli,linux