How to add PHP 8.1.24 to Ubuntu 22.04

Published on Author JFLeave a comment

You’re running the default PHP 8.1.22 on Ubuntu 22.04 and need to update to 8.1.24. Fortunately, the ondrej/php repository includes 8.1.22 so you can just remove the existing php, add ondrej/php and then install 8.1.24.

Check version on Ubuntu

php -v
> PHP 8.1.2-1ubuntu2.14 (cli) (built: Aug 18 2023 11:41:11) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.14, Copyright (c), by Zend Technologies

php -v | grep ^PHP | cut -d' ' -f2
> 8.1.2-1ubuntu2.14

Remove PHP

  • Check if php is on hold. If it is, unhold it.
apt-mark showhold
apt-mark unhold php
  • Now remove it

Note: we are keeping /etc/…php.ini because we want to keep the config file — no need to remove it. Also, I’m not using -y. The output of the remove will show what PHP modules are installed for reference. Copy these to a txt doc to make it easy to re-install. If you want a complete list of all modules and dependencies you can do php -M.

apt remove php-common
apt update

You can purge, but be very careful of removing things you don’t want to remove.

apt purge php8.1*
apt update
  • Install ondrej/php repository. I like to do install > update > upgrade > reboot
add-apt-repository ppa:ondrej/php
apt update
apt list --upgradeable
apt upgrade
reboot

Install PHP 8.1.24

This is just a sample list of modules needed.

apt install php8.1 php8.1-bz2 php8.1-cli php8.1-common php8.1-curl php8.1-gd php8.1-gmp php8.1-intl php8.1-mbstring php8.1-mcrypt php8.1-mysql php8.1-opcache php8.1-readline php8.1-soap php8.1-sqlite3 php8.1-uploadprogress php8.1-xml php8.1-xmlrpc php8.1-yaml 

php -v 

PHP 8.1.24 (cli) (built: Oct 6 2023 09:46:42) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.24, Copyright (c) Zend Technologies
with Zend OPcache v8.1.24, Copyright (c), by Zend Technologies

PHP 8.1.24 is now installed.

If you want to prevent automatic updates:

apt-mark hold php 
apt-mark showhold 

Check with phpinfo();


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.