How to install PHP on Ubuntu Linux

PHP is one of the most used languages when it comes to programming dynamic web sites. If you are a web developer, or just hosting a website that requires PHP, you will need to install the software on your server in order for your website to make use of its PHP code.

PHP is also required by various content management systems, including the most popular one in the world, WordPress. In this tutorial, we will take you through the step by step instructions to install PHP on Ubuntu Linux.

In this tutorial you will learn:

  • How to install PHP on Ubuntu Linux
  • How to verify proper installation of PHP
  • How to install additional PHP modules
How to install PHP on Ubuntu Linux
How to install PHP on Ubuntu Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu Linux
Software PHP
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to install PHP on Ubuntu Linux step by step instructions



  1. Start by opening a command line terminal on your Ubuntu system and typing the following commands to install PHP from your system’s package manager.
    $ sudo apt update
    $ sudo apt install php
    

    This will install the latest PHP package available from Ubuntu’s software repository, along with some additional, related packages in most cases. If you do not already have a web server installed, Apache will also be installed with PHP, as indicated in our screenshot below.

    Using apt to install PHP and all of its related software
    Using apt to install PHP and all of its related software
  2. After PHP has been installed, you can use the following command to verify that it works properly.
    $ php -version
    
  3. A better way to ensure that PHP works on your web server is to use the phpinfo function, but inside of a php file. The perk of this method is that all the information will be formatted nicely for us and viewable inside a browser. We have a full guide on how to create a phpinfo.php page, but it basically just involves saving a PHP file with the following line of code and then accessing it from your browser.
    
    
    phpinfo PHP page accessed from web browser
    phpinfo PHP page accessed from web browser
  4. Note that you may need to install additional PHP modules later. Some are required by WordPress, such as those below:
    $ sudo apt install php-mysql php-curl php-xml php-mbstring php-imagick php-zip php-gd
    

Closing Thoughts




In this tutorial, we saw how to install PHP on Ubuntu Linux. We also learned how to verify proper installation of PHP by accessing a test page in web browser, and how to install additional modules that are used in some content management systems. This should be everything you need in order to get started with developing PHP websites or hosting them on your server.