In this article, I will show you how to install PHP in Ubuntu operating system .
PHP is a server-side scripting language that is primarily used to build web applications, which can range from a very simple blog website to a full-fledged e-commerce site.
In fact, PHP is one of the most popular web languages ​​for doing web development.
Prerequisites for installing PHP in Ubuntu
Before we move forward and install PHP , let’s look at the following things:
I assume you know how to use the Linux terminal to run commands. In fact, a basic knowledge of the Linux shell is enough. You don’t need to be an expert!
Generally speaking, the PHP installation process remains the same across different Ubuntu versions.
How to Install PHP 7 in Ubuntu
Now let’s get to the heart of the matter.
We will walk through the steps required to install PHP. Specifically, we will install the latest stable version of PHP, which is supported for Ubuntu installation available on your system.
Update Ubuntu Packages
For Linux and Unix based systems, it is always better to update the list of available packages before installing or upgrading any software. It makes sure that you get the latest version of the software. So I recommend you to run the apt-get update
and commands apt-get upgrade
.
So, before we go ahead and install PHP, let’s run the following commands in your terminal.
$apt-get update && apt-get upgrade
The command apt-get update
should update the list of available packages and their versions. On the other hand, the command apt-get upgrade
installs and updates the necessary packages.
After successfully running the above commands, we are ready to move on to the next section.
Installing PHP on Ubuntu
As for installing PHP, it only takes one command: apt-get install php
.
In fact, if you want to make sure which packages will be installed before installing it, you can add the option --dry-run
with the command.
First, let’s do a dry run instead of actually installing anything.
$
sudo
apt-get
install
--dry-run php
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
php7.2
The following NEW packages will be installed:
php php7.2
0 upgraded, 2 newly installed, 0 to remove and 230 not upgraded.
Inst php7.2 (7.2.24-0ubuntu0.18.04.4 Ubuntu:18.04
/bionic-updates
, Ubuntu:18.04
/bionic-security
[all])
Inst php (1:7.2+60ubuntu1 Ubuntu:18.04
/bionic
[all])
Conf php7.2 (7.2.24-0ubuntu0.18.04.4 Ubuntu:18.04
/bionic-updates
, Ubuntu:18.04
/bionic-security
[all])
Conf php (1:7.2+60ubuntu1 Ubuntu:18.04
/bionic
[all])
As you can see, this displays a complete list of packages that will be installed, upgraded, and removed. In fact, I recommend you to give it a try before installing any software, as it provides some useful information.
Finally, let’s go ahead and install PHP:
$sudo
apt-get
install
php
And with that, apt-get
should install PHP and other necessary packages in your system. Let’s quickly check the PHP version that was just installed with the following command:
$php -
v
PHP 7.2.24-0ubuntu0.18.04.3 (cli) (built: Feb 11 2020 15:55:52) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.24-0ubuntu0.18.04.3, Copyright (c) 1999-2018, by Zend Technologies
As you can see, this displays the PHP version along with other useful information. You should see similar output if PHP is successfully installed.
In the next section, I will show you how to install PHP extensions.
How to install PHP extensions
In the previous section we installed PHP, but you may also need to install PHP extensions separately.
You can use the following command to list all PHP extensions.
$apt-cache search --names-only ^php
And if you are looking for extensions specifically supported for PHP 7.2 version, you can use the following command:
$apt-cache search --names-only ^php |
grep
"php7.2"
Once you have a list of PHP extensions you want to install, you just need to use the apt-get install
command to install them. For example, the following command installs the php7.2-odbc
.
$apt-get
install
php7.2-odbc
This installs the extension php-odbc
and upgrades all necessary packages with it.
In the next section we will see how you can install a specific version of PHP.
Conclusion
In this article, we have followed the steps required to install PHP in Ubuntu operating system.
If you have any questions, please feel free to use the comment form.