Install multiple PHP versions on Ubuntu

0
6940
Install multiple PHP versions on Ubuntu
Install multiple PHP versions on Ubuntu

You want to run several projects using different PHP versions? No problem, I will show you how to install multiple PHP versions on Ubuntu Linux.

Pre-requisites

Let’s update repository before installing PHP.


$ sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https 

$ LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php

$ sudo apt update

The above commands will add Ondrej PPA, which is the main PPA for supported PHP versions with many PECL extensions.

In addition, this guide only works for following Ubuntu version:

  • 22.04
  • 21.04
  • 20.04
  • 18.04

Install multiple PHP versions

At the time of writing this post, the follow PHP package versions are available to be installed

  • php5.6
  • php7.0
  • php7.1
  • php7.2
  • php7.3
  • php7.4
  • php8.0
  • php8.1

To install the PHP versions of your choice, just issue the command with above package name:

$ sudo apt install php8.1 

$ sudo apt install php8.0

$ sudo apt install php7.4

$ sudo apt install php5.6

...

Just install one or more PHP versions if you need, there will not have any conflict at all, since the PHP binary will be named with corresponding versions.

$ php8.1 --version
PHP 8.1.6 (cli) (built: May 17 2022 16:46:54) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.6, Copyright (c), by Zend Technologies

$ php7.4 --version
PHP 7.4.29 (cli) (built: Apr 28 2022 11:47:05) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.29, Copyright (c), by Zend Technologies

It’s cool, right?

Install PHP extensions

Your PHP applications need some PHP extensions to be installed as well. The package is named with following format:

php{VERSION}-{EXTENSION_NAME}

Replace {VERSION} and {EXTENSION_NAME} with appropriate values.

These are the available PHP extension names you can look up:

- amqp
- apcu
- ast
- bcmath
- bz2
- cgi
- cli
- common
- curl
- dba
- decimal
- dev
- ds
- enchant
- facedetect
- fpm
- gd
- gearman
- gmagick
- gmp
- gnupg
- grpc
- http
- igbinary
- imagick
- imap
- inotify
- interbase
- intl
- ldap
- mailparse
- maxminddb
- mbstring
- mcrypt
- memcache
- memcached (this one uses libmemcached)
- mongodb
- msgpack
- mysql
- oauth
- odbc
- opcache
- pcov
- pgsql
- phpdbg
- protobuf
- ps
- pspell
- psr
- raphf
- readline
- redis
- rrd
- smbclient
- snmp
- soap
- solr
- sqlite3
- ssh2
- swoole
- sybase
- tideways
- tidy
- uopz
- uploadprogress
- uuid
- xdebug
- xhprof
- xml
- xmlrpc
- xsl
- yac
- yaml
- zip
- zmq
- zstd

Be aware that some extensions might not be available on some PHP versions.

Use the following example command to install PHP extensions

$ sudo apt install -y -q php8.1-{cli,common,fpm,mysql,mbstring,bcmath,xml,curl,imagick}

You can also determine which extensions are available for a specific PHP version by specifying the php version as prefix for package search, something like this:

$ sudo apt search php7.4

There you can look up for all available extensions with prefix version in package name.

Switch default PHP version for CLI

For command-line interface, you might notice there is a default command:

$ php -v

PHP 5.6.40-57+ubuntu22.04.1+deb.sury.org+1.10 (cli)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

You can change to another PHP version by issuing following command:

$ sudo update-alternatives --config php

There are 4 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
  0            /usr/bin/php8.1   81        auto mode
* 1            /usr/bin/php5.6   56        manual mode
  2            /usr/bin/php7.3   73        manual mode
  3            /usr/bin/php7.4   74        manual mode
  4            /usr/bin/php8.1   81        manual mode

Press <enter> to keep the current choice[*], or type selection number: _

It will ask you to prompt in the option of the PHP version for CLI, just select the version of your choice and press Enter.

Location of PHP configuration files (INI file)

Each PHP versions is installed under a separate directory, and same thing applied to the configuration file.

You can find them at these directories:

# For PHP CLI
/etc/php/VERSION/cli/php.ini

# For php-fpm (work with nginx)
/etc/php/VERSION/fpm/php.ini

# For Apache
 /etc/php/VERSION/apache2/php.ini

Add custom PHP config file

By default, PHP config will load all .ini files inside PHP_ROOT_DIRECTORY/conf.d , so you only need to put your custom.ini file into that directory.

# For PHP CLI 
/etc/php/VERSION/cli/conf.d/custom.ini

# For php-fpm
/etc/php/VERSION/fpm/conf.d/custom.ini

# For Apache 
/etc/php/VERSION/apache2/conf.d/custom.ini

Location of php-fpm sock

One more thing, if you use nginx and want to specify php-fpm sock location, you can locate them here:

$ ls -asl /var/run/php
total 16
0 drwxr-xr-x  2 www-data www-data  220 May 29 03:10 .
0 drwxr-xr-x 31 root     root     1000 May 29 06:23 ..
0 lrwxrwxrwx  1 root     root       30 May 28 04:49 php-fpm.sock -> /etc/alternatives/php-fpm.sock
4 -rw-r--r--  1 root     root        5 May 28 21:14 php5.6-fpm.pid
0 srw-rw----  1 www-data www-data    0 May 28 21:14 php5.6-fpm.sock
4 -rw-r--r--  1 root     root        5 May 28 21:04 php7.3-fpm.pid
0 srw-rw----  1 www-data www-data    0 May 28 21:04 php7.3-fpm.sock
4 -rw-r--r--  1 root     root        5 May 28 20:32 php7.4-fpm.pid
0 srw-rw----  1 www-data www-data    0 May 28 20:32 php7.4-fpm.sock
4 -rw-r--r--  1 root     root        6 May 29 03:10 php8.1-fpm.pid
0 srw-rw----  1 www-data www-data    0 May 29 03:10 php8.1-fpm.sock

Uninstall PHP

The process of uninstalling PHP and its extensions is similar to install.

$ sudo apt remove php{VERSION} php{VERSION}-{EXTENSION_NAME}

Example:

$ sudo apt remove php8.1 php8.1-fpm php8.1-cli

You can also uninstall everything of a specific PHP version using this handy command:

$ sudo apt remove php{VERSION} php{VERSION}-*

Example:

$ sudo apt remove php8.1 php8.1-*

Conclusion

As you can, it is pretty easy to install multiple PHP versions on Ubuntu. This post has shown you everything you need to know to manage PHP.

Enjoy!