Category: PHP7

Added: 23rd of October 2020

Updated On: 25th of November 2023

Viewed: 2,002 times


This article was updated and rechecked on 25th of November 2023

Running .php scripts from the terminal using Ubuntu

To install the latest version of PHP in Ubuntu or another Ubuntu based distribution enter the following command

sudo apt-get install php-cli


It will install the latest version from the repository, in my case it installed php7, to check this I just entered the following command in the terminal

Enter php --version in the terminal to display the latest version

Next I created a new file on the desktop and entered the following code.
#!/usr/bin/php -q
<?php echo "Welcome to My Computer Tips"; ?>


And then saved the file as test.php

The first line of the code points to php interpreter which can be found in the bin directory
The second lines prints out a simple message, Welcome to My Computer Tips

So that you can run .php scrips from the terminal, we need to make the file executable.
To do this we use the chmod +x command.
chmod +x test.php


Then to run the .php script, enter the following
./test.php