See all UNIX related articles/faq
I have multiple versions of PHP installed on my development VM. I am migrating all my projects from PHP 7.x to 8.x and testing it. However, this leads to specific problems. For example, my production Linux container is on PHP 7.4.26, while the development VM is on PHP 8.1. So they don’t match and causes problem even in the dev environment because I have 7.x and 8.x. Here is how to tell the composer to use different PHP versions per project requirements.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements PHP with Composer
Est. reading time 4 minutes

Finding PHP version

Here is how to check PHP version on your Linux box:
php7 -v
php8 -v

From Alpine Linux VM:
Finding PHP version to Tell Composer to use Different PHP Version
Then I found out that `composer` on Alpine Linux dev VM hard-coded with PHP 8 path:

/usr/bin/php8 /usr/bin/composer.phar "$@"

That causes all sorts of issues when you want to test with PHP 7.4.

Forcing composer to use a specific PHP version

Here is how to force and tell composer to use a specific PHP version on Linux or Unix:

  1. Use the type command to find path to PHP:
    type -a php7
  2. Then find composer.phar using the find command:
    find / -type f -name "composer.phar" 2>/dev/null
  3. Use the cd command to change to project/dev/prod directory. For example:
    cd wiki
  4. Finally, tell the composer to use specific PHP version:
    /usr/bin/php7 /usr/bin/composer.phar update
How to force Composer to use a specific PHP version on Linux dev box

Click to enlarge

Of course, running composer as the root user is not recommended for security reasons, so I will run it as an nginx user. For instance:

runuser -u {USER_NAME_HERE} -- /usr/bin/php7 /usr/bin/composer.phar update --no-dev
runuser -u devuser -- /usr/bin/php7 /usr/bin/composer.phar update --no-dev
runuser -u nginx -- /usr/bin/php7 /usr/bin/composer.phar update --no-dev

Tell composer to use Different PHP Version such as 8

The steps are the same except using the php8 or php8.1 path as per your Linux or Unix version:

# 1. Find PHP 8 path
type -a php8
# 2. Find composer.phar path
type -a composer.phar
## OR ##
find / -type f -name "composer.phar" 2>/dev/null
# 3. Run it
/usr/bin/php8 /usr/bin/composer.phar update 
## OR ##
runuser -u nginx -- /usr/bin/php8 /usr/bin/composer.phar update --no-dev

How to enforce a PHP Version for installed composer packages using composer.json

Edit the composer.json and update it as follows under config:

	"config": {
		"optimize-autoloader": true,
		"prepend-autoloader": false,
		"platform": {
			"php": "8.0.13"

In the above example, I am telling Composer to use the 8.0.13 PHP version with my app by using the platform configuration in our composer.json file. However, you don’t have to edit this file by hand using a text editor such as nano or vim. Instead, you can type the following command where composer.json file is located:
cd /path/to/project/dir
ls -l composer.json
composer config platform.php {PHP_VERSION_HERE}
composer config platform.php 8.0.13

Now you can run any composer command as per your needs. Do check this platform documentation for more info. Here is how to tell composer to ignore the version requirements too:
composer update --ignore-platform-reqs --no-dev
## OR ##
composer install --ignore-platform-reqs

Getting help

Use the help command composer -h
The help command displays help for a given command:
php /usr/bin/composer.phar help list
php7 /usr/bin/composer.phar help list
php8 /usr/bin/composer.phar help list

Summing up

I used the first method, and it was handy when I wanted to test wiki under different versions of PHP. Once I am satisfied with version requirements and customization, I can build Linux containers as per my need and finally switch to PHP 8 or 8.1. Here is my bash script for downloading and testing stuff:

Sample shell script

# A sample shell script for dev machine/VM to test various PHP versions 
# *******************************************************************************
# DO NOT USE OR CALL WHEN DOING DOCKER IMAGE BUILD (e.g. docker build -t wiki .)
# *******************************************************************************
# Author: Vivek Gite, under GPL 2.x+
# --------------------------------------------------------------------------------
set -e
VERSION="${1:?Error must pass at least Mediawiki version as CLI arg.}"
PHP="/usr/bin/php7" # change php version here. e.g. php8 and then update requirements 
cd /tmp/
[ -f "$DL" ]  && { echo "Removing existing $DL..."; rm -rf "$DL"; }
[ -f "$DLSIG" ]  && { echo "Removing existing $DLSIG..."; rm -rf "$DLSIG"; }
echo "Downloading $DL and $DLSIG..."
wget "${VERSION%.*}/$DL" &>/dev/null
wget "${VERSION%.*}/$DLSIG" &>/dev/null
if gpg -q --verify "mediawiki-${VERSION}.zip.sig" &>/dev/null
	echo "Unzipping $DL ..."
	unzip "$DL" &>/dev/null
    # i've files in ~/prod/ so we can overwrite it for testing the PHP 8 or 7 here #
	echo "Installing/overwriting with new files ..."
	cp -ar /tmp/mediawiki-"${VERSION}"/* "$DEST/"
	echo "Running composer and then updating wiki ..."
	cd "$DEST/"
	runuser -u "$WWW_USR" -- $PHP /usr/bin/composer.phar update --no-dev
	runuser -u "$WWW_USR" -- $PHP maintenance/update.php
	read -r -p "Press any key to resume ..."
	echo "GPG verification failed for the $DL."

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.