Knowledge base
1000 FAQs, 500 tutorials and instructional 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 plan.
Introduction
- Useful for configuring a specific script or a PHP session in the command-line interface (CLI).
- To change the PHP version of the web server (FPM/Apache) via the Manager, please refer to this other guide.
Default PHP CLI Version
The php command uses the server's default version. Check the active version with this command:
# Check current PHP version
php -vTo ensure the stability of your scripts, use an explicit path (e.g., php8.2) or modify your PATH variable.
Change the PHP version in CLI
You can configure the PHP version loaded automatically in your SSH session via two main files.
1. Using .bashrc (Recommended)
The ~/.bashrc file is read when an interactive shell is opened.
Open the file (or create it if it doesn't exist):
touch ~/.bashrc nano ~/.bashrcAdd this line to define the desired version (example with PHP 8.3):
export PATH="/opt/php8.3/bin:$PATH"Update the configuration:
source ~/.bashrcCheck the change:
php -v which php
2. Using .profile (Alternative)
The ~/.profile file is read during an SSH connection (login mode).
Edit the file:
nano ~/.profileAdd the export line:
export PATH="/opt/php8.3/bin:$PATH"
3. Load .bashrc systematically
To apply the configuration to all session types, add this code to your ~/.bash_profile or ~/.profile files:
# Load .bashrc if it exists
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
Run a specific version temporarily
To run a script with a specific version without changing your global environment, call the binary directly:
# Execute with a specific version
/opt/php8.2/bin/php my_script.php
/opt/php8.3/bin/php -vOnce these steps are completed, your SSH sessions and CLI scripts will use the selected PHP version by default.
Link to this FAQ: https://faq.infomaniak.com/2108
Has this FAQ been helpful?