1

I have PHP 8.1.2 with Apache 2.4.52 on Ubuntu 22.04 and I think I need the GD Graphics Extension for my PHP scripts to work but it appears not to be installed and / or enabled (there is no GD section in the output from running phpinfo().

Some advice I have seen is to run apt-get install php-gd and restart Apache without any modifications to php.ini.

At php.net it seems to suggest there is a

recommended bundled version of the GD library

already present in my PHP and that I need to edit php.ini in order to enable it. I understood (I now think wrongly) this meant I needed to remove the prefixed semi colons from these pre existing directives:

  • ;--with-gd[=DIR]
  • ;--enable-gd

The advice continues on to suggest for certain image formats (although possibly not for PNG) I need to uncomment:

  • ;--with-jpeg
  • ;--with-xpm
  • ;--with-webp

For enhancing GD's font capabilities uncomment:

  • --with-XXXX
  • --with-freetype

searching /etc/php/8.1/apache2/php.ini revealed that was no ;--enable-gd line or any of the other above directives in that file. There was a ;extension=gd line.

I would like to know how to proceed from here in order to install (if it is not already installed as I thought the documentation implied) and enable the GD Graphics extension.

Edit I also saw this geeksforgeeks article suggesting to uncomment ;extension=gd and restart the server.

user3425506
  • 1,285
  • 1
  • 16
  • 26
  • 2
    You're looking at instructions for _building_ GD [and also PHP] where you can _optionally_ build it into core. Most distros build GD separately as an extension, which is what the `php-gd` package is. Depending on which distro and who built the package it may or may not have added the relevant PHP config to enable it. Uncomment the extension line. – Sammitch Dec 06 '22 at 23:19
  • 1
    make sure after you changed the php.ini (e.g. uncomment the line) , you restart the httpd – Ken Lee Dec 06 '22 at 23:25
  • @Sammitch does that mean I should do `apt-get install php-gd`? Then uncomment `;extension=gd`? Then restart server? – user3425506 Dec 06 '22 at 23:25
  • 1
    What version of `PHP` do you have now? You can run `php -v`. Can you run `which php` and show the log? – DreamBold Dec 06 '22 at 23:28
  • @DreamBold `php -v` gives *PHP 8.1.2-1ubuntu2.9 (cli) etc etc* and `which php` gives */usr/bin/php* – user3425506 Dec 06 '22 at 23:35
  • 1
    @user3425506 You can try `sudo apt-get install php8.1-gd` then – DreamBold Dec 07 '22 at 00:17
  • @DreamBold I did that and did not edit php.ini and now GD section appears in phpinfo() output and also my image handling code is working. Thanks! – user3425506 Dec 07 '22 at 00:31
  • 1
    @user3425506 Perfect, let me add it as an answer to this question – DreamBold Dec 07 '22 at 05:40

1 Answers1

3

You can run php -v to see what version of PHP you have on the machine. After that, you can run sudo apt-get install php<your-php-version>-gd, e.g. sudo apt-get install php8.1-gd. Hope it helps!

DreamBold
  • 2,727
  • 1
  • 9
  • 24
  • 1
    `php -v` will give you the PHP version for the CLI, which isn't always the same as the one used for rendering the web. It's best to use `phpinfo()` to be sure (especially when using FPM). – Aldo Jul 10 '23 at 08:28