Keywords: Laravel | PHP Extensions | Ubuntu Installation | Server Configuration | Development Environment
Abstract: This article provides a detailed guide on installing PHP extensions required by the Laravel framework on Ubuntu 16.04 and later versions. It analyzes Laravel's server requirements, including core extensions like OpenSSL, PDO, Mbstring, Tokenizer, and XML, and offers installation commands for different PHP versions. Through specific code examples and system command demonstrations, developers can quickly configure a PHP environment that meets Laravel's specifications.
Analysis of PHP Extension Requirements for Laravel Framework
Laravel, as a representative modern PHP framework, has specific server environment requirements. According to official documentation, Laravel 10.x requires support for the following PHP extensions: BCMath, Ctype, cURL, DOM, Fileinfo, JSON, Mbstring, OpenSSL, PCRE, PDO, Tokenizer, and XML. These extensions provide core functionalities such as encryption, database operations, string processing, and XML parsing for Laravel.
Extension Installation Methods on Ubuntu Systems
In Ubuntu systems, installing PHP extensions via terminal commands is the most direct and effective approach. For environments with PHP 7.1 already installed, use the following command to install the required extensions in bulk:
sudo apt install openssl php-bcmath php-curl php-json php-mbstring php-mysql php-tokenizer php-xml php-zip
This command installs the OpenSSL library and corresponding PHP extension packages. The php-mysql extension provides MySQL database driver support for PDO, while the php-zip extension is useful for handling file compression.
Extension Installation for Specific PHP Versions
For environments using specific PHP versions, such as PHP 8.3, install the corresponding versioned extension packages:
sudo apt install openssl php8.3-bcmath php8.3-curl php8.3-json php8.3-mbstring php8.3-mysql php8.3-tokenizer php8.3-xml php8.3-zip
This naming convention ensures compatibility between extensions and specific PHP versions. During installation, the system automatically handles dependencies to ensure all necessary library files are correctly installed.
Extension Installation Verification and Troubleshooting
After installation, verify that extensions are successfully loaded using the PHP command line:
php -m | grep -E "(openssl|pdo|mbstring|tokenizer|xml)"
This command lists all installed extensions and filters key ones using grep. If an extension does not appear, restarting the web server (e.g., Apache or Nginx) or PHP-FPM service may be necessary.
Extension Package References for Different Ubuntu Versions
Extension package availability may vary across different Ubuntu versions:
- Ubuntu 22.04 LTS (Jammy Jellyfish) offers the latest PHP extension packages
- Ubuntu 20.04 LTS (Focal Fossa) supports PHP 7.4 and 8.0
- Ubuntu 18.04 LTS (Bionic) primarily supports PHP 7.2
- Ubuntu 16.04 LTS (Xenial) supports PHP 7.0 and 7.1
Developers are advised to select appropriate extension package sources based on their actual Ubuntu version.
Composer Dependencies and Additional Extension Needs
Beyond the core extensions required by Laravel, specific Composer packages may need additional PHP extensions. When deploying projects, carefully check the require and require-dev sections in the composer.json file to ensure all dependent extensions are installed. For example, image processing packages might require gd or imagick extensions, while Redis caching may need the redis extension.