DEV Community

MuthuKumar
MuthuKumar

Posted on • Edited on

Install PHP on Ubuntu using the terminal

To install PHP on Ubuntu using the terminal, you can follow these steps:

  1. Update Package List: It's a good practice to start by updating your package list to make sure you're installing the latest version of PHP available in your repositories.
   sudo apt update
Enter fullscreen mode Exit fullscreen mode
  1. Install PHP: You can install PHP along with commonly used extensions by running the following command:
   sudo apt install php
Enter fullscreen mode Exit fullscreen mode
  1. Install Additional PHP Extensions (Optional): Depending on your project's requirements, you might need additional PHP extensions. You can search for available extensions using the following command:
   sudo apt search php- | grep '^php-'
Enter fullscreen mode Exit fullscreen mode

Then, install the desired extension(s) using a command like:

   sudo apt install php-extension-name
Enter fullscreen mode Exit fullscreen mode
  1. Check PHP Version: After installation, you can check the installed PHP version using:
   php -v
Enter fullscreen mode Exit fullscreen mode
  1. Restart Web Server (if applicable): If you're planning to use PHP with a web server like Apache or Nginx, you'll need to restart the server to apply the changes.

For Apache:

   sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

For Nginx:

   sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

That's it! PHP should now be installed on your Ubuntu system. You can test it by creating a simple PHP file and running it through a web server, or you can use the built-in PHP CLI (Command Line Interface) to execute PHP scripts directly from the terminal.

I apologize for any confusion. The "php-extension-name" in my previous response was a placeholder for the actual name of the PHP extension you want to install. Let me clarify:

When installing PHP extensions on Ubuntu, you need to provide the correct package name for the extension you want to install. Here's how you can search for available PHP extensions and install them:

  1. Search for Available PHP Extensions: To find available PHP extensions, you can use the apt search command followed by "php-" as a prefix. This will list all the available PHP-related packages.
   sudo apt search php-
Enter fullscreen mode Exit fullscreen mode
  1. Install the Desired PHP Extension: Once you've identified the extension you want to install from the search results, use the sudo apt install command to install it. Replace php-extension-name with the actual name of the extension.
   sudo apt install php-extension-name
Enter fullscreen mode Exit fullscreen mode

For example, if you want to install the GD extension for image manipulation, the command would be:

sudo apt install php7.4-gd  # Replace "7.4" with your PHP version
Enter fullscreen mode Exit fullscreen mode

Please replace php7.4-gd with the actual name of the extension you want to install. If you're unsure about the correct package name, you can refer to the search results from the apt search command.

Remember to adapt the version number (like "7.4") to the version of PHP you have installed on your system. If you're not sure about the PHP version, you can check it using the php -v command.

Top comments (0)