Install Any PHP version on Arch / Manjaro

Posted on 2023. Oct 17

Through the AUR it is possible to install older and newer PHP versions, simultaneously on the same system. I often had trouble installing using pacman and pamac so here's what I did:

mkdir -p $HOME/bin
mkdir ~/src
cd ~/src
git clone https://aur.archlinux.org/php81.git
cd php81
makepkg -si
# Wait a very long time (it literally compiles and installs php 
# AND ALL MODULES.
# Enter sudo password after the compile step is done

In that example, php 8.1 is now available at /usr/bin/php81 along with /usr/bin/phpize81 . These steps can be repeated by just changing php81 to another version, such as php74 or php80 to get more versions installed.

Then, to help with activating a specific PHP version at any given time (mainly for CLI commands) I use this simple script, placed in my $PATH:

#!/usr/bin/env bash

[[ -n $DEBUG ]] && set -x

red='\033[0;31m'
green='\033[0;32m'
reset='\033[0m'

VERSION=$1

if [[ -z $1 ]]; then
  echo -e "${red}Error: no version specified!${reset}"
  echo -e "Usage: \`phpenv [VERSION]\`"
  echo -e "Example: \`phpenv 81\`"
fi

PHP_BIN="/usr/bin/php$VERSION"
PHPIZE_BIN="/usr/bin/phpize$VERSION"
PHP_FPM_BIN="/usr/bin/php-fpm$VERSION"
PHP_DBG_BIN="/usr/bin/phpdbg$VERSION"
PHP_CGI_BIN="/usr/bin/php-cgi$VERSION"
PHP_PEAR_BIN="/usr/bin/pear$VERSION"
PHP_PECL_BIN="/usr/bin/pecl$VERSION"

if [[ -f "$PHP_BIN" ]]; then
  echo -e "${green}Activating PHP $VERSION..."
  rm -f $HOME/bin/php $HOME/bin/phpize $HOME/bin/php-fpm $HOME/bin/phpdbg $HOME/bin/php-cgi $HOME/bin/pear $HOME/bin/pecl
  ln -s $PHP_BIN $HOME/bin/php
  ln -s $PHPIZE_BIN $HOME/bin/phpize
  ln -s $PHP_FPM_BIN $HOME/bin/php-fpm
  ln -s $PHP_DBG_BIN $HOME/bin/phpdbg
  ln -s $PHP_CGI_BIN $HOME/bin/php-cgi
  ln -s $PHP_PEAR_BIN $HOME/bin/pear
  ln -s $PHP_PECL_BIN $HOME/bin/pecl
  php -v
else
  echo -e "${red}Error: $PHP_BIN could not be found!${reset}"
fi

Then I can run it any time with phpenv 74 to activate 7.4, and phpenv 81 to activate 8.1. You can customize and add more versions as needed, just update the paths.

As a reminder, php --ini will print out the location of the .ini files that are loaded.

How to Update PHP when a patch is released

  • cd to the directory originally used in the above instructions (e.g. cd ~/src/php81)
  • git pull to pull in the latest updates
  • makepkg -si and wait a while

Note 1: If you deleted the directory before, you can just repeat the original steps (clone the repository and run the makepkg -si command to install the latest).

Note 2: The system package manager (e.g. Discover on KDE) might try to update PHP, but that will fail. You have to do it via these steps instead.

Handy Links:

This article, and all articles on this blog, were written without the use of any AI, GPT, or Language Learning Models. It's old fashioned I guess.