How to use earlier version of PHP with Composer

If you are running the latest version of PHP on your Windows system but need to run an older version of PHP for Composer when installing an older project with outdated dependencies, here’s a quick fix that does not require mucking with environment variables.

Download the version of PHP your project requires and place it in an easily accessible folder. For me, that was this archive:

I’m running Windows 11 and have a simple bin folder in my user directory where I drop this kind of thing.

Clone your repository, then run composer in this way from Git Bash:

~/bin/php/7.4.27/php -f composer install

Composer will install all dependencies without complaint, although it may warn you that some packages have newer versions. Sometimes, you simply need to do this for the install, and can run the app happily on a newer version of PHP. But if you need the older version to run it, too, then you can fire it up like this:

~/bin/php/7.4.27/php -S localhost:8000 -t public # or, on Laravel... ~/bin/php/7.4.27/php artisan serve

You can make this even easier by adding an alias in your Git Bash .profile.

alias php7="~/bin/php/7.4.27/php"

Which you can now use like this:

php7 -f composer install php7 artisan serve

Why I used this solution

I had to make a quick fix to a live Laravel project that had been happily running for four years:

  • Laravel 5.6
  • PHP ^7.1.3

The server on which the project runs was using php 8.1, but Composer squawked when running composer install on the cloned repository and demanded an update. I just didn’t have the time to update the project to a newer version of Laravel (something I’ll look into soon), and needed a quick solution. The above steps worked just fine.