PHP7 CLI attempts to load xdebug twice - "Cannot load Xdebug - it was already loaded"
If you recently upgraded Ubuntu and started seeing the Cannot load Xdebug - it was already loaded error in your CLI environment, you are likely facing a specific initialization sequence issue rather than a configuration duplication problem. The symptom is clear: the Zend extension fails to register because it believes itself active, yet the output of phpinfo() or php -v confirms it isn't actually providing debugging functionality until you restart the shell.
This behavior points directly to how Xdebug initializes within the CLI context. Unlike Apache modules, which load once per process, the PHP CLI binary is typically launched fresh for every command invocation. However, in certain environmentsâspecifically when using phpdbg or specific wrapper scriptsâthe initialization logic can differ. The error message appears because the extension manager attempts to inject the Xdebug module into the Zend Engine before it has finished its own internal checks. When the CLI detects that the extension is already loaded (a state often left over from a previous session in phpdbg mode or a cached state in specific build configurations), it aborts the loading sequence to prevent conflicts, resulting in that misleading error message.
The confusion often stems from the file structure. In Ubuntu-based systems, configuration files are symlinked from /etc/php/7.x/mods-available/ into /etc/php/7.x/conf.d/. The php --ini output confirms that the extension is being included via a standard .ini file, and there is indeed only one reference to xdebug.so in the entire directory tree. This rules out duplicate inclusion from multiple .ini files or php.ini directives.
A critical diagnostic step involves examining the binary paths and alternatives system. You may observe that /usr/bin/php is a symlink to /etc/alternatives/php, which in turn points to /usr/bin/php7.x. While php -v triggers the error, calling php7.x -v directly might bypass the problematic initialization path entirely. This discrepancy suggests that the issue is not with the PHP version itself, but rather with how the default binary resolves its environment or interacts with the debugger agent.
To resolve this, you should ensure that your Xdebug configuration does not inadvertently enable remote debugging features in a CLI-only context without an active debugger connection. Specifically, check if xdebug.remote_enable is set to 1. If your workflow involves running CLI scripts locally without connecting to a debugger (like Xdebug Cloud or a remote IDE), this setting can trigger the "already loaded" state because Xdebug attempts to establish a handshake that fails silently or leaves the extension in a flagged state.
The most effective fix is to disable the remote debugging features in your CLI configuration unless explicitly needed for remote profiling. You can do this by modifying the xdebug.ini file (either directly or via the mods-available symlink) to set xdebug.remote_enable=0. If you need Xdebug functionality for local debugging, ensure that xdebug.remote_autostart=1 is also managed correctly, as leaving it enabled when no debugger is listening can cause initialization conflicts.
Finally, verify that you are not mixing different PHP versions in your environment. The presence of multiple php binaries or conflicting alternative configurations can lead to the CLI picking up a version that expects Xdebug while another instance has already loaded it. Cleaning up your path and ensuring the alternatives system points consistently to the intended interpreter will often clear the stale state causing the error.