How to Install PHP in Termux and Run a Script (2026 Guide)
Want to turn your Android device into a portable web development station? You can. Learning how to install PHP in Termux lets you write and test server-side code without a laptop. Whether you're testing backend logic on the go or building lightweight scripts, Termux makes mobile PHP development genuinely usable.
This guide covers the exact installation steps, how to write and run your first PHP file, how to use the built-in PHP development server, and how to fix the errors people run into most often.
What Is PHP in Termux?
PHP is a widely used, open-source server-side scripting language built for web development. Paired with Termux — an Android terminal emulator that gives you a real Linux command-line environment — you get a working PHP setup directly on your phone or tablet, with no root access required.
If you're new to Termux itself, it's worth first working through our guide on how to install Termux on Android and getting comfortable with the basic Termux commands for beginners before diving into PHP.
Once PHP is running inside Termux, you can:
- Execute command-line PHP scripts.
- Test dynamic web pages locally.
- Learn and practice backend logic anywhere you take your phone.
Quick Summary: Install PHP in Termux
If you just need the short version:
- Update your package repositories with
pkg update && pkg upgrade. - Install PHP with
pkg install php. - Confirm the install by checking the version:
php -v. - Create a script file and run it with
php filename.php.
Prerequisites Before You Begin
Before typing any commands, make sure your environment is ready. You'll need:
- The Termux app installed from F-Droid or GitHub. These are the official, actively maintained release sources — the Google Play Store build has historically lagged behind or gone unmaintained, so it's best avoided for a dependable setup.
- An active internet connection to download packages.
- Roughly 100–150MB of free storage for PHP and its dependencies.
Step-by-Step Guide: How to Install PHP in Termux
Open Termux and follow these steps in order.
Step 1: Update and Upgrade Packages
Always refresh your local package index before installing anything new — this avoids dependency errors and broken mirror links. For more detail on why this matters, see our guide on updating and upgrading packages in Termux.
pkg update && pkg upgrade -y
Step 2: Install PHP
Run the install command. Termux will pull the latest PHP build available in its official package repository.
pkg install php -y
Step 3: Verify the Installation
Once installation finishes, confirm PHP is working by checking its version.
php -v
If the terminal prints PHP version details, the installation was successful.
Writing and Running Your First PHP Script
Let's go beyond a version check and write something real: a script that prints text and runs a basic calculation.
-
Create a project folder and move into it:
mkdir php-projects && cd php-projects -
Create a file named
index.phpusing nano. If you haven't used it before, our guide to Nano and Vim in Termux covers the basics.nano index.php -
Paste in the following code:
<?php echo "Hello from Termux!\n"; $current_time = date("Y-m-d H:i:s"); echo "Current Server Time: $current_time\n"; $a = 15; $b = 25; echo "The sum of $a and $b is: " . ($a + $b) . "\n"; ?> - Save and exit nano: press
Ctrl + O, thenEnterto save, thenCtrl + Xto exit. -
Run the script:
php index.php
You should see the output printed directly in your Termux session.
Using the PHP Built-in Server in Termux
PHP ships with a built-in development server, which means you can test web pages locally without installing Apache or Nginx first. This is the fastest way to preview PHP output in a real browser.
From your project folder, run:
php -S localhost:8080
With the server running, open your mobile browser and go to http://localhost:8080 or http://127.0.0.1:8080. Your PHP script will render as a web page. To stop the server, return to Termux and press Ctrl + C.
If you eventually want a full local site setup — including virtual hosts or a persistent web server rather than the quick development server — check our guide on hosting a local website using Termux.
PHP in Termux vs. Alternative Tools
How does running PHP locally in Termux compare to online compilers or a cloud VPS?
| Feature | Termux (Local Android) | Online PHP Compilers | Cloud VPS (DigitalOcean/AWS) |
|---|---|---|---|
| Internet required | No (once installed) | Yes | Yes |
| Cost | Free | Free / ad-supported | Monthly subscription |
| File access | Full local storage access | Limited / temporary | Full SSH access |
| Setup complexity | Low (a few commands) | None | Medium to high |
Cloud servers still win for production hosting, but Termux is hard to beat for offline practice, zero cost, and instant local experimentation from your pocket.
Troubleshooting: Common PHP-in-Termux Errors
1. "pkg: command not found" or "Unable to locate package php"
Cause: Your package lists are outdated, or you're running an unmaintained Termux build.
Fix: Run pkg update first. If the package still won't resolve, see our full breakdown in how to fix "Unable to Locate Package" in Termux, and make sure you're using a build from F-Droid or GitHub rather than an outdated Play Store install.
2. Permission denied when saving files
Cause: Termux doesn't automatically have access to your device's shared storage.
Fix: Run termux-setup-storage and grant the permission prompt. It's simplest to keep your PHP projects inside Termux's home directory (~/) to avoid storage permission issues entirely — our Termux storage permission fix guide covers this in more depth.
3. "Address already in use" when starting the built-in server
Cause: Another process, or a previous server instance you forgot to stop, is already using port 8080.
Fix: Start the server on a different port, such as:
php -S localhost:9000
Tips for PHP Development in Termux
- Use a real keyboard when you can. Typing symbols like
$,;, and{}on a phone screen is slow — a Bluetooth keyboard or a symbol-friendly keyboard app makes a real difference. - Install Composer for dependency management. Once you're past basic scripts, Composer lets you pull in PHP libraries for larger projects:
pkg install composer. - Back up your work. Phones get dropped, rebooted, or run out of battery. Push your scripts to a Git repository — see how to install Git in Termux and clone repositories — or sync them to cloud storage.
Frequently Asked Questions
Can I run WordPress using PHP in Termux?
Technically yes. Since Termux can run PHP alongside MySQL/MariaDB in Termux and a web server, you can assemble a local LAMP-style stack for testing WordPress on your device. It's fine for learning and experimentation, but expect it to be heavier on battery and CPU than a proper server.
Do I need root access to run PHP in Termux?
No. Termux runs in a user-space sandbox, so you can install and run PHP without rooting your device or touching core Android system files.
How do I update PHP in Termux?
Update it the same way as any other package: pkg update && pkg upgrade. This pulls the latest PHP build available in Termux's repository.
Conclusion
You now know how to install PHP in Termux, write and run your own scripts, launch the built-in development server, and fix the most common setup errors. With this in place, your Android phone becomes a genuinely usable place to practice and prototype PHP code.
Next, open Termux, run your update commands, and start experimenting — or check out our guide on installing Node.js and npm in Termux if you want to compare PHP against a JavaScript-based backend setup.
Join the conversation