Fix Termux Package Permission and Storage Errors
In this article
Termux is a powerful terminal emulator for Android, but its sandboxed nature often leads to frustrating permission and storage errors when installing packages or accessing files. This guide walks you through the most common causes and fixes, from granting proper storage access to resolving package manager lock issues. You'll learn the exact commands and settings needed to get Termux working smoothly again.
What You'll Need
- A device running Android 7.0 or newer (for scoped storage support)
- Termux installed from F-Droid or the Google Play Store (version differences matter)
- An internet connection for downloading packages
- Optional: a file manager app to verify permissions
Step-by-Step
1. Grant Storage Permission to Termux
Termux needs explicit permission to access shared storage (like /sdcard). Without it, you'll see errors like Permission denied when trying to read or write files outside its private directory.
- Open your device's Settings app.
- Navigate to Apps or Application Manager.
- Find and tap on Termux.
- Tap Permissions.
- Enable Storage (and optionally Files and media on newer Android versions).
- Return to Termux and run
termux-setup-storage. This creates a~/storagedirectory with symlinks to shared storage.
2. Update Termux and Its Repositories
Outdated package lists can cause 404 errors or missing dependencies. Always start with a clean update.
- Run
pkg updateto refresh the package list. - Run
pkg upgradeto upgrade all installed packages. - If you get a
Repository ... is not trustederror, runpkg install termux-keyringfirst, then repeat the update.
3. Fix Package Manager Lock Errors
If you see Could not get lock /data/data/com.termux/files/usr/var/lib/dpkg/lock, another package process is still running or crashed.
- Close all Termux sessions (swipe them away).
- Wait a few seconds, then reopen Termux.
- If the error persists, run
pkg cleanto clear cached archives. - As a last resort, run
rm -f $PREFIX/var/lib/dpkg/lockand thenpkg update. This forcefully removes the lock file.
4. Resolve "Read-only file system" Errors
This usually happens when trying to write to system directories or when the storage permission isn't fully granted.
- Ensure you're writing to
~/storageor$HOME, not to/systemor/datadirectly. - If you need to write to an SD card, check that the SD card is not write-protected (physical switch) and that Termux has permission to access it.
- Run
termux-setup-storageagain to re-establish symlinks.
5. Handle "Failed to fetch" or 404 Errors
These often occur because the repository URL is outdated or the package doesn't exist for your Termux version.
- Run
termux-change-repoand select a different mirror (e.g., a regional one). - If you're using the Play Store version, note that it's deprecated and often has outdated packages. Consider switching to the F-Droid version.
- Clear the package cache:
pkg clean.
6. Fix "Permission denied" for Specific Commands
Some commands (like apt) require proper execution permissions. This is rare but fixable.
- Run
chmod 755 $PREFIX/bin/apt(replaceaptwith the actual command if different). - If you see
termux-setup-storage: not found, reinstall thetermux-toolspackage:pkg reinstall termux-tools.
7. Reset Termux (Nuclear Option)
If nothing works, a full reset will clear all corrupted data. This removes all installed packages and your home directory files.
- Back up any important files from
~/storageto your device's shared storage. - In Termux, run
rm -rf $PREFIX(this removes the entire Termux installation). - Close Termux completely.
- Reopen Termux. It will reinitialize itself.
- Run
pkg updateandpkg upgradeto restore packages.
Common Mistakes
- Granting permission but not running
termux-setup-storage– The permission alone isn't enough; you must run this command to create the symlinks. - Using the Play Store version – It's outdated and often causes repository errors. The F-Droid version is actively maintained.
- Forgetting to close other Termux sessions – Multiple sessions can interfere with package locks.
- Writing to
/sdcarddirectly – Always use~/storagesymlinks; direct paths may fail due to Android's scoped storage. - Ignoring the keyring package – Without it, repository signatures fail, causing trust errors.
Tips
- Always run
pkg updatebefore installing anything new. - Use
pkg searchto verify a package exists before installing. - If you're on Android 11+, make sure Termux has All files access permission if you need to access other apps' data.
- Keep Termux and its packages updated regularly to avoid compatibility issues.
- For persistent storage issues, consider using
termux-setup-storagein a fresh session after every Termux update.
Troubleshooting
If the steps above don't resolve your issue, try these additional checks:
- Check Termux's version: Run
termux-infoand verify it's the latest from F-Droid (0.118.0+). If not, update it. - Check Android's storage permission: Go to Settings > Apps > Termux > Permissions and ensure Storage is toggled on. On some devices, you may need to allow "Files and media" separately.
- Check for SELinux issues: If you're rooted, run
getenforce. If it returnsEnforcing, trysetenforce 0temporarily to see if that fixes the issue. Remember to re-enable it. - Reinstall Termux completely: Uninstall, then install from F-Droid. This clears all data and settings.
- Check for corrupted package lists: Run
rm -rf $PREFIX/var/lib/apt/listsand thenpkg update.
Frequently Asked Questions
Q: Why do I get "Permission denied" when trying to access /sdcard?
You haven't granted storage permission or haven't run termux-setup-storage. Grant the permission in Android settings, then run that command.
Q: The Play Store version of Termux is outdated. What should I do?
Uninstall the Play Store version and install Termux from F-Droid. The Play Store version is no longer maintained and causes many repository errors.
Q: How do I fix "Could not get lock" errors?
Close all Termux sessions, wait a few seconds, then reopen. If it persists, run pkg clean or remove the lock file with rm -f $PREFIX/var/lib/dpkg/lock.
Q: Why can't I write to my external SD card?
Android's scoped storage restricts direct writes to SD cards. Use termux-setup-storage and access it via ~/storage symlinks. Also check the SD card's physical write-protect switch.
Q: What does "Repository ... is not trusted" mean?
It means the repository's GPG key is missing or outdated. Install the keyring with pkg install termux-keyring, then update.
Q: Can I reset Termux without losing my files?
Yes, back up your files to shared storage first. Then run rm -rf $PREFIX, which only removes the Termux installation, not your home directory. Your files in ~/storage remain intact.