How to Install PHP in Termux and Run a Script
How to Install PHP in Termux and Run a Script
Want to turn your Android device into a portable web development station? You can. By learning how to install PHP in Termux and run a script, you unlock mobile coding without needing a heavy laptop. Whether you want to test server-side code on the go or build lightweight applications, Termux makes it possible.
This guide walks you through the entire setup process. We will cover the exact installation steps, how to write and execute your first file, how to leverage the termux php built-in server, and how to troubleshoot common roadblocks.
What is PHP in Termux?
PHP is a widely-used, open-source server-side scripting language primarily designed for web development. When paired with Termux—an Android terminal emulator and Linux environment—you get a fully functional Linux command-line interface directly on your phone or tablet. You do not need root access to get started.
By running PHP inside Termux, you can:
- Execute command-line scripts.
- Test dynamic web pages locally.
- Understand backend logic anywhere you take your phone.
Quick Summary: Install PHP Termux (AI Overview Optimized)
If you need a fast overview, here is the short version of what you need to do:
- Update your Termux package repositories using
pkg update && pkg upgrade. - Run the primary install php termux command:
pkg install php. - Verify the installation by checking the version:
php -v. - Create a script file and run it using the command
php filename.php.
Prerequisites Before You Begin
Before typing any commands, make sure your environment is ready. You need:
- The Termux app installed on your Android device (downloaded from F-Droid for the most up-to-date version, as Google Play Store versions are deprecated).
- An active internet connection to download the packages.
- About 100MB of free storage space for PHP and its dependencies.
Step-by-Step Guide: How to Install PHP in Termux
Let’s dive into the installation process. Open your Termux app and follow these steps carefully.
Step 1: Update and Upgrade Packages
Always update your local package index before installing new software. This prevents dependency errors and broken links.
pkg update && pkg upgrade -y
Step 2: Run the Install Command Block
Now, execute the installation command for PHP. Termux will fetch the latest stable release available in its repository.
pkg install php -y
Step 3: Verify the Installation
Once the installation finishes, verify that PHP is working correctly by checking its version number.
php -v
If the terminal returns the PHP version details (for example, PHP 8.x.x), you have successfully completed the installation.
Real Use-Case Example: Writing and Running Your First PHP Script
Let's move beyond a simple version check. Here is a real use-case example: creating a simple script that outputs text and processes a basic mathematical calculation.
- Create a new directory for your projects and navigate into it:
mkdir php-projects && cd php-projects - Create a file named
index.phpusing the nano text editor (or your preferred editor):nano index.php - Paste the following code into the editor:
<?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, andCtrl + Xto exit). - Run the script using PHP:
php index.php
You should instantly see the output printed right inside your mobile terminal window.
Using the Termux PHP Built-in Server
One of the most powerful features of PHP is its built-in web server. You can use the termux php built-in server to test web pages locally without installing Apache or Nginx.
To start the built-in server from your project folder, run:
php -S localhost:8080
Once running, open your mobile web browser and navigate to http://localhost:8080 or http://127.0.0.1:8080. You will see your PHP script rendered as a web page! To stop the server, go back to Termux and press Ctrl + C.
Comparison Note: PHP in Termux vs. Alternative Tools
How does running PHP in Termux compare to using online PHP compilers or a cloud-based VPS?
| Feature | Termux (Local Android) | Online PHP Compilers | Cloud VPS (DigitalOcean/AWS) |
|---|---|---|---|
| Internet Required | No (Once installed) | Yes | Yes |
| Cost | 100% Free | Free / Ad-supported | Monthly Subscription |
| File Access | Full local storage access | Limited / Temporary | Full SSH access |
| Setup Complexity | Low (Few commands) | None | Medium to High |
While cloud servers offer production-grade environments, Termux wins for offline usability, zero cost, and instant local experimentation right from your pocket.
Troubleshooting Section: Common Errors and Fixes
Sometimes things do not go as planned. Here are 3 common errors users face and how to fix them:
1. "pkg: command not found" or "E: Unable to locate package php"
Cause: Your package lists are outdated, or you downloaded Termux from the Google Play Store (which is no longer maintained).
Fix: Update your repositories using pkg update. If that fails, uninstall the Play Store version and install the latest release from F-Droid.
2. Permission Denied Error when saving files
Cause: Termux does not have access to your device's shared storage folders (like internal storage).
Fix: Run the storage setup command termux-setup-storage and grant the requested permissions. It is recommended to write your scripts inside Termux's home directory (~/) to avoid permission hassles.
3. Built-in Server Port Already in Use
Cause: You tried starting the built-in server, but another process or an old instance is already utilizing port 8080.
Fix: Use a different port number, such as port 8000 or 9000: php -S localhost:9000.
Expert Tips for PHP Development in Termux
- Use a good keyboard: Coding on a phone screen can be tough. Consider using a physical Bluetooth keyboard or a smart keyboard app like Hacker's Keyboard for easier access to symbols like
$,;, and{}. - Install Composer: Once you master basic scripts, you can install Composer in Termux to manage PHP dependencies for larger projects.
- Keep backups: Mobile devices are prone to battery loss, accidental drops, or system reboots. Sync your Termux scripts to a local Git repository or cloud storage.
Frequently Asked Questions (FAQs)
Can I run WordPress using PHP in Termux?
Yes, you can! Since Termux supports PHP, MariaDB, and Apache/Nginx (the LEMP/LAMP stack), you can technically host WordPress locally on your Android device for testing purposes. However, it can consume significant battery and CPU resources.
Do I need root access to run PHP in Termux?
No, root access is completely unnecessary. Termux runs within a user-space sandbox, allowing you to install and execute PHP safely without modifying your Android operating system core files.
How do I update PHP in Termux?
You can easily update PHP along with all other installed packages by running the standard package upgrade command: pkg update && pkg upgrade.
Conclusion
You now know how to install PHP in Termux, write and run scripts, spin up a development server, and troubleshoot common issues. Mobile development has come a long way, and your Android phone is now a capable coding environment.
Ready to build your next mobile-coded project? Open up your Termux terminal, run your update commands, and start experimenting with code today!
Join the conversation