Post

How to install .Net on termux

Introduction

this is a short guide on how to code c# on your android phone using termux (but i wouldn’t recommend it unless you have no other option)

preparing the environment

It’s recommended that your phone has at least 4Gb of real RAM

Termux

termux is a “terminal emulator application for Android OS extendible by variety of packages.” basically it gives you a linux like terminal to interact with on your phone.

before installing you need to check the architecture of your phone’s CPU, you can either look online or use CPU info

next install the latest termux’s version from their github build actions (you need to be logged in to github)

click on the latest build, and choose the file that fits your phone.

Notes:

  • if you have android 5 or 6 then download a version that has android-5 in the name
  • if you have android 7 or above then download a version that has android-7 in the name

Finally after installing termux run this command to update it:

1
$ pkg update && pkg upgrade

proot-distro

proot-distro is a “utility for managing installations of the Linux distributions in Termux.”

to install it simply run:

1
$ pkg install proot-distro

next list the available distros:

1
$ proot-distro list

and choose the one you want, I’ll be going with Alpine

1
2
$ proot-distro install alpine
$ proot-distro login alpine

.Net on android

I don’t think Bill Gates would approve of this

Installation

choose a .Net version, I’ll go with 8.

then choose an architecture, either arm32 or arm64 (or the alpine variant if you installed Alpine Linux) depending on which termux version you downloaded

  • armeabi-v7a → arm32
  • arm64-v8a → arm64

next download it using curl (it’s pre-installed), and extract it

1
2
3
4
5
$ curl -L https://download.visualstudio.microsoft.com/download/pr/092bec24-9cad-421d-9b43-458b3a7549aa/84280dbd1eef750f9ed1625339235c22/dotnet-sdk-8.0.101-linux-arm64.tar.gz -o dotnet8.gz

$ mkdir dotnet

$ tar zxf dotnet8.gz -C dotnet

If you’re using Alpine you need to install some extra packages

1
$ apk add libstdc++ icu-libs

now if you try to run it, you might get this error

1
2
3
$ dotnet/dotnet new list
GC heap initialization failed with error 0x8007000E
Failed to create CoreCLR, HRESULT: 0x8007000E

Debugging

looking through the github issues for .Net, it looks like dotnet is trying to reserve 256Gb of RAM on start but android (or proot?) doesn’t allow that so it crashes immediately

thankfully there’s a fix to this (or this guide would’ve not existed)

basically all we need to do is limit the virtual memory the GC can allocate

simply open /root/.bashrc (or create /root/.profile if you’re on Alpine) and in the bottom add

1
export DOTNET_GCHeapHardLimit=1C0000000

tho a .Net developer recommends using the value 700000000 instead, so if the above doesn’t work for you, you can try this

optionally you can add this as well to disable the .Net telemetry

1
export DOTNET_CLI_TELEMETRY_OPTOUT=1

and the last step is to exit the Linux installation and login again

Conclusion

It’s kinda cool how .Net started as a windows only framework and now we can run it on android under a Linux vm lol

This post is licensed under CC BY 4.0 by the author.