Ubuntu, Development

Ubuntu switching php versions

Most applications I work with using PHP 7.2 but I also have legacy projects that need PHP 5.6 still, so I'll install 5.6 alongside 7.2 and then set which one I want to use on the command line.

This post assumes PHP 7.2 is already installed

sudo apt install php7.2

To install 5.6 first run this command

sudo add-apt-repository ppa:ondrej/php

Now to install 5.6

sudo apt install php5.6

Now you should have 7.2 and 5.6 installed, to list all PHP versions installed:

sudo update-alternatives --config php

This menu lets you set which version to use:

sudo update-alternatives --config php

Which shows:

There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status

------------------------------------------------------------

 * 0            /usr/bin/php7.2   72        auto mode

  1            /usr/bin/php5.6   56        manual mode

  2            /usr/bin/php7.2   72        manual mode



Press <enter> to keep the current choice[*], or type selection number: 

In the above code, I have 7.2 as my active version to switch I would type 1 and press enter. Now checking the PHP version:

php -v

Returns:

PHP 5.6.38-1+ubuntu18.04.1+deb.sury.org+2 (cli)

Copyright (c) 1997-2016 The PHP Group

Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

Now I can easily switch back and forth.

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.