PHP is the heart of your web server. In a Linux environment (Ubuntu or Debian), installing and maintaining PHP via the terminal is fast and efficient. Below are the steps for a new installation and switching to the latest stable version.
Installing PHP
- Open the terminal and update system packages:
sudo apt update && sudo apt upgrade
- Install PHP:
sudo apt install php
- Verify the installation:
php -v
The current version of PHP should be displayed.
- Install common extensions (for database work, XML, ZIP, etc.):
sudo apt install php-cli php-mbstring php-curl php-xml php-zip php-mysql
Upgrading to the Latest PHP Version
- Add Ondřej Surý PPA (repository for the latest PHP):
sudo add-apt-repository ppa:ondrej/php -y
- Update package lists and install the latest available version:
sudo apt update sudo apt install php php-cli php-common php-curl php-mbstring php-xml php-zip
- Set PHP as the default version in the system:
sudo update-alternatives --install /usr/bin/php php /usr/bin/php8.3 83 sudo update-alternatives --set php /usr/bin/php8.3
Note: If you want to use a specific version, replace
php8.3with the name of the installed version (e.g.,php8.5). For automatic identification, you can use the commandls /usr/bin/php*. - Check the active version:
php -v
The output should show the installed PHP version.