Today, I’m going to show you how to install PHP in the Ubuntu OS. First, I’ll show you how to install the default stable version of PHP, and then I’ll show you how you can install other versions of PHP.
PHP is a server-side scripting language which is mostly used to build web applications—these may range from a very simple blog website to a full-fledged eCommerce store. In fact, PHP is one of the most popular server-side scripting languages for web development.
Learn PHP for Free
If you want to learn PHP, check out our free online course on PHP fundamentals!
In this course, you’ll learn the fundamentals of PHP programming. You’ll start with the basics, learning how PHP works and writing simple PHP loops and functions. Then you’ll build up to coding classes for simple object-oriented programming (OOP). Along the way, you’ll learn all the most important skills for writing apps for the web: you’ll get a chance to practice responding to GET and POST requests, parsing JSON, authenticating users, and using a MySQL database.
In the next section, we’ll go through the prerequisites for installing PHP in your Ubuntu installation.
Before we go ahead and install PHP, let’s make sure of the following things.
I’m assuming that you know how to use the Linux terminal to run commands. In fact, some basic knowledge of the Linux shell is enough—you don’t have to be an expert!
To install software packages, either you need to have root access or you need to have a user with
sudo access. Throughout this tutorial, I’m going to use the root user to install PHP and other necessary packages. Of course, there’s nothing stopping you from using a user with
sudo rights instead.
Generally speaking, the process of installing PHP remains the same in different versions of Ubuntu. Unless you specify, the
install command will try to find the default stable version of PHP for your Ubuntu installation and install that. But, if you want to install a specific version of PHP, you may need to add related packages. In the latter half of the article, we’ll go through an example to demonstrate how you could install the PHP 7.4 version.
In the first half of the article, we’ll see how you could install the
default stable version of PHP in Ubuntu. Next, we’ll discuss how to
install the PHP 7.4 version in Ubuntu. In fact, PHP has several
different versions, and if you want to install some other version of PHP,
drop me a line in the comments section and I’ll be happy to help.
With that out of our way, we’re ready to move on to the next section!
How to Install PHP 7 in Ubuntu
In this section, we’ll go through the necessary steps to install PHP. Specifically, we’ll install the latest stable version of PHP, which is supported for the Ubuntu installation available on your system.
Update Ubuntu Packages
For *nix-based systems—Linux and Unix—it’s always best to update the list of available packages before we install or upgrade any software. It makes sure that we get the latest version of the software. So I recommend that you run the
apt-get update and
apt-get upgrade commands.
Thus, before we go ahead and install PHP, let’s run the following commands in your terminal.
$apt-get update && apt-get upgrade
apt-get update command should update the list of available packages along with their versions. On the other hand, the
apt-get upgrade command actually installs and updates the necessary packages.
After running the above commands successfully, we’re ready to move on to the next section.
Let’s Install PHP
When it comes to installing PHP, it takes only a single command:
apt-get install php. In fact, if you want to make sure which packages will be installed before actually installing it, you can add the
--dry-run option along with the command.
Firstly, 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 do a dry run before installing any software, as it provides 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 verify the PHP version which is 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 installed successfully.
In the next section, I’ll show you how to install PHP extensions.
How to Install PHP Extensions
In the previous section, we installed PHP, but you might also need to install PHP extensions separately.
You can use the following command to list all the PHP extensions.
$apt-cache search --names-only ^php
And if you’re looking for extensions that are specifically supported for the 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 that you want to install, you just need to use the
apt-get install command to install them. For example, the following command installs the
$apt-get install php7.2-odbc
This installs the
php-odbc extension and upgrades any necessary packages along with it.
In the next section, we’ll discuss how you could install a specific version of PHP.
How to Install PHP 7.4 in Ubuntu
In this section, we’ll go through how you could install PHP 7.4 in Ubuntu, which is the latest stable version of PHP.
Update Ubuntu Packages
As we discussed in the previous section, let’s run the following commands to update and upgrade the list of available packages.
$apt-get update && apt-get upgrade
Add the PHP PPA Repository
Next, we’ll need to add the
ondrej/php repository, which is a PPA repository and contains the latest build packages of PHP.
Go ahead and run the following commands that add the
ppa:ondrej/php repository and update the list of available packages along with their versions.
$apt -y install software-properties-common $add-apt-repository ppa:ondrej/php $apt-get update
And with that, we’re ready to install PHP 7.4.
Let’s Install PHP 7.4
Finally, let’s use the following command to install PHP 7.4.
$apt -y install php7.4
After running the above command successfully, you could check the PHP version with the
php -v command.
How to Install PHP 7.4 Extensions
After installing PHP 7.4, if you want to install additional extensions, you could search it using the following command.
$apt-cache search --names-only ^php | grep "php7.4"
And finally, run the
apt-get install command to install extensions that you want to install.
How to Handle Multiple PHP Versions
Sometimes, you need to manage multiple versions of PHP on the same Ubuntu server. For example, your old projects might still be running on PHP 5.x, and they might not yet be upgradable to PHP 7.x. On the other hand, if you’re working on a new project, you might want the latest stable version of PHP 7.x. In this scenario, you have to install two different versions of PHP on your server.
There are different ways you could configure the specific version of PHP to be used with Apache for Nginx web servers. Or you could also use the
update-alternatives command to set the default version of PHP which will be used if you have multiple versions of PHP.
Let’s assume that you’ve installed two different versions of PHP: PHP 7.2 and PHP 7.4. If you want to set the PHP 7.4 version to be the default version, you could run the following command.
$update-alternatives --set php /usr/bin/php7.4
In this way, you can switch between different versions of PHP for development purposes.
In this article, we went through the necessary steps that are required to install PHP in the Ubuntu OS. Along with that, I also took an opportunity to show how to install the specific version of PHP.
The Best PHP Scripts on CodeCanyon
For complete applications that you can use and customize, take a look the professional PHP scripts on CodeCanyon.
Explore thousands of the best and most useful PHP scripts ever created on CodeCanyon.
Here are a few of the best-selling and up-and-coming PHP scripts available on CodeCanyon for 2020.