Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Fix Termux Package Permission and Storage Errors

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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.

  1. Open your device's Settings app.
  2. Navigate to Apps or Application Manager.
  3. Find and tap on Termux.
  4. Tap Permissions.
  5. Enable Storage (and optionally Files and media on newer Android versions).
  6. Return to Termux and run termux-setup-storage. This creates a ~/storage directory 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.

  1. Run pkg update to refresh the package list.
  2. Run pkg upgrade to upgrade all installed packages.
  3. If you get a Repository ... is not trusted error, run pkg install termux-keyring first, 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.

  1. Close all Termux sessions (swipe them away).
  2. Wait a few seconds, then reopen Termux.
  3. If the error persists, run pkg clean to clear cached archives.
  4. As a last resort, run rm -f $PREFIX/var/lib/dpkg/lock and then pkg 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.

  1. Ensure you're writing to ~/storage or $HOME, not to /system or /data directly.
  2. 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.
  3. Run termux-setup-storage again 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.

  1. Run termux-change-repo and select a different mirror (e.g., a regional one).
  2. 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.
  3. 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.

  1. Run chmod 755 $PREFIX/bin/apt (replace apt with the actual command if different).
  2. If you see termux-setup-storage: not found, reinstall the termux-tools package: 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.

  1. Back up any important files from ~/storage to your device's shared storage.
  2. In Termux, run rm -rf $PREFIX (this removes the entire Termux installation).
  3. Close Termux completely.
  4. Reopen Termux. It will reinitialize itself.
  5. Run pkg update and pkg upgrade to 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 /sdcard directly – Always use ~/storage symlinks; 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 update before installing anything new.
  • Use pkg search to 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-storage in 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-info and 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 returns Enforcing, try setenforce 0 temporarily 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/lists and then pkg 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.