Mario
Mario

Reputation: 51

Run multiple PHP versions in Alpine Linux

Is it possible to run multiple versions of PHP directly inside an Alpine Linux system? Like using php 5.6, 7.0 and 7.4 at the same time?

In ubuntu, this works just fine without any problems. This can be done by using the ppa repository for example. Afterwards different PHP versions can be installed easily over the apt package manager.

add-apt-repository ppa:ondrej/php
apt-get update
apt-get install php5.6
apt-get install php7.0
apt-get install php7.4

Is there a way to do this on Alpine?

Upvotes: 5

Views: 4330

Answers (1)

valiano
valiano

Reputation: 18551

On Alpine Linux repositories, only the latest version of each package is available. Old package versions are discarded, and once they do, they could no longer be installed using apk. Therefore, as opposed to Ubuntu, there is no more than one PHP version for each major PHP version (5, 7, 8) that lives in Alpine repositories.

However, you may be able to easily mix PHP 5, 7 and 8 (if you don't mind about specific minor versions).
On Alpine 3.13, the packages php7 and php8 are available (versions 7.4.15-r0 and 8.0.2-r0 at the time of writing). They could be installed with:

apk --update --no-cache add php7 php8

In addition, the php5 package is not available in Alpine 3.13, but you can attempt install from Alpine 3.8, where it is still available (version 5.6.40-r0):

apk add --update --no-cache php5 libressl2.7-libcrypto --repository=http://dl-cdn.alpinelinux.org/alpine/v3.8/community --repository=http://dl-cdn.alpinelinux.org/alpine/v3.8/main

(libressl2.7-libcrypto is a requirement of php5, which is available in the v3.8/main repository).

Putting it together, Alpine 3.13 image with PHP 5, 7 and 8:

$ sudo docker run -it alpine:3.13
/ # apk --update --no-cache add php7 php8
fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/community/x86_64/APKINDEX.tar.gz
(1/11) Installing php7-common (7.4.15-r0)
(2/11) Installing argon2-libs (20190702-r1)
(3/11) Installing ncurses-terminfo-base (6.2_p20210109-r0)
(4/11) Installing ncurses-libs (6.2_p20210109-r0)
(5/11) Installing libedit (20191231.3.1-r1)
(6/11) Installing pcre2 (10.36-r0)
(7/11) Installing xz-libs (5.2.5-r0)
(8/11) Installing libxml2 (2.9.10-r6)
(9/11) Installing php7 (7.4.15-r0)
(10/11) Installing php8-common (8.0.2-r0)
(11/11) Installing php8 (8.0.2-r0)
Executing busybox-1.32.1-r0.trigger
OK: 18 MiB in 25 packages
/ #
/ # apk add --update --no-cache php5 libressl2.7-libcrypto --repository=http://dl-cdn.alpinelinux.org/alpine/v3.8/commun
ity --repository=http://dl-cdn.alpinelinux.org/alpine/v3.8/main
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/community/x86_64/APKINDEX.tar.gz
(1/6) Installing libressl2.7-libcrypto (2.7.5-r0)
(2/6) Installing php5-common (5.6.40-r0)
(3/6) Installing pcre (8.44-r0)
(4/6) Installing readline (7.0.003-r0)
(5/6) Installing php5-cli (5.6.40-r0)
(6/6) Installing php5 (5.6.40-r0)
Executing busybox-1.32.1-r0.trigger
OK: 29 MiB in 31 packages
/ #
/ # php5 --version
PHP 5.6.40 (cli) (built: Jan 16 2019 23:59:17) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
/ #
/ # php7 --version
PHP 7.4.15 (cli) (built: Feb  4 2021 20:58:13) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
/ #
/ # php8 --version
PHP 8.0.2 (cli) (built: Feb  5 2021 00:39:11) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.2, Copyright (c) Zend Technologies

It's possible to build PHP from source, as suggested, but it is a difficult and lengthy procedure, and you may end up with unreliable binaries if the build or environment are not properly configured. For reference, you could find instructions for building PHP 7.4 on Ubuntu here:
https://github.com/gitKearney/php7-from-scratch.

Upvotes: 3

Related Questions