You are switching from one PHP version to another, and the build cannot be completed successfully.
This guide will look into the primary causes of these errors and how to solve them.
Low on RAM
1.5 GB of RAM is required when compiling PHP, especially PHP 7.x.x and PHP 8.x.x. You can check the RAM available by running this command in your VPS (or CWP) terminal:
free -h

The recommended swap memory is 2 GB, to prevent Out Of Memory incidences.
Outdated OS
There are OS that have reached End Of Life (EOL). CentOs 6 and 8 are in this period.
Many repositories are disabled in such OS and you may experience the non-availability of packages and dependencies required to build the PHP.
Kindly ensure you are using any of the following OS:
Centos 8 stream
AlmaLinux 8
Rocky Linux 8
Oracle Linux 7 and 8
Resolver Issue
The resolver may not be set correctly, and the packages cannot be downloaded due to misconfiguration.
Edit the /etc/resolv.conf file
nano /etc/resolv.conf
and add the following line:
nameserver 1.1.1.1
nameserver 8.8.8.8
Then reboot server
reboot
Ensure to check logs to get a detailed reason for the build errors.