A complete solution to the installation and version switching of PHP on Ubuntu

Keywords: Linux PHP sudo Ubuntu curl

The official sources on Ubuntu, such as PHP5.6.x in Ubuntu 14.04 and PHP7.0.x in Ubuntu 16.04, what should I do if I want to install PHP7.1 and PHP7.2 on Ubuntu 16.04?

The answer is to install from a third-party source, ppa:ondrej/php It is a well-known PHP source (currently maintaining php5.6,php7.0,php7.1,php7.2). Use this source to install the PHP version you want.

  1. Install PHP from source

    sudo add-apt-repository ppa:ondrej/php
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install php7.1   # for PHP 7.1
    sudo apt-get install php7.0   # for PHP 7.0
    sudo apt-get install php5.6   # for PHP 5.6
  2. Install the relevant version of PHP module

    sudo apt-cache search php7*
    ...
    php-all-dev - package depending on all supported PHP development packages
    php7.0 - server-side, HTML-embedded scripting language (metapackage)
    php7.0-cgi - server-side, HTML-embedded scripting language (CGI binary)
    php7.0-cli - command-line interpreter for the PHP scripting language
    php7.0-common - documentation, examples and common module for PHP
    php7.0-curl - CURL module for PHP
    php7.0-dev - Files for PHP7.0 module development
    php7.0-gd - GD module for PHP
    php7.0-gmp - GMP module for PHP
    ...
    ...
    ...
    php7.1-xml - DOM, SimpleXML, WDDX, XML, and XSL module for PHP
    php7.1-xmlrpc - XMLRPC-EPI module for PHP
    php7.1-zip - Zip module for PHP
    php7.1-opcache - Zend OpCache module for PHP
    php7.1 - server-side, HTML-embedded scripting language (metapackage)
    php7.1-xsl - XSL module for PHP (dummy)
    ...
    ...
    php7.2-bcmath - Bcmath module for PHP
    php7.2-bz2 - bzip2 module for PHP
    php7.2-cgi - server-side, HTML-embedded scripting language (CGI binary)
    php7.2-cli - command-line interpreter for the PHP scripting language
    php7.2-common - documentation, examples and common module for PHP
    php7.2-curl - CURL module for PHP
    php7.2-dba - DBA module for PHP
  3. Select the required module and install it

    sudo apt-get install php7.2-curl php7.2-dev php7.2-gd php7.2-imap php7.2-intl php7.2-mbstring php7.2-mysql php7.2-xml php7.2-zip
  4. Switch PHP version

    sudo update-alternatives --config php
  5. Set up Apache to run with the correct PHP version

    sudo a2dismod php7.1         # unload the current version
    sudo a2enmod  php5.6         # load the version you need
    sudo service apache2 restart # restart webserver to apply

At this end, you don't need to worry about installing PHP and switching the PHP version.

Reference resources:

Posted by RobNewYork on Fri, 03 Apr 2020 15:54:46 -0700