Skip to content

Instantly share code, notes, and snippets.

@michaelbutler
Last active July 19, 2024 07:28
Show Gist options
  • Save michaelbutler/4a89bb23e2d30f1b0585b98d2b67cf55 to your computer and use it in GitHub Desktop.
Save michaelbutler/4a89bb23e2d30f1b0585b98d2b67cf55 to your computer and use it in GitHub Desktop.
Install multiple PHP versions on Arch / Manjaro Linux

Install Any PHP on Arch / Manjaro

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.

@NikunjGupta01
Copy link

Hello I know this not where to ask but when i am building and installing using make si .It completes successfully but it does not create an the php73 or php74 file in /bin

@timosz
Copy link

timosz commented Dec 27, 2023

Great work! Thanks for sharing!

@iv7dev
Copy link

iv7dev commented May 9, 2024

Thank you! Excellent work

@JakubTkac
Copy link

c-client is having problems to be compiled on gcc 13. Not sure if it is connected but now I am also getting this error while trying to makepkg -si php81 or php74

/home/jakub/src/php81/src/php-8.1.28/ext/libxml/libxml.c: In function ‘zif_libxml_use_internal_errors’:
/home/jakub/src/php81/src/php-8.1.28/ext/libxml/libxml.c:981:48: warning: comparison of distinct pointer types lacks a cast [-Wcompare-distinct-pointer-types]
  981 |         if (current_handler && current_handler == php_libxml_structured_error_handler) {
      |                                                ^~
/home/jakub/src/php81/src/php-8.1.28/ext/libxml/libxml.c:999:49: error: passing argument 2 of ‘xmlSetStructuredErrorFunc’ from incompatible pointer type [-Wincompatible-pointer-types]
  999 |                 xmlSetStructuredErrorFunc(NULL, php_libxml_structured_error_handler);
      |                                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                                 |
      |                                                 void (*)(void *, xmlError *) {aka void (*)(void *, struct _xmlError *)}
In file included from /usr/include/libxml2/libxml/valid.h:15,
                 from /usr/include/libxml2/libxml/parser.h:19,
                 from /home/jakub/src/php81/src/php-8.1.28/ext/libxml/libxml.c:32:
/usr/include/libxml2/libxml/xmlerror.h:898:57: note: expected ‘xmlStructuredErrorFunc’ {aka ‘void (*)(void *, const struct _xmlError *)’} but argument is of type ‘void (*)(void *, xmlError *)’ {aka ‘void (*)(void *, struct _xmlError *)’}
  898 |                                  xmlStructuredErrorFunc handler);
      |                                  ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
/home/jakub/src/php81/src/php-8.1.28/ext/libxml/libxml.c: In function ‘zif_libxml_get_last_error’:
/home/jakub/src/php81/src/php-8.1.28/ext/libxml/libxml.c:1016:15: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
 1016 |         error = xmlGetLastError();
      |               ^
make: *** [Makefile:968: ext/libxml/libxml.lo] Error 1
==> ERROR: A failure occurred in build().
    Aborting...

@ahmada3mar
Copy link

@Its-Satyajit
Copy link

Its-Satyajit commented Jul 14, 2024

I've made some enhancement on top this. take a look https://github.com/Its-Satyajit/phpv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment