How can I change php-cli version on Ubuntu 14.04?

How can I change php-cli version on Ubuntu 14.04?

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

Stefan Bogdanescu

Founder & Senior Architect

Resolving PHP Version Mismatches on Ubuntu 14.04

When a web application reports one PHP version and the command line reports another, it almost always indicates that two different binaries are being invoked depending on how the request is routed. On Ubuntu 14.04, this discrepancy typically stems from the way php-fpm or Apache/Nginx invoke PHP versus what your shell's PATH environment variable prioritizes.

If you run php -v in a terminal and see version 7.0, but phpinfo() inside a script served via your web server shows 5.6, the system is correctly separating production concerns. The web server uses its own configuration to call a specific PHP binary (likely located in /usr/bin/php5.6 or similar), while your interactive shell picks up the first executable named php found in your PATH. Since most Linux distributions install newer versions later in the installation process, they often end up earlier in the PATH, causing the shell to default to 7.0 even if you need the legacy 5.6 version for a specific application.

Diagnosing the Active Binaries

Before modifying any environment variables, you must identify exactly which binaries are currently active. Run the following commands to inspect the full paths of both executables:

which php
php -i | grep 'Loaded Configuration File'

The output of which php will likely point to /usr/bin/php7.0. Meanwhile, your web server is almost certainly configured to use a binary located in /usr/bin/php5.6. You can verify this by checking the PHP configuration loaded by the web server:

php -r "echo ini_get('extension_dir');" | grep "/usr/lib"
# Or check the specific binary path if you know it
ls -la /usr/bin/php*

The goal is to ensure that when your application runs, it uses the same interpreter as the web server. If your application relies on extensions compiled specifically for PHP 5.6 (such as older versions of mbstring or pdo_mysql), switching the CLI to 7.0 can cause fatal errors like "Class 'PDO' not found" or missing function warnings.

Aligning the Command Line Environment

To make the command line use the same version as the web server, you need to adjust your shell's execution order. The most robust method involves modifying the PATH environment variable in your interactive shells (e.g., .bashrc or .zshrc).

First, ensure the older binary is accessible. On Ubuntu 14.04 with legacy packages, the PHP 5.6 binary might reside at /usr/bin/php5.6. Add this path to the beginning of your PATH:

echo "export PATH=/usr/bin:/usr/local/bin:$PATH" >> ~/.bashrc
# If php5.6 is specifically located elsewhere:
# echo "export PATH=/usr/bin/php5.6:\$PATH" >> ~/.bashrc

After editing, reload your shell configuration:

source ~/.bashrc
php -v

If the version now matches 5.6, you have successfully aligned the CLI with the web server's context. However, be cautious: hardcoding paths can lead to maintenance issues if the package manager updates locations or if multiple PHP versions coexist in /usr/bin.

The Modern Alternative: update-alternatives

A more elegant and maintainable solution involves using the Debian/Ubuntu package management utility update-alternatives. This tool allows you to manage multiple executables with a single command, preventing you from manually editing PATH every time you start a new session.

Check if alternatives are available for PHP:

sudo update-alternatives --list php

If listed, switch the default to version 5.6:

sudo update-alternatives --config php

Select the path corresponding to PHP 5.6. This approach ensures that both your CLI and any scripts invoked by the web server (if they rely on the system default) use a consistent binary without relying on fragile PATH hacks. Note that this requires root access (sudo) and assumes both versions are installed as separate packages.

Why Keeping Versions Separate Often Makes Sense

While aligning the versions solves the immediate confusion, consider whether you actually need them to match. In modern development workflows, it is common for the CLI to run a newer PHP version while the web server runs an older one. This setup allows developers to leverage new language features and security updates in their local environment while maintaining compatibility with production dependencies that may still rely on legacy PHP 5.6 extensions.

If your application was designed specifically for PHP 5.6, forcing the CLI to use that version ensures consistency during debugging. However, if you are migrating a project, it might be better to update the web server configuration to match the newer CLI version rather than downgrading the entire stack. Always test your application thoroughly after changing PHP versions, as extension compatibility and opcode caching behavior can vary significantly between major releases.

For further reading on managing PHP environments in Laravel projects, refer to https://laravelcompany.com for best practices on environment isolation and deployment strategies.

Our network