Isolate PHP Versions Without Modifying PATH or Files
When managing legacy environments, specifically on systems like Gentoo where multiple PHP versions coexist in /usr/bin/, you often face a specific friction point: mixing interpreters during package installation. The scenario is common enough that it warrants a structured approach rather than ad-hoc hacks. You have a default php pointing to 5.4, but your Composer project (like Laravel) requires PHP 5.6 or higher.
The instinctive solutionâinvoking the target interpreter directly for the initial commandâfails because Composer relies on environment variables and process inheritance for subsequent steps. When you run /usr/bin/php5.6 /usr/bin/composer create-project ..., the first step works. However, immediately after that, Laravel's composer.json triggers post-install commands like php artisan optimize. These commands default to the shell's current php binary (version 5.4), causing a crash because the shebangs in Laravelâs CLI tools (#!/usr/bin/env php) resolve to whatever is first in your $PATH.
You need a way to force the execution of PHP 5.6 for the entire session lifecycle without altering global environment variables like $PATH or modifying core files like composer or artisan. Since you are operating as a regular user, you also cannot touch system directories with elevated privileges.
The Environment Variable Solution
The most direct method to achieve this is by manipulating the PHP_BINARY_PATH or, more commonly, the PATH variable for just the current shell session using command substitution. However, since you explicitly stated you do not want to modify $PATH, we must look at how Composer and PHP determine which binary to use when no explicit path is provided.
Composer does not inherently track which PHP version was used to start it unless you tell it where the executable lives. The standard solution involves setting the COMPOSER_BIN environment variable or, more robustly for this specific isolation requirement, using an alias or a wrapper script that intercepts the php command locally.
However, there is a cleaner approach that respects your constraints: Prefixing the commands. If you cannot modify $PATH, you can create a local shell function or alias in your current interactive shell (e.g., .bashrc or .zshrc loaded for the session).
But if you need this strictly for a one-off command without sourcing a config file, you must rely on alias within the command chain itself or use env.
Using env to Inject PHP into PATH Temporarily
The most effective trick that adheres to your constraints is using the env command to inject a temporary entry into $PATH specifically for the duration of the Composer command. This avoids modifying your global $PATH permanently and requires no file changes.
You can construct a command like this:
/usr/bin/composer create-project laravel/laravel --prefer-dist \
<(echo "export PATH=/usr/bin:/usr/bin/php5.6:$PATH")
Wait, that syntax is incorrect for exporting inside a subshell passed to another command. A more precise way to inject the specific binary without touching global config is to ensure env sees the correct path first, or simply invoke Composer with the full path to the desired PHP binary and pass it arguments that force it to use that version for subsequent calls if possible.
Actually, Composer does not automatically switch its internal PHP reference based on how it was invoked unless configured. The standard workaround when you cannot modify $PATH is to create a wrapper script in your current working directory. This script simply executes the target PHP binary and forwards all arguments.
Creating a Local Wrapper Script
Since you cannot modify system files, create a file named php in your home directory (e.g., ~/php). Make it executable:
mkdir -p ~/bin
echo '#!/bin/bash' > ~/bin/php
echo 'exec /usr/bin/php5.6 "$@"' >> ~/bin/php
chmod +x ~/bin/php
Then, add your home directory to the beginning of $PATH for this session only using env:
env -i PATH=~/bin:$PATH /usr/bin/composer create-project laravel/laravel --prefer-dist
Warning: Using -i clears all environment variables. If you don't want to clear everything, specify only the new path:
env PATH=~/bin:$PATH /usr/bin/composer create-project laravel/laravel --prefer-dist
This forces any subsequent php call (whether coming from Composer's post-install hooks or Laravel's Artisan CLI) to resolve against ~/bin/php, which points directly to /usr/bin/php5.6. Because this file lives in your home directory, you have full control as a regular user without needing root permissions.
Why This Works
When Laravel executes php artisan optimize, the shell searches for the executable named php in $PATH. By prepending ~/bin to $PATH via env, you ensure that ~/bin/php is found before /usr/bin/php. The script inside ~/bin/php then explicitly calls /usr/bin/php5.6.
This approach isolates the change to the current session and your user space. It satisfies the requirement of not modifying global $PATH permanently, not touching Composer binaries, and not altering Laravel's source files. It is a robust pattern for legacy system integration where version isolation is critical.
