1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Change the PHP version in CLI
This guide explains how to change the PHP version used in the command line (PHP CLI) on an Infomaniak Web Hosting.
Preamble
- This guide is useful if you need to temporarily adjust settings for a specific script or for a PHP session executed in the command line (CLI).
- To modify the general PHP version used by your hosting on the web server (FPM/Apache) via the Infomaniak Manager, refer to this other guide.
Default PHP version in command line
When you run the php command (usually via /usr/bin/php), it is the default PHP version configured on the server that is used. This version may evolve over time according to the platform updates.
To find out the exact version currently active on your hosting, run:
php -vTo ensure the stability of your scripts, it is recommended to use an explicit version (php7.4, php8.0, php8.1, etc.) or to adjust your PATH variable to point to the directory of the desired version (for example /opt/php8.1/bin).
Modify the PHP version used in CLI
There are two main files that can be used to configure the PHP version automatically loaded in your SSH session:
1. Using ~/.bashrc (recommended)
The .bashrc file is read by Bash when opening an interactive shell (non-login), i.e., in the majority of cases when you open a normal SSH session or execute commands via deployment tools.
Create the
~/.bashrcfile if it does not exist, then open it:touch ~/.bashrc nano ~/.bashrcAdd the following line to specify the desired PHP version (example: PHP 8.1):
export PATH="/opt/php8.1/bin:$PATH"Reload your environment:
source ~/.bashrcCheck the currently used version:
php -v which phpYou should see a path of the type
/opt/php8.1/bin/php.
2. Using ~/.profile (alternative)
The .profile file is read only when the shell is launched in login mode (for example during an initial SSH connection). If your environment does not automatically load .bashrc, you can define the PHP version directly there.
Create the
~/.profilefile if it does not exist, then open it:touch ~/.profile nano ~/.profileAdd the following line:
export PATH="/opt/php8.1/bin:$PATH"Reload your environment:
source ~/.profile
3. Load .bashrc from other profiles
To ensure that the configuration is loaded in all types of sessions (login and non-login), it is recommended to include in your ~/.bash_profile and ~/.profile files the following line:
if [ -f ~/.bashrc ]; then . ~/.bashrc; fiThus, your PHP configuration defined in .bashrc will always be applied, regardless of how the SSH session is opened.
Run a specific version occasionally
If you want to run a script with a specific version of PHP without modifying your environment, you can call the corresponding binary directly:
/opt/php8.1/bin/php mon_script.php
/opt/php8.2/bin/php -vAfter these steps, the chosen PHP version will be loaded automatically each time a new session is opened, and your CLI scripts will run with the desired version.