In this article, we will see how to install PHP 7.4 on Ubuntu 22.04. Ubuntu 22.04 may not come with PHP 7.4 pre-installed in its default repositories. However, you can easily install PHP 7.4 on Ubuntu 22.04 by adding the PHP repository maintained by Ondřej Surý, which provides the latest PHP versions for Ubuntu users. This guide will walk you through the steps to install PHP 7.4 on Ubuntu 22.04 and configure it for your development or production environment.

Add the PHP Repository

o add the PHP repository containing PHP 7.4, open a terminal and run the following command:

sudo add-apt-repository ppa:ondrej/php

This command adds the PHP repository maintained by Ondřej Surý, which provides up-to-date PHP versions for Ubuntu systems.

Update Package Index

After adding the repository, update your system’s package index to ensure that the newly added repository is recognized and its packages are available for installation.

sudo apt update

Update Ubuntu Server 22.04

Install PHP 7.4

Now that the repository is added and the package index updated, you can proceed to install PHP 7.4 using the following command

sudo apt install php7.4

Install PHP 7.4 on Ubuntu 22.04

Install PHP Extensions (Optional)

Depending on your project requirements, you may need to install additional PHP extensions. You can search for available PHP modules using:

apt search php7.4-

Install PHP Extensions in Ubuntu 22.04

Install the necessary extensions using:

sudo apt install <extension-name>

For example, to install commonly used extensions like MySQL, XML, JSON, MBString, and GD, run the following command:

sudo apt install php7.4-common php7.4-mysql php7.4-xml php7.4-json php7.4-mbstring php7.4-gd php7.4-curl

Install PHP Extensions in Ubuntu 22.04

Verify Installation

Check PHP Version After the installation is complete, verify that PHP 7.4 is installed successfully by running the following command:

php -v

Check PHP Version in Ubuntu 22.04

This command should display the PHP version you’ve installed.

Configure PHP (Optional)

PHP configuration files are typically located in /etc/php/7.4/. Make changes to php.ini to configure PHP settings according to your requirements.

PHP Configure in Ubuntu

Switch PHP Version (Optional)

If you have multiple PHP versions installed on your system and you want to switch between them, you can use the update-alternatives command. This command will list all installed PHP versions and allow you to choose the default one:

sudo update-alternatives --config php

Restart Web Server

If you’re using Apache or Nginx as your web server, restart it for the changes to take effect.

For Apache:

sudo systemctl restart apache2

For Nginx:

sudo systemctl restart nginx

Conclusion

By following these simple steps, you can easily install PHP 7.4 on Ubuntu 22.04 and configure it for your development or production environment. PHP 7.4 offers various performance improvements, new features, and security enhancements, making it a recommended choice for PHP developers. Upgrade to PHP 7.4 today to take advantage of the latest improvements and ensure compatibility with modern PHP applications.

Leave a Reply

Your email address will not be published. Required fields are marked *