How7o
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Reading: How to Install PHP on Ubuntu (22.04 & 24.04): Step-by-Step Guide
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > Web Development > How to Install PHP on Ubuntu (22.04 & 24.04): Step-by-Step Guide
Web Development

How to Install PHP on Ubuntu (22.04 & 24.04): Step-by-Step Guide

how7o
By how7o
Last updated: April 16, 2026
9 Min Read
Install PHP on Ubuntu — terminal with apt install php command and stylized elephant icon
SHARE

This guide walks through how to install PHP on Ubuntu — both the 22.04 LTS default (PHP 8.1) and the 24.04 LTS default (PHP 8.3) — then layers on the extensions a typical web application needs, and finishes with a hello.php test so you can confirm the CLI works end-to-end. If you need to run multiple PHP versions side-by-side, I cover that too via the community-maintained Ondřej Surý PPA.

Contents
  • TL;DR
  • Prerequisites
  • Step 1 — Update the package index
  • Step 2 — Install PHP
  • Step 3 — Verify the installation
  • Step 4 — Install the extensions your application needs
  • Step 5 — Set up the rest of the stack
  • Step 6 — Test with a hello.php script
  • Troubleshooting
    • php: command not found after install
    • apt complains about conflicting PHP versions
    • Extension shows installed but php -m doesn’t list it
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-16 on Ubuntu 22.04 LTS (PHP 8.1) and 24.04 LTS (PHP 8.3). Originally published 2023-02-25, rewritten and updated 2026-04-16.

TL;DR

Run sudo apt update && sudo apt install php to install the distro-default PHP (8.1 on 22.04, 8.3 on 24.04). Add extensions for common web apps with sudo apt install php-cli php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-bcmath. Confirm with php -v and php -m. For a specific version, add the Ondřej Surý PPA before installing.

Prerequisites

  • An Ubuntu 22.04 or 24.04 system.
  • A user with sudo privileges.
  • Outbound access to archive.ubuntu.com (and ppa.launchpad.net if you’ll use the Ondřej PPA).

Step 1 — Update the package index

Pull the latest package lists so apt knows about the current PHP builds in the Ubuntu repositories:

sudo apt update

Step 2 — Install PHP

The simplest install uses the distro-default PHP meta-package:

sudo apt install php

This gives you PHP 8.1 on Ubuntu 22.04 and PHP 8.3 on Ubuntu 24.04, along with Apache and libapache2-mod-php as recommended dependencies. If you’re running nginx + PHP-FPM and don’t want Apache pulled in, install without recommends:

sudo apt install --no-install-recommends php php-fpm

To pin a specific version (say, PHP 8.2 on 22.04 where the default is 8.1), first add the community-maintained Ondřej Surý PPA, which packages every supported PHP version:

sudo apt install -y software-properties-common
sudo add-apt-repository -y ppa:ondrej/php
sudo apt update
sudo apt install php8.2

About older versions: PHP 7.4 (EOL November 2022) and PHP 8.0 (EOL November 2023) no longer receive security updates. Install them only for maintaining legacy applications in a sandbox — never for anything reachable from the public internet.

Step 3 — Verify the installation

php -v

You should see output like this:

PHP 8.1.2-1ubuntu2.x (cli) (built: ...) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies

If php -v returns command not found, the binary isn’t on your PATH — skip to the Troubleshooting section.

install php on ubuntu — apt install php, add extensions, verify with php -v flow

Step 4 — Install the extensions your application needs

Each PHP extension is a separate apt package following the pattern php{VERSION}-{EXTENSION}. You can omit the version to let apt pick the default:

sudo apt install php-PACKAGE_NAME

Most web applications (Laravel, WordPress, modern CMSs) need these together:

sudo apt install -y php-cli php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-bcmath
  • php-cli — command-line interpreter for running scripts from a shell.
  • php-common — shared documentation, examples, and common modules.
  • php-mysql — MySQL/MariaDB driver (PDO and mysqli).
  • php-zip — zip and unzip archives from PHP.
  • php-gd — image manipulation (resizing, watermarks, thumbnails).
  • php-mbstring — multibyte string handling for non-ASCII characters.
  • php-curl — outbound HTTP requests.
  • php-xml — XML parsing and DOM, required by a lot of frameworks.
  • php-bcmath — arbitrary-precision arithmetic, required by several e-commerce and crypto libraries.

To see every extension currently loaded by the CLI:

php -m

Step 5 — Set up the rest of the stack

A working PHP CLI is enough for scripting, but most projects also need a database and a way to manage dependencies. The two pieces I’d install next:

  • MySQL for data storage. Follow How to Install MySQL on Ubuntu 22.04 — same Ubuntu server, secure setup included.
  • Composer for dependency management. Follow How to Install Composer on Ubuntu — the official installer flow, with live hash verification.

If you need the Apache web server in front of PHP, install it with sudo apt install apache2 libapache2-mod-php. For nginx + PHP-FPM, use sudo apt install nginx php-fpm instead.

Step 6 — Test with a hello.php script

Create a simple script to confirm the interpreter runs end-to-end:

nano hello.php

Paste the following — modern PHP style drops the closing ?> tag for scripts that only contain PHP code:

<?php
echo 'Hello World!' . PHP_EOL;

Save and exit (Ctrl+O, Enter, then Ctrl+X in nano; :wq in vi). Then run it:

php hello.php

If you see Hello World! on the next line, your PHP environment is ready.

Troubleshooting

php: command not found after install

Confirm the binary actually landed: ls /usr/bin/php*. If you only see /usr/bin/php8.1 (no bare /usr/bin/php), the meta-package didn’t install the alternative. Fix it with sudo update-alternatives --install /usr/bin/php php /usr/bin/php8.1 100.

apt complains about conflicting PHP versions

Installing multiple versions via Ondřej’s PPA is supported, but some packages (like libapache2-mod-php) can only be the active one at a time. Use sudo update-alternatives --config php to switch the CLI, and configure each PHP-FPM pool to listen on its own socket.

Extension shows installed but php -m doesn’t list it

Some extensions are enabled per SAPI (CLI vs FPM vs Apache). If it’s only missing in one, enable it there: sudo phpenmod -s cli extension_name. Then re-run php -m to confirm.

Frequently asked questions

Which PHP version does apt install php give me on Ubuntu?

On Ubuntu 22.04 LTS the php meta-package currently resolves to PHP 8.1, and on 24.04 LTS it resolves to PHP 8.3. If you need a specific version that isn’t the distro default — for example 8.2 on 22.04 — add the Ondřej Surý PPA first (ppa:ondrej/php) and install php8.2 explicitly.

Is PHP 7.4 or 8.0 safe to install today?

No. PHP 7.4 reached end-of-life in November 2022 and PHP 8.0 in November 2023 — no more security patches are published. Install them only for maintaining legacy applications in a sandbox. For anything touching the public internet, use PHP 8.1 or newer.

Why did apt install php8.1 pull in Apache?

The php8.1 meta-package recommends the libapache2-mod-php8.1 module, which in turn pulls in apache2. If you don’t want Apache — for example you’re using nginx + PHP-FPM — install with sudo apt install --no-install-recommends php8.1 and add php8.1-fpm instead.

How do I list the PHP extensions currently loaded?

Run php -m. The output groups compiled modules and Zend extensions separately. Use php -i | grep -i extension_dir to see where additional .so extensions are loaded from.

How do I switch between multiple PHP versions on the same server?

Install each version side-by-side (php8.1, php8.2, php8.3) and use sudo update-alternatives --config php to switch the php CLI. For web requests, each FPM pool listens on its own socket, so nginx or Apache picks the version per vhost.

Related guides

  • How to Install MySQL on Ubuntu 22.04 — the database layer of your LAMP stack.
  • How to Install Composer on Ubuntu — the dependency manager Laravel, Symfony, and most modern PHP projects require.

References

Official PHP manual: php.net/manual. Supported versions and EOL dates: php.net/supported-versions. Ondřej Surý PHP PPA: launchpad.net/~ondrej/+archive/ubuntu/php.

TAGGED:aptBashcomposerconfigurationinstallationphpUbuntu

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Install Composer on Ubuntu — terminal with composer-setup.php and PHP elephant icon How to Install Composer on Ubuntu: Step-by-Step Guide
Next Article Install Laravel on Ubuntu — terminal with composer create-project command and Laravel red-pillar icon How to Install Laravel on Ubuntu: Step-by-Step Guide
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
Step-by-step guide to upgrading the Linux kernel in CentOS 7 using ELRepo
How to Upgrade the Linux Kernel in CentOS 7
April 16, 2026
Install Node.js on Ubuntu — terminal with NodeSource setup_22.x curl command and Node.js hexagon icon
How to Install Node.js on Ubuntu (22.04 & 24.04): Step-by-Step
April 16, 2026
Configure WordPress multisite with subdirectories on Nginx — nginx gear + wordpress tree with subsite branches
How to Configure WordPress Multisite with Subdirectories on Nginx
April 16, 2026
Install Laravel on Ubuntu — terminal with composer create-project command and Laravel red-pillar icon
How to Install Laravel on Ubuntu: Step-by-Step Guide
April 16, 2026
Install PHP on Ubuntu — terminal with apt install php command and stylized elephant icon
How to Install PHP on Ubuntu (22.04 & 24.04): Step-by-Step Guide
April 16, 2026

You Might Also Like

Debug PHP like console.log using error_log and server logs
Web Development

How to Debug in PHP Like console.log (echo, error_log, WordPress debug.log)

6 Min Read
Dynamically set site title and tagline in WordPress by country
Web Development

How to Dynamically Set Site Title and Tagline in WordPress (By Country)

6 Min Read
Change welcome message on Ubuntu VPS server (MOTD + SSH banner)
Server Management

Change Welcome Message on Ubuntu VPS (MOTD + SSH Banner)

6 Min Read
Automatic logout timeout for command line in Ubuntu (TMOUT 300s)
Server Management

Automatic Logout Timeout for Command Line in Ubuntu (TMOUT 300s)

5 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?