# Fix PHP Version Mismatch for Composer Dependencies

> Resolve Composer PHP version errors by upgrading PHP, managing multiple versions with PATH, and configuring platform settings in composer.json

**URL:** https://sentry.io/answers/how-do-i-fix-the-php-version-for-composer-dependencies/

---

## The Problem

You may encounter the following error working with Laravel, or PHP applications in general: `Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= `. This is common in cloud services like shared web hosts, Digital Ocean, or AWS. It is especially confusing when you already have the latest version of PHP installed.

There are a few causes of this error message:
- Your PHP version is too old.
- You have multiple versions of PHP installed and are using the wrong one.
- The PHP version specified in your `composer.json` file is incorrect.

Since Composer (the PHP dependency manager) uses the CLI version of PHP, neither your Apache PHP module version nor your Nginx PHP-FPM (PHP FastCGI Process Manager) version affects this problem. The Apache and Nginx PHP versions affect only web requests, not dependency resolution.

## The Solution

The solution to this configuration error differs depending on what system you are running on.

### Local Machine

First, consider a local machine where you have full control, including a Docker container or a virtual machine.

Check the PHP version installed by running the command below in a terminal:

```sh
php -v
```

If your PHP version is older than the one required by Composer, you need to upgrade it. To see how to install PHP on your operating system, please follow [the documentation](https://www.php.net/manual/en/install.php).

Upgrading PHP, then your web server and PHP modules can be complicated. The easiest solution is to use a prepackaged Docker image for Laravel that has the latest version of PHP, such as [this popular option](https://hub.docker.com/r/bitnami/laravel).

#### Upgrade PHP With a Package Manager

If you want to upgrade PHP instead, and you are using a Debian-based server or Docker container, installation should be as simple as running the commands below:

```sh
apt install software-properties-common
apt update
apt install php
```

If you are using a RedHat-based system, the commands should be similar, but use `yum`.

#### Upgrade PHP from Source Code

If the available PHP package is not recent enough, you can install PHP from the source code. To do this, download [the latest `.tar.gz` file](https://www.php.net/downloads). Extract it, change into its folder, configure it, and install it. Below are the commands to do this (change 8.3.4 to whatever the latest version is):

```sh
apt update
apt install gcc pkg-config make libxml2 libxml2-dev sqlite3 -y
curl https://www.php.net/distributions/php-8.3.4.tar.gz -o php.tar.gz
tar -xvf php.tar.gz
cd php-8.3.4
./configure
make install
```

If `configure` says your SQLite version is too old, install the latest version of SQLite with the code below, then rerun the PHP configure command:

```sh
curl https://www.sqlite.org/2024/sqlite-autoconf-3450200.tar.gz -o sqlite.tar.gz
tar -xvf sqlite.tar.gz
cd sqlite-autoconf-3450200
./configure
make install
```

The latest PHP should now be available on your system.

#### Handle Multiple Versions of PHP

Your computer, virtual machine, or container might have multiple versions of PHP. To ensure that you are using the latest one, you need to change your PATH environment variable. Where the operating system first finds an executable file called `php` in your path is where it will call `php` from, regardless of whether that version is the latest. If the latest version of PHP is installed to `usr/local/bin`, ensure that the folder is first in your path.

You can do this for the root user by running the following command to alter the root `.bashrc` file:

```sh
echo 'export PATH="/usr/local/bin:$PATH"' >> /root/.bashrc
```

Test this works by running `php -v` again in a new terminal.

#### Test Your Web Server

If your web server runs under a different user that is not root, update that user's configuration files to point to the latest version of PHP.

You also need to ensure that your web server, most likely Apache or Nginx, is compatible with the new version of PHP and any associated modules. If the server shows errors when browsing your web application, please follow the documentation for your web server to upgrade PHP, and then restart the server.

#### Upgrade Your Composer Dependencies

Once PHP is upgraded, get the latest version of all your Composer dependencies and refresh the cache by running the commands below:

```sh
composer update
composer global update
composer dump-autoload
```

### Do Not Hide the Composer Warnings

You might find advice online to add `"platform-check": false` to your `composer.json` file or run Composer with `--ignore-platform-reqs`. Do not do this — it will almost certainly cause errors at runtime.

If you are certain that the version of PHP set in your `composer.json` file is incorrect, you can change it to the version you want in the following field:

```js
"config": {
  "platform": {
    "php": "7.1.0"
  }
},
```

### Shared Web Host

If your Composer dependencies work locally and your application runs, but you have errors on your remote web host, you need to set the correct version of PHP there. If you are using a web host where you have full control, such as AWS or Digital Ocean, you can upgrade PHP (or preferably use a Docker container with the latest PHP) as noted earlier.

If you are using a shared web host, which you can most likely configure through cPanel, you need to set PHP through the web interface.

Log in to cPanel on your host and browse to **Software —> MultiPHP Manager** or similar, depending on your version of cPanel.

In the PHP Selector page that opens, set the `Current PHP version` field to the highest version of PHP available. If the version you want is unavailable, email your host administrator and ask them to install it.

---

*Source: [sentry.io/answers/how-do-i-fix-the-php-version-for-composer-dependencies/](https://sentry.io/answers/how-do-i-fix-the-php-version-for-composer-dependencies/)*
