Coexisting PHP Versions in a Single Environment
Upgrading from PHP 7 to PHP 8 is often a straightforward process, yet it frequently leaves you in a state where the default php binary points to version 8 while older versions like 7.4 remain installed in /usr/bin. This specific configuration creates a paradox: your application code relies on modern features like named arguments or the match expression, but system-level services like Docker and VirtualBox depend on the legacy PHP-FPM service to initialize correctly.
When you attempt to install these packages on Ubuntu 20 (or similar distributions), the installation scripts invoke php7.4-fpm.service, which fails because the symlink resolution or environment expectations conflict with the current default binary pointing to PHP 8. You aren't asking for a downgrade of your application stack; you need a mechanism where PHP 8 handles web traffic and development while PHP 7.4 remains available for legacy system scripts without requiring a full virtual machine.
The core issue lies in how package managers and systemd services resolve the php command versus specific versioned binaries. On Debian-based systems, /usr/bin/php is typically a symlink managed by the package manager (often pointing to /usr/bin/php8.x after an upgrade). System scripts, however, often hardcode paths or rely on specific service file configurations that assume the presence of php7.4-fpm.
To resolve this without isolating PHP 7 in a VM, you must manipulate the symlinks and ensure the system can distinguish between the two versions based on the command invoked. The most robust approach involves creating a distinct versioned path for PHP 8 while restoring or maintaining the specific php7.4 binary where expected.
Restoring Specific Version Binaries
The first step is to inspect the current state of your /usr/bin directory. You will likely see a symlink named php pointing to the 8.x version and potentially a separate php7.4 binary that is either missing or broken.
To allow system tools to find PHP 7.4 explicitly, ensure the specific binary exists. If your package manager removed it during the upgrade, you can reinstall the specific FPM package:
sudo apt-get install php7.4-fpm
However, simply installing the package might not restore the symlink if the default php command is hardcoded to 8.x in the system path configuration. You need to verify that php7.4-fpm.service can actually start. If it fails with an exit code, check the status logs:
sudo systemctl status php7.4-fpm
journalctl -u php7.4-fpm -n 50
The error usually indicates that the service is trying to execute php which resolves to version 8, or the configuration file path is incorrect. You may need to specify the binary path directly within the .service unit file:
[Service]
ExecStart=/usr/bin/php7.4-fpm
This tells systemd exactly which binary to launch, bypassing the ambiguity of the generic php command.
Managing Default Behavior for Development
For your development environment, you want php -v to return 8.x, but system scripts to use 7.4. This requires careful management of symlinks. On many systems, /usr/bin/php is a symlink managed by update-alternatives. You can manage this group to switch the default without uninstalling versions.
Alternatively, if you prefer direct manipulation, ensure that the generic php command points to your desired 8.x version, while leaving the specific php7.4 binary intact for system calls.
# Ensure php points to PHP 8
sudo ln -sfn /usr/bin/php8.0 /usr/bin/php
# Ensure php7.4 is available explicitly if it was removed
sudo ln -sfn /usr/bin/php7.4 /usr/bin/php7.4
This setup allows you to run php for your Laravel or Symfony projects, leveraging the new JIT and JIT-related optimizations in PHP 8, while legacy scripts continue to call php7.4.
Trade-offs and System Complexity
The primary trade-off here is administrative overhead. Maintaining two active versions in a single OS environment increases the risk of configuration drift. A script that works on PHP 7 might inadvertently pick up PHP 8 if the path resolution changes, or vice versa. This is particularly relevant when deploying applications; you must ensure your .htaccess files or Nginx/Apache configurations explicitly call the correct FPM socket for each version to avoid mixing opcodes or extensions that are not compatible across versions.
Furthermore, while running both versions locally is feasible, it complicates dependency management. If you install a specific PHP extension via apt-get, it might apply globally unless you use containerization. This is why many developers eventually move toward Docker containers for each version, isolating the environments completely rather than managing them side-by-side on the host OS.
If your goal is specifically to satisfy installer requirements without heavy VM overhead, ensuring the specific binaries are accessible via their versioned paths and updating systemd unit files to reference those explicit paths is the most direct solution. This approach respects the legacy dependencies of tools like VirtualBox while allowing you to continue developing with modern PHP features.