Wednesday, January 22, 2020

Runnig multipe versions of PHP on Ubuntu

Running multiple versions of PHP on Ubuntu is fairly trivial since all supported version can be found in the main PPA for supported PHP versions. The repository can be added with

$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update

Look here1 for details. Then the relevant versions can then be installed

$ sudo apt install php7.4
$ sudo apt install php5.6

It is now possible to switch to the relevant version interactively with

$ sudo update-alternatives --config php

or alternatively directly with

$ sudo update-alternatives --set php /usr/bin/php5.6

This will change the version on the command line. Change version used by Apache with2

$ sudo a2dismod php7.4
$ sudo a2enmod php5.6
$ sudo systemctl restart apache2

Extensions

Most relevant extensions can be installed directly via apt. Fx.

$ sudo apt install php-ast

This will add the extension to all installed version of PHP the extension is supported for

Extensions via PECL3

Some extension is only available via PECL. PECL comes with the dev files for PHP, also available via apt

$ sudo apt install php7.4-dev

When using multiple versions of PHP, you need to set up the dev dependencies correctly, so extensions are installed correctly. php, phpize and php-config all need to be set to use the correct PHP version

$ sudo update-alternatives --config php
$ sudo update-alternatives --config phpize
$ sudo update-alternatives --config php-config

Extension installed via PECL is only installed for the current version of PHP. You need to manually add the extension to php.ini with (ast extension used as an example)

extension=ast.so

Manually installing extensions4

If you are compiling an extension yourself, you need to setup PHP the same way as when using PECL. After downloading the source code for the extension, build and install the extension with

$ cd extname 
$ phpize 
$ ./configure 
$ make

Remember to add the extension to php.ini

References

  1. https://launchpad.net/~ondrej/+archive/ubuntu/php
  2. https://opensenselabs.com/blog/tech/change-php-version-drupal-website-apache-nginx
  3. https://www.php.net/manual/en/install.pecl.pear.php
  4. https://www.php.net/manual/en/install.pecl.phpize.php