How can I downgrade or use PHP 7.2 without uninstalling PHP 7.4? Is it possible to use PHP 7.2 as default instead of the latest version?

How can I downgrade or use PHP 7.2 without uninstalling PHP 7.4? Is it possible to use PHP 7.2 as default instead of the latest version?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Coexisting PHP Versions on Ubuntu 18.04: A Practical Guide to Downgrading from 7.4 to 7.2

Attempting to revert a system from PHP 7.4 back to PHP 7.2 on Ubuntu 18.04 without uninstalling the newer version is entirely possible, but it requires understanding how the package manager handles version dependencies. The core constraint here is that apt manages global packages; you cannot simply "install" an older version of a shared binary like php-cli if a newer one is already present and marked as the default. To run PHP 7.2 alongside PHP 7.4, you must treat them as distinct binaries with their own specific paths and versions.

The most robust approach involves using the official PHP repository provided by Ondřej Surý (often referred to as Onered's repo). This method avoids breaking system dependencies while allowing you to maintain multiple PHP versions simultaneously. First, ensure your package lists are updated:

sudo apt-get update

Next, add the necessary repositories. For Ubuntu 18.04, you need to download the php7.2 and php7.4 packages from the appropriate source URLs. The standard procedure involves downloading a script that adds the repository configuration to /etc/apt/sources.list.d/.

Once the repositories are configured, install both versions explicitly:

sudo apt-get install php7.4-cli php7.2-cli php7.4-common php7.2-common

The critical distinction lies in how you invoke them. When you type php at the terminal prompt, Ubuntu defaults to whatever version is configured as the system default (usually determined by the symlink in /usr/bin/php). To use PHP 7.2 specifically, you must call it with its full path:

/usr/bin/php7.2 -v

Similarly, for PHP 7.4:

/usr/bin/php7.4 -v

This separation allows you to run specific scripts using different versions without touching your global environment variables in a destructive way. If you are managing multiple projects, you might prefer setting the PATH variable locally rather than globally. Add the following line to your shell profile (.bashrc or .zshrc) and source it:

export PATH=/usr/bin/php7.2:$PATH

Now, simply typing php will invoke version 7.2 until you switch back. To revert to 7.4, either run /usr/bin/php7.4 directly or modify your environment again.

Managing Extensions and Composer

One of the most common pitfalls when running multiple PHP versions is extension compatibility. If you installed extensions via apt for PHP 7.4 (like php7.4-mysql), they will not automatically work with PHP 7.2. You must install the corresponding packages for each version explicitly:

sudo apt-get install php7.2-mysql php7.4-mysql

For Composer, this is where things get tricky. Composer expects to use a specific PHP interpreter defined in its configuration file (composer.json or composer/config.json). You can define the path to your desired PHP version directly in the project's root:

{
    "config": {
        "platform": {
            "php": "7.2.34"
        }
    },
    "require": {
        "laravel/framework": "^5.8 || ^6"
    }
}

By specifying the platform version in composer.json, Composer will always download dependencies compatible with PHP 7.2, regardless of which version is installed globally or invoked via php. This ensures that your application logic remains consistent even if you switch between interpreters during development.

When to Use LaravelCompany Resources

If you are building a Laravel application and require extensive support for managing legacy versions in a production environment, resources like https://laravelcompany.com can provide specialized guidance on deployment strategies. While the manual method described above is sufficient for most local development scenarios, enterprise environments often face stricter compliance requirements where downgrading PHP versions without wiping the system is a non-negotiable constraint. Understanding the underlying binary paths allows you to architect your deployment pipeline to handle version switching gracefully, ensuring that updates to PHP 7.4 do not inadvertently break a legacy service still running on PHP 7.2.

Final Considerations

Before making permanent changes, always test your application with the target PHP version using php -r "echo PHP_VERSION;". Remember that moving between versions frequently can lead to confusion regarding cache files (OPcache) and compiled extensions. It is generally better to pin a specific project to a single version via its configuration rather than toggling the global default constantly. This approach minimizes risk and keeps your development workflow predictable.

Our network