Switch php versions on commandline ubuntu 16.04

Switch php versions on commandline ubuntu 16.04

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

Stefan Bogdanescu

Founder & Senior Architect

Switching PHP Versions for the CLI on Ubuntu 16.04

On Ubuntu 16.04, managing multiple PHP versions is a standard requirement for testing compatibility or running legacy scripts alongside modern code. You can successfully toggle between interpreters for your Apache server by manipulating the mod_php modules. Enabling php5.6-fpm or php7.1-fpm and ensuring the corresponding libapache2-mod-php-x.x module is active allows Apache to load the correct handler based on your configuration files. Disabling one module and enabling another triggers an immediate change in behavior because Apache explicitly loads only what is enabled in its virtual host configurations.

The internal PHP development server, however, operates differently. When you execute php -S localhost:8888, the shell attempts to invoke the PHP binary located at /usr/bin/php by default. This specific executable points to a single version installed during your package installation process. If you installed PHP 7.1 as the primary package or if it was installed last, it overwrites the symlink or becomes the active binary for that path. Consequently, running php -S will consistently use PHP 7.1 regardless of which Apache module is currently enabled on the system.

To run the built-in server with a specific version without permanently altering your system binaries, you must invoke the interpreter directly using its full path or an absolute path to the executable. The most reliable method involves bypassing the generic php command and targeting the specific binary for the version you intend to use. For instance, if your system has /usr/bin/php7.1 and /usr/bin/php5.6 as distinct executables, you can run:

/usr/bin/php7.1 -S localhost:8888

This command explicitly loads the 7.1 runtime, bypassing any ambiguity regarding which version is "active" in your PATH. This approach is critical when developing applications that require specific PHP functions available only in newer versions versus those relying on deprecated features found in older releases like 5.6.

Alternatively, if you have added custom symlinks to /usr/bin/php that point to different versions depending on an environment variable, you can leverage the PHPRC or simply ensure your shell's PATH is ordered correctly. However, direct path invocation remains the most deterministic solution. You might also use the -v flag with specific binaries to verify which version is actually executing:

/usr/bin/php5.6 -S localhost:8888

It is important to note that switching between these versions on the command line does not automatically affect your Apache configuration unless you have configured a shared library setup where .so files are dynamically loaded based on environment variables. In most standard Ubuntu LAMP setups, Apache expects specific module paths defined in apache2.conf or mods-available. The CLI developer server, by contrast, is self-contained; it does not consult the Apache module list to determine which PHP engine to use.

If you are working with a Laravel project or similar framework that relies on Composer autoloaders installed specifically for one version, switching versions via direct binary path ensures that your composer.json expectations match the runtime environment. This distinction prevents common errors where a script runs successfully in Apache but fails on the CLI because the underlying engine differs.

While Ubuntu 16.04 is reaching end-of-life and no longer receives security updates, understanding these version-switching mechanisms remains relevant for maintaining legacy applications or learning environments. The principle of invoking specific binaries extends to any scenario where the default php command does not align with your testing requirements. By explicitly naming the binary path, you retain full control over which language features and extensions are available during development without needing to reconfigure system-wide Apache modules every time you start a local server.

Our network