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:
Update: Better version of this script is in a comment below.
#!/usr/bin/env bash
[[ -n $DEBUG ]] && set -x
red='\033[0;31m'
green='\033[0;32m'
reset='\033[0m'
# $1 is version: 7 for latest 7, 8 for latest 8
if [ "$1" == "7" ]; then
echo -e "${green}Activating php 7 at location /usr/bin/php7 ...${reset}"
rm -f $HOME/bin/php $HOME/bin/phpize
ln -s /usr/bin/php7 $HOME/bin/php
ln -s /usr/bin/phpize7 $HOME/bin/phpize
sleep 0.5
php -v
fi
if [ "$1" == "8" ]; then
echo -e "${green}Activating php 8.1 at location /usr/bin/php81 ...${reset}"
rm -f $HOME/bin/php $HOME/bin/phpize
ln -s /usr/bin/php81 $HOME/bin/php
ln -s /usr/bin/phpize81 $HOME/bin/phpize
sleep 0.5
php -v
fi
Then I can run it any time with phpenv 7
to activate 7.4, and phpenv 8
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.
Thank you! Excellent work