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.

 

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 -v

To 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.

  1. Create the ~/.bashrc file if it does not exist, then open it:

    touch ~/.bashrc
    nano ~/.bashrc
  2. Add the following line to specify the desired PHP version (example: PHP 8.1):

    export PATH="/opt/php8.1/bin:$PATH"
  3. Reload your environment:

    source ~/.bashrc
  4. Check the currently used version:

    php -v
    which php

    You 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.

  1. Create the ~/.profile file if it does not exist, then open it:

    touch ~/.profile
    nano ~/.profile
  2. Add the following line:

    export PATH="/opt/php8.1/bin:$PATH"
  3. 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; fi

Thus, 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 -v

 

After 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.


Has this FAQ been helpful?