Search code examples
phplinuxdebianimagick

Cannot load/run Imagick on Raspberry OS (Debian 10) and PHP 8.2


Background

Dear all,

I've installed PHP 8.2 on me Raspbian OS (PHP 8.x required by some libraries). I need to install the PHP imagemagick extension to convert images (SVG to PNG). However, I can't get it running.

Here is what i tried

Firstly, I installed imagemagick (don't know wheather that's necessary, but just to be on the safe side):

$ sudo apt install imagemagick

That was successful. There are no other packages avaiable:

$ sudo apt install imagemagick -a
imagemagick/oldstable,now 8:6.9.10.23+dfsg-2.1+deb10u1 armhf  [installiert]

Secondly, I installed Imagick:

$ sudo apt install php-imagick

That was successful as well. There are no other packages avaiable:

$ sudo apt list php-imagick -a
php-imagick/oldstable,now 3.4.3-4.1 armhf [installiert]

I've added the following entry in my php.ini:

[Imagick]
extension=imagick.so

Finally, I restarted apache: $ sudo systemctl restart apache2

During troubleshooting, I even restarted php: $ sudo service php8.2-fpm reload

Problem and troubleshooting so far

Imagick doesn't show up, neither when I try php -r 'phpinfo();' | grep imagick nor php -m | grep imagick.

Saying that, of course $im = new \Imagick(); results in a

Fatal error: Uncaught Error: Class "Imagick" not found.

I've studied web pages (including php error: Class 'Imagick' not found) and manuals for more than two hours now without any success. Any help/hint is highly appreciated!

Edit: Hints found so far

Maybe one hint is that imagick.so was installed in the directory /usr/lib/php/20180731, while the current php extensions directory (according to php.ini) is /usr/lib/php/20220829. Copying the file to 20220829 (and restarting the services) didn't help.

Another hint might be that /etc/php/7.3/cli/conf.d contains a (link file) 20-imagick.ini (pointing to /etc/php/7.3/mods-available/imagick.ini), while /etc/php/8.2/cli/conf.d doesn't contain such a link, and /etc/php/8.2/mods-available doesn't contain an imagick.ini file. Same is applicable for /etc/php/x.y/apache2/conf.d. That seems suspicious, so I copied the following files

/etc/php/7.3/mods-available/imagick.ini -> /etc/php/8.2/mods-available/imagick.ini [content: extension=imagick.so]
/etc/php/7.3/cli/conf.d/20-imagick.ini -> /etc/php/8.2/cli/conf.d/20-imagick.ini
/etc/php/7.3/apache2/conf.d/20-imagick.ini -> /etc/php/8.2/apache2/conf.d/20-imagick.ini
/etc/php/7.3/fpm/conf.d/20-imagick.ini -> /etc/php/8.2/fpm/conf.d/20-imagick.ini
/usr/lib/php/20180731/imagick.so -> /usr/lib/php/20220829/imagick.so

After $ sudo systemctl restart apache2 when I run $ php -m | grep imagick I now get

PHP Warning:  PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/lib/php/20220829/imagick.so (/usr/lib/php/20220829/imagick.so: undefined symbol: add_next_index_zval), /usr/lib/php/20220829/imagick.so.so (/usr/lib/php/20220829/imagick.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0

Maybe for some reason apt install and/or php-imagick think that my current php version is 7.3, so it updated the wrong files and took the wrong shared library (imagick.so), while it is 8.2 (7.3 is still installed, but inactive and 8.2 is active)?

Found this link here, that at least matches the error message: The propcedure entry point add_index_zval could not be located in the dynamic link library xampp/php/ext/php_imagic.dll

Possible solution: I have to get Imagick version>=3.5.0, according to the change log https://pecl.php.net/package-changelog.php?package=imagick, in order to have PHP 8.x supported (hopefully 8.2).


Solution

  • Main problem: Certificates of repository not installed

    The main problem was that I had installed PHP 8.2 from an additional repository, as PHP 8.x is not yet supported by the Raspberr Pi Foundation, but I needed PHP 8.x for some extensions. The repository is fine, but I hadn't added the some certificates, so apt couldn't load all required files from this repository.

    And you get lots of headache, when you can't load required files from a repository.

    Step by step solution (for Debian 10 Buster)

    Here is what I did in order to solve (or, to be more specific, to avoid) the problem.

    Clean connection to repository

    1. Add repository gpg to your list if trusted repositories.
    $ sudo wget /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
    $ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
    
    1. Update apt package information This step is imporant, don't omit it.
    $ sudo apt update
    
    1. Follow instructions of the repository provider Unfortunately, I wasn't aware of the following steps, and this caused my problems. According to https://packages.sury.org/php/README.txt
    $ sudo apt-get -y install lsb-release ca-certificates curl
    $ sudo curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg
    $ sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
    $ sudo apt-get update
    

    Otherwise, the required cerficicates are not installed, and apt doesn't download all files from the repository (although it downloaded some - I have no idea why this is the case).

    Get phpize

    1. Get phpize You need phpize to install Imagick later. phpize is included in the package pho-dev. When I tried this before solving the certificate issue (see above), I failed to do so due to package requiremens restrictions. Make sure to have the repostories properly connected (see above).

    The error message was similar to this one (I had to translate it and hope I got it right):

    The following packages have unmet dependencies:
     libpcre2-dev : Depends: libpcre2-8-0 (= 10.32-5) but 10.34-7+0~20191219.5+debian9 is to be installed
    

    As soon the certificate issue was solved, the following command installed phpsize für PHP 8.2:

    $ sudo apt-get install php-dev 
    

    Install PECL

    1. Install PECL Rather then installing installing imagick with apt, I installed it by using PECL, as recommended in the PHP documentation. First, I had to download go-pear.phar. Second, I installed it
    $ wget http://pear.php.net/go-pear.phar
    

    I moved go-pear.phar to a directory ~/Downloads/pear/.

    $ sudo php ~/Downloads/pear/go-pear.phar
    Below is a suggested file layout for your new PEAR installation.  To
    change individual locations, type the number in front of the
    directory.  Type 'all' to change all of them or simply press Enter to
    accept these locations.
    
     1. Installation base ($prefix)                   : /usr
     2. Temporary directory for processing            : /tmp/pear/install
     3. Temporary directory for downloads             : /tmp/pear/install
     4. Binaries directory                            : /usr/bin
     5. PHP code directory ($php_dir)                 : /usr/share/php
     6. Documentation directory                       : /usr/docs
     7. Data directory                                : /usr/data
     8. User-modifiable configuration files directory : /usr/cfg
     9. Public Web Files directory                    : /usr/www
    10. System manual pages directory                 : /usr/man
    11. Tests directory                               : /usr/tests
    12. Name of configuration file                    : /etc/pear.conf
    
    1-12, 'all' or Enter to continue:
    

    It is important to run this with sudo, otherwise user-specific directories are suggested. I pressed Enter to continue.

    Install imagemagick and Imagick

    1. Install imagemagick
    $ sudo apt-get install imagemagick
    $ sudo apt-get install libmagickwand-dev libmagickcore-dev
    
    1. Install imagick (via PECL)
    $ sudo pecl install imagick
    

    During this installation, phpize is required to compile imagick. phpize had the wrong path for sed configured (see the first lines in the file phpize).

    To be on the save side, I copied sed as (hard) link

    $ sudo ln /bin/sed /usr/bin/sed
    

    Disconnect repository added as first step

    If you wish to disconnect the repository sury.org/php, you have to execute this commands. I'm not recommending this, as the possible updates or changes of the software installed depends on this repository.

    sudo rm -rf /etc/apt/trusted.gpg.d/php.gpg
    sudo rm -rf /etc/apt/sources.list.d/php.list