How to Install PHP on Ubuntu 24.04
PHP is a robust and general-purpose scripting language used by some of the biggest websites like Google, WordPress, Drupal, Shopify, and Slack. PHP is one of the beloved choices of developers that helps them create a simple blogging page to develop dynamic websites. PHP provides users with a wide range of tools, libraries, and frameworks so that they can build powerful websites and bring the websites to life.
If you have recently started using Ubuntu 24.04 and want to begin your web development journey, you must install PHP on your system.
In this tutorial, you will learn:
- How to Install PHP on Ubuntu 24.04
- How to Use PHP on Ubuntu 24.04
- How to Switch to Another PHP Version on Ubuntu 24.04
- How to Set the Default PHP Version on Ubuntu 24.04
- Conclusion
How to Install PHP on Ubuntu 24.04
You can install PHP on Ubuntu 24.04 from:
How to Install PHP on Ubuntu 24.04 from Apt Repository
The Ubuntu apt repository includes PHP version 8.3, which can be installed directly from the apt install command. However, before you install PHP from the apt install command, you must update the Ubuntu apt repository using:
sudo apt update && sudo apt upgrade -y
Once the Ubuntu repository is updated, you can install PHP from the following command:
sudo apt install php -y
Note: The PHP version might be changed after you update the repository at the time of writing this article. However, the PHP version won’t be the latest.
After completing the PHP installation on Ubuntu, you can confirm the installation by using:
php --version
Note: To completely remove PHP from Ubuntu 24.04, you can use:
sudo apt autoremove php -y
How to Install PHP on Ubuntu 24.04 from tar.gz Source File
You can also download the official PHP tar.gz source file from the website and install the PHP latest version on Ubuntu 24.04. To use the tar.gz source file method for PHP installation, use the following steps:
Step 1: Download PHP Latest Version tar.gz File
First, navigate to the official PHP download page and download the latest version tar.gz file on Ubuntu. The latest version of PHP at the time of installation was 8.3.3, which can be directly downloaded from the following wget command:
wget https://www.php.net/distributions/php-8.3.3.tar.gz
Step 2: Extract tar.gz File Content
Now, you should extract the content of the PHP tar.gz source file on Ubuntu from the tar command given below:
tar -xzf php-8.3.3.tar.gz
Step 3: Download Dependencies for PHP Installation
You must also ensure installing some dependencies needed for PHP installation, they can be installed on Ubuntu from:
sudo apt install cmake build-essential libxml2-dev re2c libsqlite3-dev autoconf pkg-config bison -y
Step 4: Configure PHP Installation Files
Now, navigate to the PHP source folder using the cd command followed by the folder name.
cd php-8.3.3
Note: The folder name will not be the same if you are installing another PHP version. You can find the folder name by using the ls command.
Then, inside the PHP folder, use the following command to configure the PHP installation files:
./configure
Step 5: Install PHP on Ubuntu
After configuring the installation files, you can then perform PHP installation on Ubuntu using the following command:
sudo make install
Step 6: Confirm PHP Installation on Ubuntu
Once you complete the PHP installation, confirm the PHP version on Ubuntu using:
php --version
Step 7: Add PATH Environment on Ubuntu (Additional)
In case you fail to run the php command, then perform an additional step by adding the PATH environment so that the system will locate the path where PHP is installed. To add a PATH environment on Ubuntu, first, find the PHP path using:
which php
Then open the .bashrc file on Ubuntu using:
sudo nano ~/.bashrc
Now, add the PHP path you retrieved from the which command inside the .bashrc file:
export PATH="$PATH:/usr/local/bin/php"
Once the path is added, save your file using keys CTRL+X, then Y, and press Enter to close the file.
Step 8: Confirm PHP Installation on Ubuntu
After adding the PATH environment, you can confirm the PHP installation on Ubuntu using:
php --version
Note: If you have installed PHP on Ubuntu from the tar.gz method, you can remove PHP by using:
sudo rm -rf /usr/local/bin/php
How to Use PHP on Ubuntu 24.04
To use PHP on Ubuntu 24.04 and ensure it is running on the system, first create a file using nano editor with .php extension:
nano testfile.php
Inside this file, add the following PHP code:
<?php echo 'Hello Linux Genie Users!' ?>
Then save the PHP file and run it on Ubuntu using the php command followed by the file name:
php testfile.php
How to Switch to Another PHP Version on Ubuntu 24.04
If you have installed several PHP versions on Ubuntu and need help switching to another version, simply run the below-given update-alternatives command
sudo update-alternatives --set php /usr/bin/php<version_no>
Ensure replacing the <version_no> with the PHP version you want to switch on Ubuntu.
How to Set the Default PHP Version on Ubuntu 24.04
You can also set the default PHP version on Ubuntu in case of multiple PHP installation by running the following command:
sudo update-alternatives --config php
Then choose the desired PHP version as your default one:
Conclusion
PHP is a server-side scripting language used for developing powerful websites and applications. You can install PHP on Ubuntu 24.04 from the apt repository or tar.gz source file. The apt repository method is simple and quickly installs PHP on Ubuntu. However, for installing the PHP latest version, you must go with the tar.gz source file method. The tar.gz method may require some time, but once the installation is done, you will be able to use the latest PHP version on Ubuntu.
Ultimately, the choice for PHP installation depends on users’ needs, since both these methods will ensure smooth PHP installation. By doing this, you can start your web development journey with PHP on your Ubuntu system.











