How to Install PHP on Ubuntu

Install PHP on Ubuntu

PHP continues to evolve, and with the release of PHP 8.4, developers are eager to harness its new features and performance improvements. If you’re running Ubuntu and want to upgrade to PHP 8.4, you’ve come to the right place. This comprehensive guide will walk you through multiple installation methods, ensuring you can get PHP 8.4 up and running smoothly on your Ubuntu system.

Table of Contents

Introduction to PHP 8.4

PHP 8.4 represents another significant milestone in the language’s evolution, building upon the solid foundation established by its predecessors. As a developer, staying current with PHP versions isn’t just about having the latest features—it’s about security, performance, and maintaining compatibility with modern frameworks and libraries.

What’s New in PHP 8.4

PHP 8.4 introduces several exciting features that make development more efficient and secure. The latest version includes improved JIT compilation, enhanced type system improvements, and new built-in functions that streamline common development tasks. Performance benchmarks show that PHP 8.4 delivers approximately 15-20% better performance compared to PHP 8.1, making it an attractive upgrade for production environments.

One of the standout features is the improved array functionality and new string manipulation methods. These enhancements reduce the need for external libraries in many common scenarios, leading to cleaner, more maintainable code. Additionally, PHP 8.4 includes better error handling mechanisms that help developers identify and resolve issues more quickly.

Why Upgrade to PHP 8.4?

The benefits of upgrading to PHP 8.4 extend far beyond new features. Security is a primary concern, as older PHP versions eventually lose official support and security updates. PHP 8.4 includes numerous security enhancements that protect your applications from emerging threats.

Performance improvements alone justify the upgrade for most applications. Real-world testing shows that typical web applications see 10-25% performance gains when migrating from older PHP versions. This translates to faster page load times, reduced server resource consumption, and improved user experience.

Furthermore, major frameworks like Laravel, Symfony, and WordPress are increasingly optimized for newer PHP versions. By upgrading to PHP 8.4, you ensure compatibility with the latest versions of these popular platforms and their extensive ecosystem of packages.

Prerequisites and System Requirements

Before diving into the installation process, it’s crucial to ensure your system meets the necessary requirements and you have the proper access levels.

Ubuntu Version Compatibility

PHP 8.4 is compatible with Ubuntu 20.04 LTS (Focal Fossa), Ubuntu 22.04 LTS (Jammy Jellyfish), and Ubuntu 24.04 LTS (Noble Numbat). While it’s possible to install PHP 8.4 on older Ubuntu versions, we strongly recommend using a supported LTS version for stability and security.

You can check your Ubuntu version by running:

lsb_release -a

This command will display your current Ubuntu version and help you determine compatibility. If you’re running an older version, consider upgrading your system before proceeding with the PHP installation.

Required Permissions and Access

You’ll need sudo access or root privileges to install PHP 8.4 and its dependencies. Most installation commands require administrative rights to modify system files and install packages. Ensure you have:

  • Sudo access on your Ubuntu system
  • Active internet connection for downloading packages
  • At least 1GB of free disk space for PHP and its extensions
  • Basic familiarity with command-line operations

If you’re working on a shared hosting environment or managed server, check with your hosting provider about PHP version upgrades, as the process might differ from a standard Ubuntu installation.

Method 1: Installing PHP 8.4 Using apt Package Manager

The most straightforward way to install PHP 8.4 on Ubuntu is using the apt package manager with the popular Ondřej Surý PPA repository. This method is beginner-friendly and handles dependencies automatically.

Adding the Ondřej Surý PPA Repository

Ondřej Surý maintains the most comprehensive and up-to-date PHP packages for Ubuntu. His PPA repository includes PHP 8.4 and all major extensions, making it the go-to choice for PHP installations.

First, update your system packages:

sudo apt update && sudo apt upgrade -y

Next, install the required dependencies for adding PPAs:

sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https

Add the Ondřej Surý PPA repository:

sudo add-apt-repository ppa:ondrej/php

When prompted, press Enter to confirm the addition. This command adds the repository to your system’s package sources, allowing you to install PHP 8.4 and its extensions.

Updating Package Lists

After adding the PPA, update your package lists to include the newly available PHP packages:

sudo apt update

You can verify that PHP 8.4 packages are now available by searching:

apt search php8.4

This command should display a comprehensive list of PHP 8.4 packages available for installation.

Step-by-Step Installation Commands

Now you’re ready to install PHP 8.4. The basic installation command is:

sudo apt install php8.4

This installs the core PHP 8.4 package. However, for most web development scenarios, you’ll want additional extensions. Install a comprehensive set with:

sudo apt install php8.4 php8.4-cli php8.4-fpm php8.4-mysql php8.4-zip php8.4-gd php8.4-mbstring php8.4-curl php8.4-xml php8.4-bcmath php8.4-json

Each extension serves specific purposes:

  • php8.4-cli: Command-line interface
  • php8.4-fpm: FastCGI Process Manager for Nginx
  • php8.4-mysql: MySQL database connectivity
  • php8.4-zip: ZIP file handling
  • php8.4-gd: Image processing
  • php8.4-mbstring: Multibyte string handling
  • php8.4-curl: HTTP client functionality
  • php8.4-xml: XML processing
  • php8.4-bcmath: Arbitrary precision mathematics

Verifying the Installation

After installation completes, verify that PHP 8.4 is properly installed:

php --version

You should see output similar to:

PHP 8.4.x (cli) (built: Dec 2024)
Copyright (c) The PHP Group
Zend Engine v4.4.x, Copyright (c) Zend Technologies

If you have multiple PHP versions installed, you might need to update your alternatives:

sudo update-alternatives --set php /usr/bin/php8.4

Method 2: Installing PHP 8.4 from Source Code

While using the package manager is recommended for most users, installing from source provides maximum control over compilation options and features. This method is more complex but offers benefits for specialized environments.

Downloading PHP 8.4 Source

Before compiling from source, install the necessary build tools and dependencies:

sudo apt install build-essential autoconf libtool bison re2c pkg-config libxml2-dev libsqlite3-dev libssl-dev libcurl4-openssl-dev libpng-dev libjpeg-dev libonig-dev libzip-dev

Download the PHP 8.4 source code:

cd /tmp
wget https://www.php.net/distributions/php-8.4.x.tar.gz
tar -xzf php-8.4.x.tar.gz
cd php-8.4.x

Compiling and Installing from Source

Configure the build with essential options:

./configure --prefix=/usr/local/php8.4 --with-config-file-path=/usr/local/php8.4/etc --enable-fpm --with-mysql --with-pdo-mysql --with-curl --with-gd --with-zip --enable-mbstring --with-openssl --enable-bcmath

Compile PHP (this may take 15-30 minutes depending on your system):

make -j$(nproc)

Install the compiled binaries:

sudo make install

Configuration Options

The configure script accepts numerous options for customizing your PHP installation. Common additional options include:

  • --enable-opcache: Enable the built-in opcode cache
  • --with-readline: Add readline support for interactive CLI
  • --enable-sockets: Enable socket support
  • --with-gettext: Add internationalization support

You can view all available options with:

./configure --help

Installing Essential PHP 8.4 Extensions

Extensions are crucial for PHP functionality, providing interfaces to databases, image processing libraries, and other system services.

Most Common PHP Extensions

The most frequently used extensions for web development include:

Database Extensions:

  • php8.4-mysql: MySQL/MariaDB support
  • php8.4-pgsql: PostgreSQL support
  • php8.4-sqlite3: SQLite support

Web Development Extensions:

  • php8.4-curl: HTTP client functionality
  • php8.4-json: JSON handling (usually included by default)
  • php8.4-xml: XML processing
  • php8.4-mbstring: Multibyte string support

Utility Extensions:

  • php8.4-zip: Archive handling
  • php8.4-gd: Image processing
  • php8.4-bcmath: High precision mathematics

Database-Specific Extensions

Different applications require specific database extensions. For WordPress or most CMS platforms, install MySQL support:

sudo apt install php8.4-mysql

For PostgreSQL-based applications:

sudo apt install php8.4-pgsql

Many modern applications use multiple databases, so installing support for several database systems is common:

sudo apt install php8.4-mysql php8.4-pgsql php8.4-sqlite3

Installing Multiple Extensions Simultaneously

You can install multiple extensions in a single command:

sudo apt install php8.4-{mysql,curl,gd,zip,mbstring,xml,bcmath,soap,intl,opcache}

This command uses bash brace expansion to install multiple extensions efficiently. It’s particularly useful when setting up new development environments.

Configuring PHP 8.4 Settings

Proper configuration ensures optimal performance and security for your PHP installation.

Locating php.ini File

PHP 8.4 uses different configuration files for different SAPI (Server Application Programming Interface) modules:

  • CLI: /etc/php/8.4/cli/php.ini
  • Apache: /etc/php/8.4/apache2/php.ini
  • FPM: /etc/php/8.4/fpm/php.ini

You can locate your active configuration file with:

php --ini

Essential Configuration Tweaks

Edit the appropriate php.ini file for your environment:

sudo nano /etc/php/8.4/cli/php.ini

Memory and Execution Time Settings

Adjust these settings based on your application requirements:

memory_limit = 256M
max_execution_time = 300
max_input_time = 300
post_max_size = 64M
upload_max_filesize = 64M

For development environments, you might want more generous limits:

memory_limit = 512M
max_execution_time = 0

Security Configuration Options

Enhance security with these recommended settings:

expose_php = Off
display_errors = Off
log_errors = On
error_log = /var/log/php/error.log
allow_url_fopen = Off
allow_url_include = Off

Enable opcache for better performance:

opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=2

Setting Up PHP 8.4 with Web Servers

PHP 8.4 works with various web servers, with Apache and Nginx being the most popular choices.

Configuring Apache with PHP 8.4

If you’re using Apache, install the Apache module:

sudo apt install libapache2-mod-php8.4

Enable the PHP module:

sudo a2enmod php8.4

If you have multiple PHP versions, disable older ones:

sudo a2dismod php8.1 php8.2 php8.3

Restart Apache to apply changes:

sudo systemctl restart apache2

Setting Up Nginx with PHP-FPM 8.4

For Nginx, you’ll use PHP-FPM (FastCGI Process Manager):

sudo apt install php8.4-fpm

Start and enable PHP-FPM:

sudo systemctl start php8.4-fpm
sudo systemctl enable php8.4-fpm

Configure Nginx to use PHP-FPM by editing your site configuration:

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Restart Nginx:

sudo systemctl restart nginx

Testing Your PHP 8.4 Installation

Thorough testing ensures your PHP installation works correctly and all required extensions are available.

Creating a Test PHP File

Create a simple test file to verify web server integration:

sudo nano /var/www/html/phpinfo.php

Add the following content:

<?php
phpinfo();
?>

Access this file through your web browser at http://your-server-ip/phpinfo.php. You should see a comprehensive PHP information page displaying PHP 8.4 and all loaded extensions.

Important: Remove this file after testing, as it exposes sensitive system information.

Running PHP from Command Line

Test command-line functionality:

php -r "echo 'PHP 8.4 is working!' . PHP_EOL;"

Check for specific extensions:

php -m | grep -i mysql
php -m | grep -i curl

Checking Loaded Extensions

List all loaded extensions:

php -m

For detailed extension information:

php --ri extensionname

For example, to check MySQL extension details:

php --ri mysqli

Common Installation Issues and Troubleshooting

Even with careful installation, issues can arise. Here are solutions to common problems.

Permission Problems

If you encounter permission errors, ensure proper ownership of web directories:

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

For development environments, you might need to add your user to the www-data group:

sudo usermod -a -G www-data $USER

Missing Dependencies

If extensions fail to load, install missing system libraries:

sudo apt install libc-client-dev libkrb5-dev
sudo apt install libmagickwand-dev
sudo apt install libmcrypt-dev

Port Conflicts and Service Issues

Check if services are running correctly:

sudo systemctl status php8.4-fpm
sudo systemctl status apache2
sudo systemctl status nginx

View service logs for error details:

sudo journalctl -u php8.4-fpm -f
sudo tail -f /var/log/apache2/error.log
sudo tail -f /var/log/nginx/error.log

Best Practices for PHP 8.4 Management

Maintaining your PHP installation requires ongoing attention to updates, security, and performance optimization.

Version Management

If you need multiple PHP versions, use update-alternatives to manage them:

sudo update-alternatives --install /usr/bin/php php /usr/bin/php8.4 84
sudo update-alternatives --config php

This allows easy switching between PHP versions when needed.

Security Updates and Maintenance

Keep PHP 8.4 updated with regular system updates:

sudo apt update && sudo apt upgrade

Monitor security advisories and apply patches promptly. Consider setting up automatic security updates for critical systems:

sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

Regular monitoring of PHP error logs helps identify potential issues:

sudo tail -f /var/log/php8.4-fpm.log

Frequently Asked Questions

1. Can I install PHP 8.4 alongside other PHP versions on Ubuntu?

Yes, you can install multiple PHP versions simultaneously on Ubuntu. The Ondřej Surý PPA supports multiple versions, and you can use update-alternatives to switch between them or configure different versions for different projects using virtual hosts or PHP-FPM pools.

2. What should I do if PHP 8.4 breaks my existing application?

First, check your application’s PHP 8.4 compatibility. Many compatibility issues can be resolved by updating deprecated function calls or adjusting configuration settings. If immediate fixes aren’t possible, you can temporarily switch back to an older PHP version while working on compatibility updates.

3. How do I optimize PHP 8.4 performance for production environments?

Enable OPcache, adjust memory limits based on your application needs, tune PHP-FPM worker processes, and consider using a reverse proxy like Nginx. Regular monitoring and performance testing help identify bottlenecks and optimization opportunities.

4. Is it safe to upgrade from PHP 7.4 directly to PHP 8.4?

While technically possible, it’s recommended to test thoroughly before upgrading production systems. PHP 8.x introduced breaking changes from 7.x versions. Create a testing environment first, update your code for compatibility, and then perform the production upgrade.

5. How often should I update PHP 8.4 and its extensions?

Install security updates as soon as they’re available. For feature updates, follow a regular schedule (monthly or quarterly) after testing in a development environment. Subscribe to PHP security announcements and your Linux distribution’s security notifications to stay informed about critical updates.

Marshall Anthony is a professional Linux DevOps writer with a passion for technology and innovation. With over 8 years of experience in the industry, he has become a go-to expert for anyone looking to learn more about Linux.

Related Posts