Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to install PHP

How to install PHP on Ubuntu?

Aman Jain, September 18, 2021October 4, 2022

PHP is widely used langauge in world of web. PHP is an open sourece language and free for everyone. It’s a server side scripting lanaguge which compiles on server and can embedded with html too.

Currently, there is three version are available of PHP that are as below

  1. PHP 5
  2. PHP 7
  3. PHP 8

In these 3 versions of PHP, PHP 7 is most stable version currently.

In this tutorial we will learn to install the PHP stable version in Ubuntu software repositories.

Let’s learn with step by step.

Step 1: Check stable version in the Ubuntu software repositories

sudo apt show php

it will show

Package: php
Version: 2:7.4+75
Priority: optional
Section: php
Source: php-defaults (75)
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian PHP Maintainers <team+pkg-php@tracker.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 13.3 kB
Depends: php7.4
Download-Size: 2712 B
APT-Manual-Installed: yes
APT-Sources: http://ap-south-1.ec2.archive.ubuntu.com/ubuntu focal/main amd64 Packages
Description: server-side, HTML-embedded scripting language (default)
 PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
 open source general-purpose scripting language that is especially suited
 for web development and can be embedded into HTML.
 .
 This package is a dependency package, which depends on latest stable
 PHP version (currently 7.4).

Step 2: Install PHP default version

sudo apt install php

//or if we want some packages also

sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-json php-zip php-mbstring


Step 3: Check PHP versions in CLI

php -v

It will show the output as below :

PHP 7.3.1 (cli) (built: Jul  5 2021 15:13:35) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.3.1, Copyright (c), by Zend Technologies

Step 4: Check in browser

Go to folder /var/www/html and create a file info.php

cd /var/www/html

Then type command to edit the or create a new file

sudo vi info.php

And type


<?php 
echo phpinfo();

Then, open http://ip_domain/info.php

how to Install different version of PHP?

Sometimes it’s required to install the different version of PHP rather then the default one of Ubuntu Repository System.

To install the different version we will follow the basic steps before installation of PHP

Step 1 : Add ppa:ondrej/php to Ubuntu system

Open the terminal or ssh connection and then type

//for <= ubunt 12.04
sudo apt install python-software-properties 
//for >12.04
sudo apt install software-properties-common

Then install ppa:ondrej

sudo add-apt-repository ppa:ondrej/php

Output:

Co-installable PHP versions: PHP 5.6, PHP 7.x and most requested extensions are included. Only Supported Versions of PHP (http://php.net/supported-versions.php) for Supported Ubuntu Releases (https://wiki.ubuntu.com/Releases) are provided. Don't ask for end-of-life PHP versions or Ubuntu release, they won't be provided.

Debian oldstable and stable packages are provided as well: https://deb.sury.org/#debian-dpa

You can get more information about the packages at https://deb.sury.org

IMPORTANT: The <foo>-backports is now required on older Ubuntu releases.

BUGS&FEATURES: This PPA now has a issue tracker:
https://deb.sury.org/#bug-reporting

CAVEATS:
1. If you are using php-gearman, you need to add ppa:ondrej/pkg-gearman
2. If you are using apache2, you are advised to add ppa:ondrej/apache2
3. If you are using nginx, you are advised to add ppa:ondrej/nginx-mainline
   or ppa:ondrej/nginx

Step 2 : Update the system

 sudo apt-get update

Step 3 : Check php versions available

sudo apt show php

Output :

Package: php
Version: 2:8.0+84+ubuntu20.04.1+deb.sury.org+1
Priority: optional
Section: php
Source: php-defaults (84+ubuntu20.04.1+deb.sury.org+1)
Maintainer: Debian PHP Maintainers <team+pkg-php@tracker.debian.org>
Installed-Size: 13.3 kB
Depends: php8.0
Download-Size: 6960 B
APT-Sources: http://ppa.launchpad.net/ondrej/php/ubuntu focal/main amd64 Packages
Description: server-side, HTML-embedded scripting language (default)
 PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
 open source general-purpose scripting language that is especially suited
 for web development and can be embedded into HTML.
 .
 This package is a dependency package, which depends on latest stable
 PHP version (currently 8.0).

Package: php
Version: 2:7.4+75
Priority: optional
Section: php
Source: php-defaults (75)
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian PHP Maintainers <team+pkg-php@tracker.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 13.3 kB
Depends: php7.4
Download-Size: 2712 B
APT-Manual-Installed: yes
APT-Sources: http://ap-south-1.ec2.archive.ubuntu.com/ubuntu focal/main amd64 Packages
Description: server-side, HTML-embedded scripting language (default)
 PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
 open source general-purpose scripting language that is especially suited
 for web development and can be embedded into HTML.
 .
 This package is a dependency package, which depends on latest stable
 PHP version (currently 7.4).

We can now install php 8.

Step 3 : Install different version of PHP

For apache you can now install any version

$ sudo apt install php5.6   [PHP 5.6]
$ sudo apt install php7.0   [PHP 7.0]
$ sudo apt install php7.1   [PHP 7.1]
$ sudo apt install php7.2   [PHP 7.2]
$ sudo apt install php7.3   [PHP 7.3]
$ sudo apt install php7.4   [PHP 7.4]
$ sudo apt install php8.0   [PHP 8.0]

For Nginx

$ sudo apt install php5.6-fpm   [PHP 5.6]
$ sudo apt install php7.0-fpm   [PHP 7.0]
$ sudo apt install php7.1-fpm   [PHP 7.1]
$ sudo apt install php7.2-fpm   [PHP 7.2]
$ sudo apt install php7.3-fpm   [PHP 7.3]
$ sudo apt install php7.4-fpm   [PHP 7.4]
$ sudo apt install php8.0-fpm   [PHP 8.0]

In this way you can install any version.

Install modules for any version

Simply type the version you want to install then type tab to show available module for specific module.

Example:

$ sudo apt install php7.0
php7.0                 php7.0-inotify         php7.0-readline
php7.0-amqp            php7.0-interbase       php7.0-recode
php7.0-apcu            php7.0-intl            php7.0-redis
php7.0-apcu-bc         php7.0-json            php7.0-rrd
php7.0-ast             php7.0-ldap            php7.0-smbclient
php7.0-bcmath          php7.0-lua             php7.0-snmp
php7.0-bz2             php7.0-lz4             php7.0-soap
php7.0-cgi             php7.0-mailparse       php7.0-sodium
php7.0-cli             php7.0-mbstring        php7.0-solr
php7.0-common          php7.0-mcrypt          php7.0-sqlite3
php7.0-curl            php7.0-memcache        php7.0-ssh2
php7.0-dba             php7.0-memcached       php7.0-stomp
php7.0-decimal         php7.0-mongodb         php7.0-sybase
php7.0-dev             php7.0-msgpack         php7.0-tideways
php7.0-ds              php7.0-mysql           php7.0-tidy
php7.0-enchant         php7.0-oauth           php7.0-uploadprogress
php7.0-facedetect      php7.0-odbc            php7.0-uuid
php7.0-fpm             php7.0-opcache         php7.0-vips
php7.0-gd              php7.0-pgsql           php7.0-xdebug
php7.0-gearman         php7.0-phalcon3        php7.0-xhprof

Example:

sudo apt install php7.0-cli php7.0-xml php7.0-mysql 

Set Default PHP Version in Ubuntu

Now, we have installed multiple versions of php but at a time we can use one so let’s choose one PHP version

Step 1 : To check available versions of PHP

sudo update-alternatives --config php

Output:

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

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php7.4   74        auto mode
  1            /usr/bin/php7.0   70        manual mode
  2            /usr/bin/php7.4   74        manual mode
Press <enter> to keep the current choice[*], or type selection number:

Enter the number and you have successfully changed the version of PHP.

Step 2 : Check version of php

$ php -v

Related

Devops Php Ubuntu

Post navigation

Previous post
Next post

Related Posts

Php How to use conditional where in Laravel 8

How to use conditional where clause in Laravel 8 eloquent ?

February 7, 2022October 4, 2022

In Laravel sometimes while creating a eloquent or db builder query you wanted to apply the where on basis of some conditions and to achieve it you may use if else condition but laravel 8 itself provides solution to handle such type of situations using when method. Laravel eloquent when…

Read More
Php How to run specific seeder class in laravel

How to run specific seeder class in laravel 9 ?

February 19, 2022February 22, 2022

In our last article we learnt about how to create seeder in laravel but sometimes we only need to run a specific seeders rather then running all so in this article we will cover to run a specific seeder. We will use a simple example of article class and will…

Read More
Php How to print raw sql query in eloquent laravel

How to print raw sql query in eloquent laravel ?

November 23, 2022March 16, 2024

In this article we will learn to print raw sql query in eloquent laravel. Sometime to debug the MySQL query in laravel eloquent we need to echo raw sql query and want to know exact query. In laravel we have multiple methods to print database query in laravel 8 and…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version