User Agents

Web browsers and everyday web tech, explained

Install PHP Extensions for Laravel: A Complete Guide

· 6 min read · Extensions

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Installing the required PHP extensions for Laravel ensures that your server environment can handle the framework's core functionalities, such as database connectivity, encryption, string manipulation, and session management. Without these extensions, Laravel will fail to boot, typically throwing a "missing extension" error during the installation of dependencies via Composer or when attempting to run the development server. This process involves identifying the specific extensions required by your version of Laravel and using your operating system's package manager to install and enable them.

What You'll Need

  • A server or local machine with a supported version of PHP installed (e.g., PHP 8.2 or 8.3).
  • Administrative or root access (sudo privileges) to install system-level packages.
  • A terminal or command-line interface (CLI).
  • Composer installed on your system to verify the extensions are working.

Step-by-Step

  1. Identify Your PHP Version: Before installing extensions, you must know exactly which version of PHP you are running, as extension packages are version-specific. Open your terminal and run php -v. If you are using PHP 8.2, your extensions must be prefixed with php8.2-. If you are using 8.3, use php8.3-. Installing an extension for the wrong version will result in it not being loaded by the active PHP binary.
  2. Update Your Package Repository: To ensure you are downloading the latest stable versions of the extensions, update your system's package list. On Ubuntu or Debian-based systems, run sudo apt update. On CentOS or RHEL, use sudo yum update or sudo dnf update. This prevents "package not found" errors during the installation phase.
  3. Install Core Laravel Extensions: Laravel requires a specific set of extensions to function. Run the following command (adjusting the version number to match your PHP version):

    For Ubuntu/Debian:
    sudo apt install php8.x-bcmath php8.x-curl php8.x-mbstring php8.x-xml php8.x-zip php8.x-tokenizer php8.x-gd php8.x-intl

    Breakdown of these extensions:
    • BCMath: Used for high-precision mathematics, essential for financial calculations.
    • Curl: Allows Laravel to make HTTP requests to external APIs.
    • MBString: Handles multi-byte strings, necessary for non-ASCII character support and string manipulation.
    • XML/DOM: Required for parsing XML files and interacting with certain API responses.
    • Zip: Needed by Composer to unpack downloaded packages efficiently.
    • Tokenizer: Used by the Blade templating engine to parse PHP code.
    • GD: Essential for image processing, resizing, and manipulation.
    • Intl: Provides internationalization support for formatting dates and currencies.
  4. Install Database-Specific Extensions: Depending on which database you intend to use, you must install the corresponding driver. Laravel does not install these by default because it supports multiple backends.
    • MySQL/MariaDB: sudo apt install php8.x-mysql
    • PostgreSQL: sudo apt install php8.x-pgsql
    • SQLite: sudo apt install php8.x-sqlite3
  5. Enable the Extensions in php.ini: While package managers usually enable extensions automatically, some environments require manual activation. Locate your php.ini file (use php --ini to find the path). Open the file in a text editor and ensure the lines corresponding to the extensions are not commented out. For example, ensure extension=mbstring does not have a semicolon (;) at the start of the line.
  6. Restart Your Web Server: Changes to PHP extensions do not take effect until the PHP process is restarted. If you are using Apache, run sudo systemctl restart apache2. If you are using Nginx with PHP-FPM, run sudo systemctl restart php8.x-fpm (replacing 8.x with your version). If you are using the built-in PHP development server (php artisan serve), simply stop the process with Ctrl+C and start it again.
  7. Verify Installation: To confirm that all required extensions are active, run the command php -m. This lists all compiled-in and loaded modules. Scan the list for the extensions you installed (e.g., bcmath, curl, mbstring). Alternatively, try running composer install in your Laravel project directory; if extensions are missing, Composer will explicitly list them in the error output.

Common Mistakes

One of the most frequent errors is installing extensions for the wrong PHP version. For instance, a user might have PHP 8.2 installed but run sudo apt install php-mbstring, which might install the extension for the default system version (perhaps 8.1), leaving the active PHP 8.2 environment without the module. Always specify the version number in the package name.

Another common oversight is forgetting to restart the PHP-FPM service when using Nginx. Because Nginx acts as a proxy and PHP-FPM handles the actual execution, restarting Nginx alone does nothing to reload the PHP configuration. The FPM service must be bounced to recognize the new .so files loaded into memory.

Users often confuse the CLI (Command Line Interface) and the Web (Apache/FPM) php.ini files. PHP often maintains two separate configuration files. You might enable an extension for the CLI, allowing php artisan commands to work, but the web browser still shows a "500 Internal Server Error" because the extension is not enabled in the web-server's php.ini.

Tips

To streamline future installations, you can create a small shell script containing all the necessary apt install commands. This is particularly useful when setting up multiple development environments or deploying to a new VPS, as it ensures consistency across all servers.

If you are working in a local development environment, consider using tools like Laravel Herd or Laravel Valet (for macOS) or Docker (via Laravel Sail). These tools come pre-configured with all the necessary PHP extensions, removing the need for manual installation and reducing the risk of version mismatch errors.

When debugging missing extensions, use the command php -i | grep "extension". This provides a more detailed view of the loaded modules and their configuration settings than php -m, which is helpful if an extension is loaded but configured incorrectly.

Troubleshooting

If you receive a "Package not found" error during installation, it is likely that your system's default repositories do not contain the specific PHP version you need. On Ubuntu, the solution is to add the Ondřej Surý PPA, which is the industry standard for up-to-date PHP packages. Run sudo add-apt-repository ppa:ondrej/php, followed by sudo apt update, and then attempt the installation again.

If an extension is installed but not appearing in php -m, check for conflicting php.ini files. Run php --ini to see which configuration file is being loaded. If you see multiple files, ensure you are editing the "Loaded Configuration File" and not a secondary "Additional .ini files" path. If the extension is listed in the conf.d directory but still not loading, check the system logs (/var/log/syslog or /var/log/apache2/error.log) for "Unable to load dynamic library" errors, which usually indicate a missing system dependency.

In cases where Composer reports a missing extension despite it being installed, ensure that the version of PHP used by the terminal matches the version Composer is checking. You can force Composer to ignore platform requirements for a quick test using composer install --ignore-platform-reqs, but this is a temporary workaround and not a fix for the underlying environment issue.

Frequently Asked Questions

Do I need to install these extensions on my local machine and the production server?

Yes. Both environments must have the same extensions to ensure that code which works locally does not crash upon deployment. It is highly recommended to use a tool like Docker to keep these environments identical.

What happens if I miss one of these extensions?

Laravel will either fail to install via Composer or throw a fatal error when you try to access the application. For example, missing bcmath will cause errors during payment processing or high-precision math operations, while missing mbstring will break string helper functions.

Can I install these extensions using a GUI?

While some hosting panels like cPanel or Plesk provide a "PHP Selector" where you can check boxes to enable extensions, the command line is the most reliable method for VPS or dedicated servers to ensure the correct versions are mapped.

Is it safe to install all available PHP extensions?

No. Installing unnecessary extensions can increase the memory footprint of each PHP process and potentially introduce security vulnerabilities. Only install the extensions required by Laravel and any third-party packages your project uses.

Why does my server say PHP is installed but Laravel says extensions are missing?

PHP is the core engine, but extensions are modular add-ons. Installing the base php package does not include specialized modules like gd or xml. You must install these as separate packages to give PHP the ability to handle those specific tasks.