Wednesday, June 10, 2020

How to Install PHP in Ubuntu

How to Install PHP in Ubuntu

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 prerequisites that are required in order to install PHP in your Ubuntu installation.

Prerequisites

Before we go ahead and install PHP, let’s make sure of the following things in the first place.

It’s assumed that you know how to use the Linux terminal to run commands. In fact, 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 the course of 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 in order 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 preferred 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 it’s recommended to run 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.

The 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 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.

As you can see, this displays a complete list of packages that will be installed, upgraded and removed. In fact, I recommend you to do a dry run before installing any software, as it provides useful information.

Finally, let’s go ahead and 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:

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 out all the PHP extensions.

And if you are looking for extensions that are specifically supported for the PHP 7.2 version, you can use the following command.

Once you’ve 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 php7.2-odbc extension.

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.

Add the PHP PPA Repository

Next, we’ll need to add the ondrej/php repository, which is a PPA repository and it 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.

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.

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.

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 use to 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’ve 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.

In this way, you can switch between different versions of PHP for development purposes.

Conclusion

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 as well.

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.


No comments:

Post a Comment