Installing PHP 8.1 and Setting Up a Local Development Environment on Ubuntu
PHP remains one of the most popular server-side scripting languages for web development, powering millions of websites and applications worldwide. With the release of PHP 8.1, developers gain access to numerous performance improvements, new features like enums, readonly properties, fibers, and enhanced type system capabilities.
Setting up a proper local development environment is crucial for efficient PHP development. This guide will walk you through installing PHP 8.1 on Ubuntu and configuring a complete development stack that includes a web server, database, and essential tools.
Table of Contents#
- Prerequisites
- Updating System Packages
- Adding PHP Repository
- Installing PHP 8.1
- Installing Apache Web Server
- Installing MySQL Database
- Configuring PHP Development Environment
- Installing Composer
- Setting Up a Sample Project
- Alternative: Using XAMPP
- Troubleshooting Common Issues
- Conclusion
- References
Prerequisites#
Before starting, ensure you have:
- Ubuntu 18.04, 20.04, or 22.04 LTS
- sudo privileges or root access
- At least 2GB of free disk space
- Stable internet connection
Updating System Packages#
First, update your package list and upgrade existing packages to their latest versions:
sudo apt update
sudo apt upgrade -yInstall some essential packages that will be needed:
sudo apt install -y software-properties-common apt-transport-https ca-certificates curl wget git unzipAdding PHP Repository#
Ubuntu's default repositories might not have the latest PHP version. We'll add the Ondřej Surý PPA which maintains updated PHP packages for Ubuntu:
sudo add-apt-repository ppa:ondrej/php -y
sudo apt updateBest Practice: Always use trusted repositories like Ondřej Surý's PPA for PHP packages as they are well-maintained and reliable.
Installing PHP 8.1#
Install PHP 8.1 along with commonly used extensions:
sudo apt install -y php8.1 php8.1-cli php8.1-common php8.1-fpmInstall additional PHP extensions that are commonly needed for web development:
sudo apt install -y php8.1-mysql php8.1-xml php8.1-curl php8.1-gd php8.1-mbstring php8.1-zip php8.1-bcmath php8.1-intl php8.1-soap php8.1-sqlite3Verify the installation:
php -vYou should see output similar to:
PHP 8.1.x (cli) (built: ...)
Check loaded extensions:
php -mInstalling Apache Web Server#
Install Apache and the PHP Apache module:
sudo apt install -y apache2 libapache2-mod-php8.1Enable necessary Apache modules:
sudo a2enmod php8.1
sudo a2enmod rewrite
sudo a2enmod headersStart and enable Apache to run on boot:
sudo systemctl start apache2
sudo systemctl enable apache2Check Apache status:
sudo systemctl status apache2Installing MySQL Database#
Install MySQL server:
sudo apt install -y mysql-serverSecure your MySQL installation:
sudo mysql_secure_installationThis will prompt you to:
- Set root password (if not set)
- Remove anonymous users
- Disallow root login remotely
- Remove test database
- Reload privilege tables
Best Practice: Always run mysql_secure_installation after installing MySQL to enhance security.
Start and enable MySQL:
sudo systemctl start mysql
sudo systemctl enable mysqlConfiguring PHP Development Environment#
PHP Configuration for Development#
Edit the PHP configuration file for development:
sudo nano /etc/php/8.1/apache2/php.iniMake the following changes for optimal development environment:
; Display errors for development
display_errors = On
display_startup_errors = On
; Error reporting level
error_reporting = E_ALL
; Increase memory limit
memory_limit = 256M
; Increase maximum execution time
max_execution_time = 120
; Increase post and upload limits
post_max_size = 50M
upload_max_filesize = 50M
; Set timezone
date.timezone = America/New_YorkCommon Practice: Use different php.ini files for development and production. The above settings are suitable for development but should be tightened for production.
Apache Virtual Host Configuration#
Create a new virtual host for your development projects:
sudo nano /etc/apache2/sites-available/dev.local.confAdd the following configuration:
<VirtualHost *:80>
ServerName dev.local
ServerAlias www.dev.local
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/dev_error.log
CustomLog ${APACHE_LOG_DIR}/dev_access.log combined
</VirtualHost>Enable the site and disable the default:
sudo a2ensite dev.local.conf
sudo a2dissite 000-default.confAdd local domain to hosts file:
sudo nano /etc/hostsAdd the line:
127.0.0.1 dev.local
Restart Apache to apply changes:
sudo systemctl restart apache2Installing Composer#
Composer is the dependency manager for PHP. Install it globally:
curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composerVerify installation:
composer --versionBest Practice: Always verify the installer signature for security:
curl -sS https://composer.github.io/installer.sig
php -r "if (hash_file('SHA384', 'composer-setup.php') === '<hash-from-above>') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"Setting Up a Sample Project#
Let's create a simple PHP project to test our setup:
sudo mkdir -p /var/www/html/myproject
sudo chown -R $USER:$USER /var/www/html/myproject
cd /var/www/html/myprojectCreate a basic index.php file:
<?php
// Basic PHP info page
phpinfo();
// Simple test
echo "<h1>Hello, PHP 8.1!</h1>";
echo "<p>Current PHP version: " . PHP_VERSION . "</p>";
// Database connection test (optional)
try {
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'password');
echo "<p>Database connection: <span style='color: green;'>Successful</span></p>";
} catch (PDOException $e) {
echo "<p>Database connection: <span style='color: red;'>Failed - " . $e->getMessage() . "</span></p>";
}
// Show loaded extensions
echo "<h2>Loaded Extensions:</h2>";
echo "<ul>";
foreach (get_loaded_extensions() as $ext) {
echo "<li>" . $ext . "</li>";
}
echo "</ul>";
?>Create a composer.json file for dependency management:
{
"name": "mycompany/myproject",
"description": "Sample PHP 8.1 project",
"type": "project",
"require": {
"php": "^8.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
"MyProject\\": "src/"
}
}
}Install dependencies:
composer installVisit http://dev.local/myproject in your web browser to see your PHP application.
Alternative: Using XAMPP#
For those who prefer an all-in-one solution, XAMPP is a good alternative:
# Download XAMPP
wget https://www.apachefriends.org/xampp-files/8.1.4/xampp-linux-x64-8.1.4-0-installer.run
# Make it executable
chmod +x xampp-linux-x64-8.1.4-0-installer.run
# Install
sudo ./xampp-linux-x64-8.1.4-0-installer.runNote: XAMPP is easier to set up but offers less flexibility than individual installations.
Troubleshooting Common Issues#
PHP not working with Apache#
# Check if PHP module is enabled
sudo apache2ctl -M | grep php
# If not found, enable it
sudo a2enmod php8.1
sudo systemctl restart apache2Permission issues#
# Fix ownership
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/htmlPort 80 already in use#
# Check what's using port 80
sudo netstat -tulpn | grep :80
# Stop conflicting service or configure Apache to use different portExtension not loading#
# Check if extension is installed
php -m | grep extension_name
# Install missing extension
sudo apt install php8.1-extension_nameConclusion#
You've successfully set up a complete PHP 8.1 development environment on Ubuntu. This setup includes:
- PHP 8.1 with essential extensions
- Apache web server configured for development
- MySQL database server
- Composer for dependency management
- Properly configured virtual host
This environment provides a solid foundation for PHP web development. Remember to:
- Regularly update your packages with
sudo apt update && sudo apt upgrade - Use version control (Git) for your projects
- Implement proper security measures for production deployments
- Keep your development tools updated
For production environments, remember to tighten security settings, disable error displaying, and optimize performance settings.
References#
- PHP Official Documentation
- Apache HTTP Server Documentation
- MySQL Documentation
- Composer Documentation
- Ubuntu Server Guide
- PHP The Right Way
Additional Resources:
Happy coding with PHP 8.1!