Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP says curl_init() does not exist because an extension is missing

ErrorPHP Fatal error: Uncaught Error: Call to undefined function curl_init()

Short answer

The function belongs to an extension (curl, mbstring, xml and others) that the PHP process running your code has not loaded. Check the loaded extensions of that exact process, not of the php command in your terminal, then install or enable the extension for that PHP version and restart PHP-FPM or Apache.

  • Applies to PHP 8.3, PHP-FPM 8.3, Ubuntu 24.04, Windows 11, Docker official php image
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the extensions of command line PHP, then ask the web side the same question through a temporary page, because CLI and web PHP are separate programs with separate settings. The temporary page prints the PHP version, the interface and whether curl is loaded.

    php -m | grep -i curl
    echo '<?php echo PHP_VERSION, " ", php_sapi_name(), " curl=", var_export(extension_loaded("curl"), true);' | sudo tee /var/www/html/ext-probe.php
    curl -s http://127.0.0.1/ext-probe.php; echo; sudo rm /var/www/html/ext-probe.php
  2. 2Match what you see, then jump to its fix

    1. 1php -m does not list curl, and the web probe prints curl=falseThe extension package is not installedFix
    2. 2The file exists (the DLL on Windows, or the package on Linux), but php -m still lacks itThe extension is installed but switched off in php.iniFix
    3. 3php -m shows curl, but the web probe prints curl=false, or prints another version numberThe web server runs a different PHP than the php commandFix
    4. 4The error appears only inside the container, and php -m in the container does not list the extensionA Docker php image does not include the extensionFix

What does the error look like?

  • PHP Fatal error: Uncaught Error: Call to undefined function curl_init()
  • Fatal error: Call to undefined function mb_strlen()
  • Class "DOMDocument" not found

Cause 1 of 4

The extension package is not installed

1Check

On Debian and Ubuntu each extension is its own package, named after the PHP version. See which PHP packages are installed and whether one for curl exists.

dpkg -l 'php*' | grep -E 'curl|fpm|cli'

2Fix

Install the package for your PHP version, then restart the service that runs PHP so it loads the new extension. Use apache2 instead of php8.3-fpm if Apache runs PHP as a module.

sudo apt install php8.3-curl
sudo systemctl restart php8.3-fpm

3Verify

The extension is listed and the web probe prints curl=true.

php -m | grep -i curl

Confirmed by packages.ubuntu.com

Did this fix it?

Cause 2 of 4

The extension is installed but switched off in php.ini

1Check

PHP reads the main php.ini plus the extra .ini files from a scan folder. On Windows the extension line in the main file is commented out with a semicolon by default. Find the files PHP loaded.

php --ini
php -i | grep -i 'curl'

2Fix

Remove the semicolon in front of the extension line in the php.ini that php --ini reports. On Windows the PHP manual also asks for the OpenSSL and libssh2 DLLs to be on the PATH. Restart the web server afterwards.

; php.ini
extension=curl

3Verify

A new php process lists curl.

php -m | grep -i curl

Confirmed by php.net

Did this fix it?

Cause 3 of 4

The web server runs a different PHP than the php command

1Check

Several PHP versions can live on one machine. The one in your terminal (php -v) is not necessarily the one behind PHP-FPM or Apache, and each has its own php.ini. Compare the version from the probe in the first step with the terminal's version.

php -v | head -n 1
systemctl list-units 'php*-fpm*' --no-legend

2Fix

Install or enable the extension for the version that the probe printed, and restart that service. If you want both to use the same version, switch the command line default instead.

sudo apt install php8.2-curl     # example: the web side runs 8.2
sudo systemctl restart php8.2-fpm

3Verify

The web probe prints curl=true.

curl -s http://127.0.0.1/ext-probe.php

Confirmed by php.net

Did this fix it?

Cause 4 of 4

A Docker php image does not include the extension

1Check

The official image compiles in many extensions, but not every one, and the list depends on the PHP version. Ask the container itself.

docker run --rm php:8.3-fpm php -m

2Fix

Add the extension in the Dockerfile with the image's helper script. Extensions that need system libraries (gd, for example) need their development packages installed first.

FROM php:8.3-fpm
RUN docker-php-ext-install -j"$(nproc)" mysqli

3Verify

Rebuild the image and check the list.

docker build -t my-php .
docker run --rm my-php php -m | grep -i mysqli

Confirmed by hub.docker.com

Did this fix it?

How do I stop it coming back?

  • Write the extensions your app needs into composer.json (ext-curl, ext-mbstring) so composer install fails with a clear message on a server that lacks them.
  • Check Laravel's list of required extensions when you provision a server, instead of adding them one error at a time.
  • Use one PHP version per server, or name the version in every command and service.
  • After a PHP upgrade, repeat the web probe; extension packages are per version and do not move over.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

PHP cannot turn a hostname into an IP address

Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed

This warning means the operating system could not resolve the hostname PHP was asked to connect to.

3 causes, each with a fix

Back end

CodeIgniter 4 cannot write its cache files

CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/

CodeIgniter 4's file cache needs a cache directory that the web server can really write to.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.