Use your distribution’s package manager
Linux distributions package PHP and extensions separately. Ubuntu and Debian use APT; Fedora uses DNF. These commands are not interchangeable. First check what your distribution offers and compare that branch with PHP’s current supported-versions page. For a new learning setup, use a maintained PHP release and its latest available security updates.
A distribution may backport security fixes without changing the upstream-looking version string. Check its security policy when maintaining an existing system. If a new installation would use a branch that is end of life upstream and is not maintained by your distribution, use a supported OS release or trusted vendor packaging instead of adding an arbitrary third-party repository.
Ubuntu and Debian: install the command-line PHP package
On Ubuntu or Debian, update package metadata, inspect the candidate php-cli package, and install it if it provides an appropriate maintained branch for your system. php-cli supplies the terminal command needed for a first script. It does not by itself configure Apache, Nginx or a public website.
APT may install supporting packages automatically. Check php -v afterward: a successful package install is not proof that the shell is invoking the release you expected. If you use a different Debian-based distribution, verify its own package names and support policy.
Ubuntu/Debian — refresh package metadata
sudo apt updateUbuntu/Debian — inspect the candidate before installation
apt-cache policy php-cliUbuntu/Debian — install CLI PHP
sudo apt install php-cliFedora: install the command-line PHP package
On Fedora, inspect the php-cli package with DNF and compare its offered branch with current PHP support. Install php-cli for terminal scripts. Fedora and other DNF-based distributions can have different release policies and package availability, so check that these commands and packages apply before copying them to another distribution.
Fedora — inspect the package
dnf info php-cliFedora — install CLI PHP
sudo dnf install php-cliVerify PHP and find its configuration
Run php -v to see the command-line version, command -v php to locate the executable, php --ini to see the loaded configuration and scanned .ini files, and php -m to list loaded extensions. If you have several PHP installations, the executable earlier in PATH is usually the one the shell selects. The CLI and a web server’s PHP process can use different settings.
On Debian-derived systems, CLI configuration is commonly under /etc/php/<version>/cli/ and FPM configuration under a separate fpm directory. On Fedora, packages commonly use /etc/php.ini and /etc/php.d/. Treat these as typical locations; php --ini tells you the location actually used by your current process.
Linux — verify the command and version
command -v php
php -vLinux — find configuration and extensions
php --ini
php -mAdd extensions only when a project needs them
Extensions provide features beyond the core installation. For example, a project might need cURL for HTTP requests or a MySQL driver for database access. Ubuntu/Debian package names include php-curl and php-mysql; Fedora package names include php-gd and php-mysqlnd. Search your distribution’s package index and verify the package belongs to the same PHP installation before installing it.
After installing an extension, check php -m in the CLI environment. A running PHP-FPM or web-server process may need a restart to load a newly installed extension or changed configuration. Avoid editing an .ini file by guesswork if the package manager already created one.
Ubuntu/Debian — search extension packages
apt search php-curlFedora — search extension packages
dnf search phpWhen PHP-FPM is useful
PHP-FPM manages PHP worker processes for a web server such as Nginx or a suitably configured Apache server. You do not need it to run php hello.php in a terminal. If you later build a local web application, install the distribution’s php-fpm package and follow that distribution’s web-server configuration guide. Keep FPM’s listening endpoint inaccessible to untrusted networks; installing the package alone is not a complete secure web deployment.
Ubuntu/Debian — optional PHP-FPM package
sudo apt install php-fpmFedora — optional PHP-FPM package
sudo dnf install php-fpmRun your first program
Create a folder, save the example below as hello.php, and run php hello.php in that folder. The terminal should display “Hello, Codectionary!”. The PHP CLI executes the file; a browser does not run .php files opened directly from disk.
Linux — make a project folder
mkdir php-first
cd php-firstLinux — after saving hello.php
php hello.phpCommon Linux setup problems
If php is not found, install your distribution’s php-cli package and check command -v php. If the version differs from the package you installed, inspect PATH and any parallel PHP installations. If an extension is missing, search for its distribution package and confirm php -m afterward. If the web server works differently from the CLI, compare their PHP versions and configuration separately.
If APT or DNF offers an unsupported upstream branch for a new setup, do not copy commands from an unrelated release or random package repository just to force a newer version. Check your distribution’s maintained releases, documented repositories and security support first. Keep local test servers bound to loopback unless you intentionally configure a secured deployment.
Syntax
<?php
echo "Hello, Codectionary!\n";
Examples
First Linux command-line script
Save as hello.php and run php hello.php from its directory.
<?php
echo "Hello, Codectionary!\n";
- The opening tag tells the PHP runtime where source code begins; the line is a PHP echo statement.
- echo sends the quoted string to standard output. The \n escape starts a new line after the greeting.
- After php hello.php prints Hello, Codectionary!, change the greeting and rerun it to confirm the CLI uses the file you saved.
Best practices
- Use commands for your specific distribution and check its package candidate before installing.
- Keep PHP and extensions on a maintained, patched release; understand when your distribution backports security fixes.
- Install PHP-FPM only when a web server needs it, and do not expose its FastCGI endpoint to an untrusted network.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual