How to Install PHP on Ubuntu 22.04 LTS (Jammy Jellyfish)


In this article, we will see how to install PHP on Ubuntu 22.04 LTS (Jammy Jellyfish). PHP is a free and open source server side scripting language, widely used for development of web applications. It has been around for sometime and still being popularly used in generating dynamic web pages. It can be embedded in HTML and usually faster than other server side scripting languages such as JSP and ASP. It is easy to learn and use which makes it one of the favorite for developers to use more frequently.

Since it supports all the famous databases such as MySQL, PostgreSQL, MongoDB etc, it is easy to integrate in any application ecosystem. It is platform independent and can be easily installed in Windows, Linux and Unix systems. Here we will see the installation and usage of PHP in Ubuntu 22.04 LTS system. There are many ways to install PHP on Ubuntu/Debian based systems but here we will look into the most simplest way to install.


How to Install PHP on Ubuntu 22.04 LTS (Jammy Jellyfish)

How to Install PHP on Ubuntu 22.04 LTS (Jammy Jellyfish)

Also Read: How to Send Unix/Linux Executables through Microsoft Teams

Step 1: Prerequisites

a) You should have a running Ubuntu 22.04 LTS Server.

b) You should sudo or root access to run privileged commands.

c) You should have apt or apt-get utility available in your Server.


Step 2: Update Your Server

Before proceeding with the installation, you need to download all the available updates and install it by using sudo apt update && sudo apt upgrade command as shown below.

cyberithub@ubuntu:~$ sudo apt update && sudo apt upgrade
[sudo] password for cyberithub:
Get:1 stable InRelease [10.4 kB]
Hit:2 stable InRelease
Hit:3 jammy InRelease
Get:4 jammy-security InRelease [110 kB]
Get:5 jammy-updates InRelease [119 kB]
Get:6 stable/main i386 Packages [5,460 B]
Get:7 stable/main amd64 Packages [55.3 kB]
Get:8 jammy-backports InRelease [108 kB]
Get:9 jammy-security/main i386 Packages [253 kB]
Get:10 jammy-updates/main i386 Packages [437 kB]
Get:11 jammy-security/main amd64 Packages [541 kB]
Get:12 jammy-updates/main amd64 Packages [763 kB]
Get:13 jammy-security/main Translation-en [134 kB]
Get:14 jammy-security/main amd64 DEP-11 Metadata [41.6 kB]
Get:15 jammy-security/main amd64 c-n-f Metadata [10.3 kB]
Get:16 jammy-security/restricted amd64 Packages [485 kB]
Get:17 jammy-updates/main Translation-en [195 kB]
Get:18 jammy-updates/main amd64 DEP-11 Metadata [99.5 kB]


Step 3: Install PHP

In the next step, you can install PHP in your system by using sudo apt install php-cli command as shown below. This will download and install the package from default Ubuntu repo along with all its dependencies.

cyberithub@ubuntu:~$ sudo apt install php-cli
[sudo] password for cyberithub:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
php-common php8.1-cli php8.1-common php8.1-opcache php8.1-readline
Suggested packages:
The following NEW packages will be installed:
php-cli php-common php8.1-cli php8.1-common php8.1-opcache php8.1-readline
0 upgraded, 6 newly installed, 0 to remove and 3 not upgraded.
Need to get 3,354 kB of archives.
After this operation, 15.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 jammy/main amd64 php-common all 2:92ubuntu1 [12.4 kB]
Get:2 jammy-updates/main amd64 php8.1-common amd64 8.1.2-1ubuntu2.11 [1,126 kB]
Get:3 jammy-updates/main amd64 php8.1-opcache amd64 8.1.2-1ubuntu2.11 [365 kB]
Get:4 jammy-updates/main amd64 php8.1-readline amd64 8.1.2-1ubuntu2.11 [13.5 kB]
Get:5 jammy-updates/main amd64 php8.1-cli amd64 8.1.2-1ubuntu2.11 [1,833 kB]
Get:6 jammy/main amd64 php-cli all 2:8.1+92ubuntu1 [3,234 B]
Fetched 3,354 kB in 2s (1,693 kB/s)
Selecting previously unselected package php-common.
(Reading database ... 203624 files and directories currently installed.)
Preparing to unpack .../0-php-common_2%3a92ubuntu1_all.deb ...
Unpacking php-common (2:92ubuntu1) ...


Step 4: Check Version

After successful installation, you can check the current installed version by using php --version command as shown below.

cyberithub@ubuntu:~$ php --version
PHP 8.1.2-1ubuntu2.11 (cli) (built: Feb 22 2023 22:56:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.11, Copyright (c), by Zend Technologies


Step 5: Using PHP

Now that PHP is installed, it is time to write a sample script to test the utility. Here we are going to create a simple example.php script using nano example.php command as shown below.

cyberithub@ubuntu:~$ nano example.php

When you run above script using php example.php command then it will show output like below. This confirms that PHP is working fine in your System.

cyberithub@ubuntu:~$ php example.php
PHP Version => 8.1.2-1ubuntu2.13

System => Linux ubuntu 5.19.0-46-generic #47~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Jun 21 15:35:31 UTC 2 x86_64
Build Date => Jun 28 2023 14:01:49
Build System => Linux
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /etc/php/8.1/cli
Loaded Configuration File => /etc/php/8.1/cli/php.ini
Scan this dir for additional .ini files => /etc/php/8.1/cli/conf.d
Additional .ini files parsed => /etc/php/8.1/cli/conf.d/10-opcache.ini,

Leave a Comment