1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Changing the PHP version in CLI
This guide explains how to modify the PHP version directly via command line when running PHP scripts using PHP CLI (Command Line Interface). This can be useful when you need to temporarily modify certain settings for a specific script or PHP session.
Modify the PHP Version Used in CLI
To change the PHP version used in command line (CLI) via SSH:
- Connect via SSH (help)
- Create a
.profile
file if it doesn't exist: touch ~/.profile - Edit the
.profile
file: nano ~/.profile - Add this code inside the file to choose the desired PHP version: export PATH=/opt/php7.4/bin:$PATH
- Load the
.profile
file: source ~/.profile - Verify the PHP version: php -v
Interactive Mode
The .profile
file mentioned above is only loaded when the user interface is used. In SSH (login mode), it's the .bash_profile
file that is sourced. And in interactive mode (non-login), it's the .bashrc
file that is sourced.
Some server deployment scripts open sessions in interactive mode (non-login), and environment variables are not loaded into the PATH even if the SSH session is correctly configured.
In this case, you need to load the PHP version into the .bashrc
file (export PATH="/opt/php7.4/bin:$PATH"
), and source the .bashrc
file in the .bash_profile
and .profile
files (. ~/.bashrc
).
After following these steps, the PHP version you specified should be loaded into your shell environment whenever you open a new session. This will allow you to run scripts using this PHP version in command line (CLI).
Mention of PHP Version
If you only use the mention php
, the release preceding the one that was already in place will be installed (latest version -1): if the latest installed version is 8.1, it's 8.0 that will be used with "php". It's preferable to use the mention php-7.0
, php-7.1
, etc. so that the version doesn't change with each update.