How To Install PHP 8 On Ubuntu 22.04 LTS

How To Install PHP 8 On Ubuntu 22.04 LTS

It provides all the steps required to install PHP 8 on Ubuntu 22.04 LTS.

December 20, 2022

This tutorial provides all the steps required to install PHP 8 on Ubuntu 22.04 LTS. The steps should be similar on the other versions of Ubuntu and Linux based systems.

Install PHP 8

In this step, we will install PHP 8 on Ubuntu 22.04 LTS.

# Refresh indexes
sudo apt update

We can install PHP 8 on Ubuntu 22.04 LTS using the commands shown below.

# Install PHP 8 on Ubuntu 22.04 LTS
sudo apt install php8.1

# The command also installs apache2 and libapache2-mod-php8.1 in case it's not installed
# We can specify the option --no-install-recommends to avoid installion of apache2

It will ask to confirm the installation. Press Y and hit Enter to confirm the installation. The important paths and files of the PHP installation are /usr/lib/php/8.1/usr/bin/php8.1, and /etc/php/8.1/cli/php.ini.

# Verify PHP
php --version

# Output
PHP 8.1.2-1ubuntu2.9 (cli) (built: Oct 19 2022 14:58:09) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.2, Copyright (c) Zend Technologies with Zend OPcache v8.1.2-1ubuntu2.9, Copyright (c), by Zend Technologies

# Autoclean
sudo apt autoclean

# Autoremove
sudo apt autoremove
# OR
sudo apt --purge autoremove

Install Apache2 Module

In case the Apache2 Module was not installed while installing PHP, we can use the below-mentioned commands to install the Apache2 Module for PHP. It's required for the Apache Web Server.

# Install Apache2 Module if not installed by default
sudo apt install libapache2-mod-php8.1

PHP for NGINX

We can install PHP FPM and configure it for Nginx. This will eliminate the need for the Apache Web Server and Nginx can handle the PHP requests.

# FPM
sudo apt install php8.1-fpm

# Check Status
systemctl status php8.1-fpm

# Output
● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2022-12-20 10:43:17 IST; 8s ago Docs: man:php-fpm8.1(8) Process: 39943 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.1/fpm/pool.d/www.conf 81 (code=exit> Main PID: 39940 (php-fpm8.1) Status: "Ready to handle connections" Tasks: 3 (limit: 18936) Memory: 7.1M CPU: 60ms CGroup: /system.slice/php8.1-fpm.service ├─39940 "php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ├─39941 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> └─39942 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> Dec 20 10:43:16 bravo systemd[1]: Starting The PHP 8.1 FastCGI Process Manager... Dec 20 10:43:17 bravo systemd[1]: Started The PHP 8.1 FastCGI Process Manager.

Install MySQL Extension

Use the below-mentioned commands to install the MySQL extension for PHP 8.

# Install MySQL Extension
sudo apt install php8.1-mysql

# Enable PDO module
sudo phpenmod pdo_mysql

# Install MySQL Extension - Required for Wordpress Installation
sudo apt install php-mysql

Additional Extensions

You can also install the additional extensions in order to fully support a web application. Some of these packages might not be required in your scenario, hence it's totally optional to install these extensions.

# Install CGI and CLI if not installed by default
sudo apt install php8.1-common php8.1-cli php8.1-cgi

# Install CURL extensions
sudo apt install php8.1-curl

# Install PHP GD and Imagick
sudo apt install php8.1-gd php-imagick

# Multibyte String, Internationalization and Spell Check
sudo apt install php8.1-mbstring php8.1-intl php8.1-pspell

# Multibyte String, Internationalization and Spell Check - Required for Wordpress Installation
sudo apt install php-mbstring php-intl php-pspell

# Emails
sudo apt install php8.1-imap

# SQLite Database
sudo apt install php8.1-sqlite3

# Tidy and XML RPC
sudo apt install php8.1-tidy php8.1-xmlrpc

# Excel
sudo apt install php8.1-xsl

Install OPcache to enable caching at the bytecode level.

# Install OPcache extension
sudo apt install php8.1-opcache

Install the extensions to handle compressed files.

# Install Zip
sudo apt install php8.1-zip

These are the steps required to install PHP 8 on Ubuntu 22.04 LTS. We have also installed the additional packages that are mostly required for the website development.

Summary

This tutorial provided all the steps required to install PHP 8 on Ubuntu 22.04 LTS. We have also installed the popular extensions for PHP which are required to host web applications.

Write a Comment
Click the captcha image to get new code.
Discussion Forum by DISQUS