
How to Install PHP 8.1 and Set Up a Local Development Environment on Ubuntu 24
Setting up PHP 8.1 on Ubuntu 24 isn’t just about getting the latest version running – it’s about creating a solid foundation for modern web development. With PHP 8.1 bringing features like enums, readonly properties, and significant performance improvements, you’ll want to get this right the first time. This guide walks you through the entire process, from installation to configuration, plus some neat tricks I’ve picked up managing servers over the years. Whether you’re spinning up a new VPS or setting up a local dev environment, these steps will get you running smooth as butter.
How PHP 8.1 Works on Ubuntu 24
Ubuntu 24 ships with its own PHP packages, but they’re often not the latest versions. PHP 8.1 introduced some killer features that make it worth the upgrade: union types, match expressions, and named arguments that’ll make your code cleaner and faster. The beauty of Ubuntu’s package management system is that you can add third-party repositories (like Ondřej Surý’s PPA) to get bleeding-edge versions without breaking your system.
Here’s what happens under the hood:
- Package Resolution: Ubuntu’s APT system checks your enabled repositories for the latest PHP packages
- Dependency Management: PHP 8.1 requires specific versions of libraries like libssl, libxml2, and others
- Service Integration: PHP-FPM (FastCGI Process Manager) handles web requests more efficiently than mod_php
- Extension Loading: Extensions are loaded dynamically through .ini files in /etc/php/8.1/
Step-by-Step Installation Guide
Let’s dive into the meat and potatoes. I’ll show you two methods: the PPA way (recommended) and compiling from source (for the brave souls).
Method 1: Using Ondřej Surý’s PPA (Recommended)
First, update your system and install prerequisites:
sudo apt update && sudo apt upgrade -y
sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https -y
Add the PPA repository:
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
Install PHP 8.1 and essential extensions:
sudo apt install php8.1 php8.1-fpm php8.1-mysql php8.1-mbstring php8.1-xml php8.1-gd php8.1-curl php8.1-zip php8.1-intl php8.1-bcmath php8.1-soap php8.1-readline -y
Verify the installation:
php -v
php-fpm8.1 -v
You should see output like:
PHP 8.1.29 (cli) (built: Jun 4 2024 19:34:06) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.29, Copyright (c) Zend Technologies
with Zend OPcache v8.1.29, Copyright (c) The PHP Group
Method 2: Compiling from Source (Advanced)
Only do this if you need specific compile-time options or you’re running a dedicated server where you want maximum control:
sudo apt install build-essential autoconf libtool bison re2c pkg-config libssl-dev libsqlite3-dev libzip-dev libxml2-dev libreadline-dev -y
wget https://www.php.net/distributions/php-8.1.29.tar.gz
tar -xzf php-8.1.29.tar.gz
cd php-8.1.29
./configure --prefix=/usr/local/php81 \
--with-config-file-path=/usr/local/php81/etc \
--enable-fpm \
--with-fpm-user=www-data \
--with-fpm-group=www-data \
--with-mysqli \
--with-pdo-mysql \
--with-openssl \
--with-zip \
--enable-mbstring \
--enable-intl
make -j$(nproc)
sudo make install
Setting Up Your Development Environment
Configuring PHP-FPM
PHP-FPM is where the magic happens for web applications. Edit the main config:
sudo nano /etc/php/8.1/fpm/php.ini
Key settings to modify for development:
memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
display_errors = On
error_reporting = E_ALL
date.timezone = UTC
Configure the FPM pool:
sudo nano /etc/php/8.1/fpm/pool.d/www.conf
Optimize these settings based on your server specs:
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.process_idle_timeout = 10s
Installing Nginx (Recommended Web Server)
Nginx plays better with PHP-FPM than Apache in most cases:
sudo apt install nginx -y
sudo systemctl enable nginx php8.1-fpm
sudo systemctl start nginx php8.1-fpm
Create a development site configuration:
sudo nano /etc/nginx/sites-available/dev-site
Add this configuration:
server {
listen 80;
server_name localhost dev.local;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Enable the site:
sudo ln -s /etc/nginx/sites-available/dev-site /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Testing Your Setup
Create a test PHP file:
sudo nano /var/www/html/info.php
Add this content:
PHP 8.1 Features Test";
echo "User: {$user->name} ({$user->status->value})
";
?>
Visit http://localhost/info.php
to see if everything’s working.
Real-World Examples and Use Cases
Performance Comparison
Here’s what you can expect from PHP 8.1 vs older versions:
PHP Version | Requests/Second | Memory Usage | Notable Features |
---|---|---|---|
PHP 7.4 | 8,500 | 12MB | Arrow functions, typed properties |
PHP 8.0 | 9,200 | 11MB | JIT compiler, union types |
PHP 8.1 | 9,800 | 10.5MB | Enums, readonly properties, fibers |
Common Development Scenarios
Scenario 1: Laravel Development
Install Composer and create a Laravel project:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
composer global require laravel/installer
laravel new my-awesome-app
cd my-awesome-app
php artisan serve --host=0.0.0.0 --port=8000
Scenario 2: WordPress Development
Download and set up WordPress:
cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzf latest.tar.gz
sudo chown -R www-data:www-data wordpress/
sudo chmod -R 755 wordpress/
Scenario 3: API Development with Slim Framework
composer require slim/slim:"4.*"
composer require slim/psr7
composer require slim/http
Troubleshooting Common Issues
Problem: “502 Bad Gateway” error
Solution: Check if PHP-FPM is running and the socket path is correct:
sudo systemctl status php8.1-fpm
sudo netstat -tunlp | grep php
ls -la /var/run/php/
Problem: Extensions not loading
Solution: Verify extension installation and configuration:
php -m | grep extension_name
sudo nano /etc/php/8.1/fpm/conf.d/20-extension.ini
Problem: Permission denied errors
Solution: Fix ownership and permissions:
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
sudo usermod -a -G www-data $USER
Advanced Configuration and Optimization
OPcache Tuning
OPcache can boost performance by 2-3x when configured properly:
sudo nano /etc/php/8.1/fpm/conf.d/10-opcache.ini
Add these optimizations:
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.revalidate_freq=2
opcache.fast_shutdown=1
opcache.enable_cli=1
opcache.jit=1255
opcache.jit_buffer_size=128M
Security Hardening
Lock down PHP for production environments:
sudo nano /etc/php/8.1/fpm/php.ini
Security-focused settings:
expose_php = Off
allow_url_fopen = Off
allow_url_include = Off
display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log
disable_functions = exec,passthru,shell_exec,system,proc_open,popen
Multiple PHP Versions
You can run multiple PHP versions simultaneously:
sudo apt install php7.4-fpm php8.0-fpm php8.1-fpm -y
# Check all running versions
sudo systemctl status php7.4-fpm php8.0-fpm php8.1-fpm
Configure Nginx to use different versions for different sites:
# For PHP 8.1 sites
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
# For PHP 7.4 legacy sites
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
Essential Development Tools
Install additional tools that’ll make your life easier:
# Xdebug for debugging
sudo apt install php8.1-xdebug -y
# Database tools
sudo apt install mysql-server phpmyadmin -y
# Version control
sudo apt install git -y
# Node.js for asset compilation
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install nodejs -y
Xdebug Configuration
Configure Xdebug for development:
sudo nano /etc/php/8.1/fpm/conf.d/20-xdebug.ini
Add these settings:
zend_extension=xdebug.so
xdebug.mode=debug,develop
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
xdebug.start_with_request=yes
xdebug.log=/tmp/xdebug.log
Automation and Scripting Possibilities
PHP 8.1 opens up some cool automation possibilities with its new features:
Fibers for Async Operations
Create non-blocking operations without traditional callbacks:
start();
echo "Fiber suspended with: $value\n";
$fiber->resume('Hello World');
?>
System Monitoring Scripts
Use PHP for server monitoring:
2.0) {
// Send alert
mail('admin@example.com', 'High CPU Load', "Load: {$load[0]}");
}
?>
Deployment Automation
Create deployment scripts using PHP 8.1’s match expressions:
[
'debug' => true,
'cache' => false,
'db_host' => 'localhost'
],
'staging' => [
'debug' => false,
'cache' => true,
'db_host' => 'staging-db.example.com'
],
'production' => [
'debug' => false,
'cache' => true,
'db_host' => 'prod-db.example.com'
]
};
?>
Integration with Modern DevOps Tools
Docker Integration
Create a Dockerfile for your PHP 8.1 environment:
FROM ubuntu:24.04
RUN apt update && apt install -y software-properties-common
RUN add-apt-repository ppa:ondrej/php -y
RUN apt update && apt install -y php8.1-fpm nginx
COPY nginx.conf /etc/nginx/sites-available/default
COPY php.ini /etc/php/8.1/fpm/php.ini
EXPOSE 80
CMD service php8.1-fpm start && nginx -g 'daemon off;'
CI/CD Pipeline Integration
GitHub Actions workflow for PHP 8.1:
name: PHP 8.1 CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v3
- name: Setup PHP 8.1
run: |
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install php8.1 php8.1-mbstring php8.1-xml
- name: Run tests
run: php vendor/bin/phpunit
Monitoring and Maintenance
Set up proper monitoring for your PHP environment:
# Check PHP-FPM status
sudo systemctl status php8.1-fpm
# Monitor PHP-FPM processes
sudo tail -f /var/log/php8.1-fpm.log
# Check error logs
sudo tail -f /var/log/nginx/error.log
# Monitor resource usage
htop
iotop
Log Rotation
Configure log rotation to prevent disk space issues:
sudo nano /etc/logrotate.d/php8.1-fpm
Add this configuration:
/var/log/php8.1-fpm.log {
daily
missingok
rotate 14
compress
notifempty
create 0640 www-data adm
postrotate
systemctl reload php8.1-fpm
endscript
}
Conclusion and Recommendations
Setting up PHP 8.1 on Ubuntu 24 gives you a solid foundation for modern web development. The performance improvements alone make it worth the upgrade, but the new language features like enums and readonly properties will make your code more maintainable and less bug-prone.
When to use this setup:
- New projects that can leverage PHP 8.1 features
- High-traffic applications that need the performance boost
- Development environments where you want to stay current
- Microservices architectures where PHP’s improved async capabilities shine
Where to deploy:
- Use a VPS for small to medium projects where you need flexibility
- Go with a dedicated server for high-traffic applications or when you need maximum performance
- Local development environments for testing and experimentation
Remember to keep your system updated, monitor your logs, and always test configuration changes in a development environment first. PHP 8.1 is a solid choice that’ll serve you well for years to come, especially with Ubuntu 24’s long-term support backing it up.
For more information, check out the official PHP 8.1 release notes and the Ubuntu Server documentation.

This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.
This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.