PHP is a popular server-side scripting language that is used to create dynamic web pages and web applications. It is open-source and is widely used for developing web applications, content management systems, and e-commerce websites. In this article, we will discuss how to install PHP 8.2, 7.4, and 5.6 on Ubuntu 18.04.

Advertisement

Ubuntu 18.04 comes with PHP 7.2 installed by default. However, if you need to use an older or a newer version of PHP, you can follow the instructions below.

Note: Before installing a new version of PHP, make sure to backup any files that may be affected by the installation process.

Step 1: Add the Ondřej Surý PPA

The Ondřej Surý PPA is a Personal Package Archive that contains the latest versions of PHP. To add this PPA to your system, run the following command in your terminal:

sudo add-apt-repository ppa:ondrej/php 

After adding the PPA, update your package list with the following command:

sudo apt-get update 

Step 2: Installing PHP

Once you have added the PPA to your Ubuntu system, You can install required PHP version on your system. Even you can install multiple PHP versions at the same time.

  • Installing PHP 8.2:
    sudo apt-get install -y php8.2 
    
  • Installing PHP 7.4:
    sudo apt-get install -y php7.4 
    
  • Installing PHP 5.6:
    sudo apt-get install -y php5.6 
    

Step 3: Verify PHP Version

After the installation process is complete, you can check the PHP version by running the following command:

php -v 
PHP 8.2.3 (cli) (built: Feb 14 2023 16:58:12) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.3, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.3, Copyright (c), by Zend Technologies

Step 4: Change Default PHP Version

In Ubuntu, multiple versions of PHP can be installed and used side-by-side. However, only one version can be set as the default at a time. If you have multiple versions of PHP installed and you want to change the default PHP version, follow the steps below:

  1. Check the available PHP versions by running the following command:
    ls /usr/bin/php* 
    

    This command will list all the available PHP versions installed on your system.

  2. To change the default PHP version, you can use the update-alternatives command. For example, if you want to set PHP 7.4 as the default version, run the following command:
    sudo update-alternatives --set php /usr/bin/php7.4 
    

    This command sets the /usr/bin/php7.4 binary as the default php command.

  3. If you have installed php-dev package, then you also need to set phpize and php-config as default.
    sudo update-alternatives --set phpize /usr/bin/phpize7.4 
    sudo update-alternatives --set php-config /usr/bin/php-config7.4 
    
  4. To verify the default PHP version, run the following command:
    php -v 
    

    This command should display the version of PHP that you just set as the default.

By default, only one version of PHP can be set as the default, but multiple versions can be installed and used side-by-side. Remember to verify the default PHP version after making any changes.

Conclusion

In this article, we have discussed how to install PHP 8.2, 7.4, and 5.6 on Ubuntu 18.04. You can choose the version of PHP that you need for your project, depending on its requirements. Keep in mind that newer versions of PHP may not be compatible with older applications, so it’s always a good idea to check compatibility before upgrading. Additionally, remember to backup any files that may be affected by the installation process.

Share.

42 Comments

  1. I’ve been using these instructions to install PHP8.2 on Ubuntu 18.04 (Bionic Beaver). When I attempt to install PHP it can’t find 8.2 in the repository for Bionic. Has this PHP vesrion been removed for Bionic? Thanks

  2. After thinking it didn’t work, I realised php -v tells you the CLI version. This changes the apache version, so a phpinfo command in a php script revealed it did in fact work perfectly.
    Thanks

  3. Diego Muñoz V on

    Hello Friend

    I am trying to do the installation and at the time of executing the repository command I have an error after pressing ENTER

    # LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
    More info: https://launchpad.net/~ondrej/+archive/ubuntu/php
    Press [ENTER] to continue or ctrl-c to cancel adding it

    gpg: keyring `/tmp/tmp93drbu74/secring.gpg’ created
    gpg: keyring `/tmp/tmp93drbu74/pubring.gpg’ created
    gpg: requesting key E5267A6C from hkp server keyserver.ubuntu.com
    gpg: /tmp/tmp93drbu74/trustdb.gpg: trustdb created
    gpg: key E5267A6C: public key “Launchpad PPA for Ond\xc5\x99ej Sur?” imported
    gpg: Total number processed: 1
    gpg: imported: 1 (RSA: 1)
    Exception in thread Thread-1:
    Traceback (most recent call last):
    File “/usr/lib/python3.4/threading.py”, line 920, in _bootstrap_inner
    self.run()
    File “/usr/lib/python3.4/threading.py”, line 868, in run
    self._target(*self._args, **self._kwargs)
    File “/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py”, line 687, in addkey_func
    func(**kwargs)
    File “/usr/lib/python3/dist-packages/softwareproperties/ppa.py”, line 370, in add_key
    return apsk.add_ppa_signing_key()
    File “/usr/lib/python3/dist-packages/softwareproperties/ppa.py”, line 261, in add_ppa_signing_key
    tmp_export_keyring, signing_key_fingerprint, tmp_keyring_dir):
    File “/usr/lib/python3/dist-packages/softwareproperties/ppa.py”, line 210, in _verify_fingerprint
    got_fingerprints = self._get_fingerprints(keyring, keyring_dir)
    File “/usr/lib/python3/dist-packages/softwareproperties/ppa.py”, line 202, in _get_fingerprints
    output = subprocess.check_output(cmd, universal_newlines=True)
    File “/usr/lib/python3.4/subprocess.py”, line 609, in check_output
    output, unused_err = process.communicate(inputdata, timeout=timeout)
    File “/usr/lib/python3.4/subprocess.py”, line 947, in communicate
    stdout = _eintr_retry_call(self.stdout.read)
    File “/usr/lib/python3.4/subprocess.py”, line 491, in _eintr_retry_call
    return func(*args)
    File “/usr/lib/python3.4/encodings/ascii.py”, line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
    UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xc5 in position 92: ordinal not in range(128)

    • Diego Muñoz V on

      Hello Friend
      I was able to solve the execution and update, but when installing 5.6 it can’t find it …

      I have Ubuntu 14.04

      root@idp1:~# sudo apt-get install -y php5.6
      Reading package lists… Done
      Building dependency tree
      Reading state information… Done
      E: Unable to locate package php5.6
      E: Couldn’t find any package by regex ‘php5.6’

  4. Even the first line doesn’t work on Ubuntu 14.04:
    > sudo apt-get install -y python-software-properties
    > E: Unable to locate package python-software-properties

  5. Hi Author,

    I followed your instrunctions for installing php 7.1 for ubuntu server (Ubuntu 12.04.5 LTS)
    I am getting this output (after step :sudo apt-get install -y php7.1)
    Building dependency tree
    Reading state information… Done
    E: Unable to locate package php7.1
    E: Couldn’t find any package by regex ‘php7.1’

    Adding here couple of results than could help pinpoint the issue I have

    #lsb_release -a
    LSB Version: core-2.0-amd64:core-2.0-noarch:core-3.0-amd64:core-3.0-noarch:core-3.1-amd64:core-3.1-noarch:core-3.2-amd64:core-3.2-noarch:core-4.0-amd64:core-4.0-noarch
    Distributor ID: Ubuntu
    Description: Ubuntu 12.04.5 LTS
    Release: 12.04
    Codename: precise
    #apt-cache policy php5-cli

    Any suggestion for fixing it.

    Thanks in advance!

  6. Cannot add PPA: ‘ppa:~ondrej/ubuntu/php’.
    ERROR: ‘~ondrej’ user or team does not exist.

    i cannot solved this problem

    Please help Me

    • me too 🙁

      root@ubuntuserver:~# add-apt-repository -y ppa:ondrej/php
      Cannot add PPA: ‘ppa:~ondrej/ubuntu/php’.
      ERROR: ‘~ondrej’ user or team does not exist.
      root@ubuntuserver:~#

        • My OS Version is:

          root@ubuntuserver:~# lsb_release -a
          No LSB modules are available.
          Distributor ID: Ubuntu
          Description: Ubuntu 16.04.2 LTS
          Release: 16.04
          Codename: xenial
          root@ubuntuserver:~#

          any suggest?…Thx before :),,

  7. Peter David Carter on

    This doesn’t appear to work anymore.

    peter@peter-VirtualBox:~/upm$ sudo apt-get install -y php5.6
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    E: Unable to locate package php5.6
    E: Couldn’t find any package by regex ‘php5.6’

    • Peter,
      Which OS are you using ? Its working for me.

      rahul@tecadmin:~$ sudo apt install php5.6
      Reading package lists… Done
      Building dependency tree
      Reading state information… Done
      The following additional packages will be installed:
      apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php5.6 libapr1 libaprutil1
      libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.1-0 libssl1.0.2 php-common php5.6-cli php5.6-common
      php5.6-json php5.6-opcache php5.6-readline
      Suggested packages:
      apache2-doc apache2-suexec-pristine | apache2-suexec-custom php-pear
      The following NEW packages will be installed:
      apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php5.6 libapr1 libaprutil1
      libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.1-0 libssl1.0.2 php-common php5.6 php5.6-cli
      php5.6-common php5.6-json php5.6-opcache php5.6-readline
      0 upgraded, 18 newly installed, 0 to remove and 415 not upgraded.
      Need to get 8,458 kB of archives.
      After this operation, 26.0 MB of additional disk space will be used.
      Do you want to continue? [Y/n]

    • Jeroen Landheer on

      Remember to run apt-get update after you added the PPA, otherwise your system won’t be able to find it.

  8. Hello,
    I tried installing php5.6 on Ubuntu 16.04, I followed the guide but instead of php5.6 php7.0.8 has been installed.

    Did anyone have the same problem?

    Cheers

    • Hi phill,

      Actually Ubuntu 16.04 have php 7 in its default repositories. That’s why it may install with packages dependencies. Please make sure that you use following syntax with php installation, like below

      $ apt install php5.6 php5.6-mysql

      • HI rahul,
        i also have using ubuntu 16.04 xenial..
        No LSB modules are available.
        Distributor ID: Ubuntu
        Description: Ubuntu 16.04.7 LTS
        Release: 16.04
        Codename: xenial
        but still am facing these issues

        E: Unable to locate package php7.2
        E: Couldn’t find any package by glob ‘php7.2’
        E: Couldn’t find any package by regex ‘php7.2’

    • If you want to toggle between versions of php you can do so with the following commands:

      i. from 7.0 to 5.6 : sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart
      ii. from 7.0 to 5.6 : sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart

  9. Please help me , I need php 5.4 the script that I use can only work with php 5.4.

    On Ubuntu 16,04 I follow this guide , but I get php5 not found ? can not install it.

    Thank you 🙂

  10. Note that for php 5.5 for an ubuntu after 14.04, you have to manually update the sources.list given by the ppa to change your current ubuntu name to trusty, then you will be able to install the php5* packages

  11. How can I remove php5 package which is loaded from ubuntu repository? For example if I add ondrej ppa, I always install ubuntu php5. When I type command apt-cache policy php5 I can see that candidate foo installation is ubuntu version not third party which I added..

  12. Dear Author,

    Could you please recheck for PHP 5.4? Because I’m using Ubuntu 15.04 and no way to do this. Below are output when I entered these commands:

    W: Failed to fetch 404 Not Found
    W: Failed to fetch 404 Not Found [IP: x.x.x.x 80]

    So, I don’t complete like as your article. Please help, thank so much!

Exit mobile version