How to upgrade to PHP 7.1 on Ubuntu

Sun, 2016-12-04 01:07

Ubuntu 16.04 came with PHP 7.0, but at this moment, there is no PHP 7.1 available on the official repos. Fortunately, the super awesome ondrej/php repo already has PHP 7.1, so I upgraded to PHP 7.1 from there. Here is how.

0. May be not too fast!

Note that PHP 7.1 comes with some backward incompatibility changes, so make sure to check everything and have backups. I had my systems well-tested on the Docker PHP7.1-rc image, so I was only taking a tiny risk. If you are using PHP5.*, the PHP7CC package is an awesome tool to scan your code for possible PHP7 incompatibilities.

1. Add ondrej/php PPA.

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

If you get a command not found error for add-apt-repository, you can install it from:

sudo apt-get install software-properties-common python-software-properties


2. List your current PHP packages

Your current PHP core and extensions should be installed in the php7.0-FOOBAR form as of now. Most of the extensions are already available in the PPA above, but make sure that you have all the packages in their PHP7-1 version as well.

dpkg -l | grep php | tee packages.txt

Above command will list all the PHP packages you already have. Some of the packages are installed alongside php7.0-common. We are also saving the list to a text file packages.txt just in case.

3. Install PHP 7.1

Depending on how you use PHP with the web server, you will probably need to turn the web server off. After that, run the following:

sudo apt-get install php7.1 php7.1-common

This should install the basic packages on your system.
Now, make sure to install the additional packages as you need them. In my case, I also installed these packages with apt-get command.

sudo apt-get install php7.1-curl php7.1-xml php7.1-zip php7.1-gd php7.1-mysql php7.1-mbstring


4. Test if PHP version is indeed 7.1

if everything went well, you should see the PHP 7.1 in the php -v command output.



5. Remove old 7.0 packages

If everything works great, you can remove the old packages now.

sudo apt-get purge php7.0 php7.0-common

This should remove the base package and all the extensions too.

Comments

This worked perfectly :)

Thanks!

Your thoughts