Skip to main content

Upgrade from PHP 7.2 to 7.4

 

Today, I sat down today to finally try out Drupal 9 and upon install realized I need to upgrade my php version from php 7.2 to at least php 7.3

    - drupal/core 9.0.2 requires php >=7.3 -> your PHP version (7.2.24) does not satisfy that requirement.

Alright here we go. 

A quick search suggests upgrading to php  7.4 requires some pretty simple steps. 

php -v
sudo apt -y install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get upgrade php
sudo sudo apt -y install php7.4
php -v

However there is more work to do if you want you projects working the same way on php 7.4 as they did on php 7.2

Install your php extensions

The command below will install some my commonly used php-extensions for my Drupal projects.

sudo apt install php7.4-common php7.4-mysqli php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd
php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap
php7.4-zip php7.4-intl -y

Be sure to compare your php 7.2 extensions directory to your php 7.4 extensions directory so everything will work the same. Your current extension can generally be found in this directory

/etc/php/7.2/mods-available

Configure PHP  FPM Pools

Upgrading php is effectively installing a newer version and then having the server use the new version. You will need to recreate your php 7.2 pools in your php 7.4 fpm directory.

I first like to back up how default php 7.4 fpm folder and then copy the contents from php 7.2. I can resolve any syntax errors due to versioning once I restart php

mv /etc/php/7.4/fpm /etc/php/7.4/fpm.7.4.bk


cp -r /etc/php/7.2/fpm /etc/php/7.4
//enable php 7.4 module
a2enconf php7.4-fpm

// restart apache2
systemctl reload apache2

// stop php7.2 fpm pool
service php7.2 fpm stop

// start php 7.4 fpm pool
service php7.4-fpm start

Troubleshooting

The above steps are if everything has gone a planned. When I sat down this morning I thought I'd pop on my server, use composer to install a Drupal project and enjoy writing a post about Drupal 9.

At the time I wrote this post my server was running

  • Ubuntu 19.04
  • PHP 7.2.24
  • Drupal 8.9.2

 

After composer informed me I needed at least php 7.3 to use Drupal 9, I immediately checked the version installed by apt.

$ apt list php* --installed

Listing... Done
php-common/disco,now 2:69ubuntu1 all [installed,automatic]
php-fpm/disco,now 2:7.2+69ubuntu1 all [installed]
php7.2-cli/disco-updates,disco-security,now 7.2.24-0ubuntu0.19.04.2 amd64 [installed,automatic]
php7.2-common/disco-updates,disco-security,now 7.2.24-0ubuntu0.19.04.2 amd64 [installed,automatic]
php7.2-curl/disco-updates,disco-security,now 7.2.24-0ubuntu0.19.04.2 amd64 [installed]
php7.2-fpm/disco-updates,disco-security,now 7.2.24-0ubuntu0.19.04.2 amd64 [installed,automatic]
php7.2-gd/disco-updates,disco-security,now 7.2.24-0ubuntu0.19.04.2 amd64 [installed]
php7.2-json/disco-updates,disco-security,now 7.2.24-0ubuntu0.19.04.2 amd64 [installed,automatic]
php7.2-mbstring/disco-updates,disco-security,now 7.2.24-0ubuntu0.19.04.2 amd64 [installed]
php7.2-mysql/disco-updates,disco-security,now 7.2.24-0ubuntu0.19.04.2 amd64 [installed]
php7.2-opcache/disco-updates,disco-security,now 7.2.24-0ubuntu0.19.04.2 amd64 [installed,automatic]
php7.2-readline/disco-updates,disco-security,now 7.2.24-0ubuntu0.19.04.2 amd64 [installed,automatic]
php7.2-xml/disco-updates,disco-security,now 7.2.24-0ubuntu0.19.04.2 amd64 [installed]
php7.2-zip/disco-updates,disco-security,now 7.2.24-0ubuntu0.19.04.2 amd64 [installed]
php7.2/disco-updates,disco-security,now 7.2.24-0ubuntu0.19.04.2 all [installed,automatic]
php/disco,now 2:7.2+69ubuntu1 all [installed]

I was hopeful I could just check for a newer version in my repositories and have apt install php 7.3 or 7.4

You can check for different packages versions with the following command.

$ apt-cache policy php

or 

$ apt-cache policy php7*

Php 7.3 nor 7.4 is not available to apt in the repositories I have enabled.

So I thought I'd just add a repository that has php 7.3 or 7.4 and no problem.

I followed the recommended step to add the repo that contained the packages I needed.

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

But the newer versions of php were not available even with this new repo.

The culprit ended up being that my Ubuntu version had reached end of life (EOL). Because it was EOL its default repos were no longer good, and I had to get my Ubuntu instance up to date before I could even attempt to add a newer version of PHP. 


So let me save you some time. If you are getting stuck trying to upgrade your php version, check your current version of Ubuntu and update that first.

Or if you are like me, and let your Ubuntu version reach end of life, you will have a lot of work to do to update your Ubuntu version before moving on.

I wrote it down in case it can help someone else.

Get you Ubuntu up to date then the steps at the top should work like a charm.