Rust Cargo on Termux
Cargo Run Issue and Its Cause
When writing Rust code on your Android device using the Termux emulator, you may encounter a permission error when running the cargo run
command. The most common reason for this is that Termux accesses the Android file system via symbolic links (symlinks). If your device is not rooted—meaning you don’t have full access to the root directory—Android restricts applications from modifying system files.
The Rust compiler, rustc
, uses a feature called incremental compilation to save intermediate information about your project in the target
folder during the build process. This allows only the changed parts to be recompiled in subsequent builds, speeding up compilation times. However, this process requires permission to write to disk and manage files. Since Android considers that Termux does not have these permissions, it throws a permission error during the build process.
Solution: Disable the incremental
Feature
Disabling the incremental compilation feature during the build process prevents cargo
from storing intermediate information, thus eliminating the permission error. To do this, add the following lines to your project's or global Cargo configuration file (config.toml
):
[build]
incremental = false
After making this change, clean the cache and test the build with the following commands:
cargo clean
cargo check
After these steps, the cargo run
command should work smoothly on Termux.
Additional Tips and Information
Android does not allow executable file permissions (+x) or the creation of symbolic links in directories like
/storage/emulated/0
or/sdcard
. Therefore, it is recommended to keep your projects within Termux’s home directory (e.g.,~/dev/project
).To allow Termux full access to Android storage, you may need to manually grant storage permissions by running
termux-setup-storage
.Some permission issues may persist due to Android’s file system restrictions if you do not have root access. Rooting your device, however, involves security and warranty risks.
If the
incremental = false
solution is not suitable for you, you can try moving your project directory under Termux’s home directory or manually managing app permissions, especially on Android 11 and later.
When developing with Rust on Termux, these permission issues arise due to Android’s file system restrictions. By disabling the incremental feature or moving your project to an appropriate directory, you can overcome these issues and run your Rust code without problems.
Citations:
[1] https://gokayburuc.bearblog.dev/termux-uzerinde-rust-cargo-run-hatas-ve-cozumu/
Top comments (4)
pretty cool, getting cargo to work smooth on termux always feels like a win for me - you reckon more stuff should be built with mobile devs in mind or are we still too early?
My personal motto is simple:
If I encounter an obstacle while working on a project, I assume that other developers might face the same issue. When I find a method to overcome this obstacle, I take time to investigate the underlying reasons behind the problem and the effectiveness of the solution.
Once I understand the cause and resolution, I document the entire process in the form of an article. I make sure to include both the problem and its root causes, along with a detailed explanation of the solution.
If possible, I reach out to the maintainers or developers—usually via GitHub or email—and share my findings with them. In most cases, this leads to a bugfix or an improvement in the project. If there is no response or progress, and if the situation allows, I develop my own script or tool as a workaround.
In general, I believe a strong user ecosystem is essential for the growth and improvement of any mobile development environment. I do my part to contribute, knowing that as the ecosystem grows, we all benefit from its evolution.
Pretty cool - always helps to have a quick fix like this ready when stuff breaks for no reason.
Great ideas!