Report this

What is the reason for this report?

Upgrade to latest php from php7.4 - nginx Ubuntu 20

Posted on August 13, 2025

I’m trying to upgrade PHP from 7.4 to the latest stable version on Ubuntu 20.04.

However, when I run:

sudo apt-get install php8.1

I get the following error:

N: Unable to locate package php8.1 N: Couldn’t find any package by glob ‘php8.1’ N: Couldn’t find any package by regex ‘php8.1’



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

The issue you’re encountering is that Ubuntu 20.04’s default repositories don’t include newer PHP versions like 8.1. You’ll need to add a third-party repository that provides updated PHP packages.

Here’s how to upgrade to the latest stable PHP version on Ubuntu 20.04:

Step 1: Add Ondřej Surý’s PHP Repository

This is the most popular and trusted repository for newer PHP versions on Ubuntu:

# Add the repository
sudo add-apt-repository ppa:ondrej/php

# Update package lists
sudo apt update

Step 2: Install the Latest PHP Version

As of mid 2025, PHP 8.3 is the latest stable version:

# Install PHP 8.3 (replace with current latest if different)
sudo apt install php8.3

# Install commonly needed extensions
sudo apt install php8.3-cli php8.3-common php8.3-mysql php8.3-zip php8.3-gd php8.3-mbstring php8.3-curl php8.3-xml php8.3-bcmath

Step 3: Switch PHP Versions (if needed)

If you have multiple PHP versions installed, you can switch between them:

# Check available versions
sudo update-alternatives --config php

# Or set a specific version as default
sudo update-alternatives --set php /usr/bin/php8.3

Step 4: Update Web Server Configuration

If you’re using Apache:

# Disable old PHP module
sudo a2dismod php7.4

# Enable new PHP module
sudo a2enmod php8.3

# Restart Apache
sudo systemctl restart apache2

If you’re using Nginx with PHP-FPM:

# Stop old PHP-FPM
sudo systemctl stop php7.4-fpm

# Install and start new PHP-FPM
sudo apt install php8.3-fpm
sudo systemctl start php8.3-fpm
sudo systemctl enable php8.3-fpm

# Update your Nginx configuration to use the new socket
# (typically in /etc/nginx/sites-available/your-site)
# Change: fastcgi_pass unix:/run/php/php7.4-fpm.sock;
# To: fastcgi_pass unix:/run/php/php8.3-fpm.sock;

sudo systemctl restart nginx

Step 5: Verify the Installation

php -v

This should show PHP 8.3.x (or whatever the latest version is).

Optional: Remove Old PHP Version

Once everything is working correctly:

sudo apt remove php7.4*
sudo apt autoremove

The Ondřej Surý repository is well-maintained and widely used in the PHP community, making it the standard solution for getting newer PHP versions on older Ubuntu releases.

Hi Deeraj,

On Ubuntu 20.04 the default package repositories don’t include newer PHP versions. You’ll need to add Ondřej Surý’s PPA, which is the common way to get PHP 8.x on Ubuntu. For example:

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php8.1

After that you should be able to install and switch between PHP versions. If you want the absolute latest stable release, check which versions are available with:

apt-cache search php8

If you run into issues with multiple versions, the update-alternatives command can help manage them.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.