How to Install PHP (8.1, 8.0, 7.4) on Ubuntu 22.04 and Set Up a Local Development Environment?

PHP is a widely used server-side scripting language known for its ability to create dynamic and interactive web pages. To start learning to program, the first step is to set up and install the programming language of your choice.

In this tutorial, you will be guided through installing PHP 8.1 on Ubuntu and creating a local programming environment through the command line. You will also install a popular dependency manager, Composer, and test your installation by running a script.

Setting Up PHP

1. Update package list:

First, update the package list using the following command in the terminal:

sudo apt update

2. Install PHP

To install PHP on Ubuntu 22.04, you can use the command:

sudo apt install php8.1

Note: To install PHP 8.0 or 7.4, replace “php8.1” with “php8.0” or “php7.4” in the above command.

This will install PHP and other recommended packages, including Apache web server. If you don’t need Apache or other related packages, you can use the following command instead:

sudo apt install --no-install-recommends php8.1

3. Check PHP version

You can retrieve information about the version of PHP installed on your system by running the following command:

php -v

The output will look something like this:


4. Install Additional PHP Modules

To install additional PHP modules, use the command below and replace PACKAGE_NAME with the name of the package you want to install:

sudo apt-get install php8.1-PACKAGE_NAME

You can also install multiple packages simultaneously. Below are some common PHP modules that you might need:

sudo apt-get install -y php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath

This command will install the following modules:

  • php8.1-cli - command interpreter, useful for testing PHP scripts from a shell or performing general shell scripting tasks
  • php8.1-common - documentation, examples, and common modules for PHP
  • php8.1-mysql - for working with MySQL databases
  • php8.1-zip - for working with compressed files
  • php8.1-gd - for working with images
  • php8.1-mbstring - used to manage non-ASCII strings
  • php8.1-curl - lets you make HTTP requests in PHP
  • php8.1-xml - for working with XML data
  • php8.1-bcmath - used when working with precision floats

The following command will display a list of all PHP modules currently loaded:

php -m

Set Up a Local Development Environment

You will need a web server and a database to set up a local development environment. The Apache web server and the MySQL database server are popular choices. You can install them using the following commands:

1. Install the Apache by running the following command:

sudo apt install apache2

For configuring Apache, you can refer to this tutorial:

2. Install the MySQL server by running the following command:

sudo apt install mysql-server

For configuring MySQL, you can refer to this tutorial:
How to Install MySQL on Ubuntu

3. Configuring Composer to Manage Dependencies (Optional)
You can follow this tutorial to set up and configure composer on your Ubuntu system:
How to Install Composer on Ubuntu: A Step-by-Step Guide

Testing the PHP Environment

To verify that PHP is working correctly, create a basic PHP script named hello.php using the following command:

vi hello.php

Inside the file, enter the following text:

    echo 'Hello World!';

To save and exit the file in the vi editor, press the ESC key to ensure you’re in command mode. Then type :wq and press ENTER. This command will save the changes and exit the editor.

Next, process the script using PHP by typing the following command:

php hello.php

If PHP is correctly installed, you should see the output “Hello World!” on your terminal.

Congratulations, your PHP environment is ready for programming.