Skip to content

Instantly share code, notes, and snippets.

@CodeIter
Last active July 14, 2024 15:49
Show Gist options
  • Save CodeIter/ccdcc840e432288ef1e01cc15d66c048 to your computer and use it in GitHub Desktop.
Save CodeIter/ccdcc840e432288ef1e01cc15d66c048 to your computer and use it in GitHub Desktop.
Setup `glibc-runner` with pacman on Termux and install Deno.JS and Bun.JS .
#!/usr/bin/env -S bash -xeuo pipefail
set -xeuo pipefail
pkg install pacman patchelf \
which time ldd tree
echo
echo
pacman-key --init
echo
echo
pacman-key --populate
echo
echo
pacman -Syu
echo
echo
pacman -Sy glibc-runner --assume-installed bash,patchelf,resolv-conf
echo
echo
grun --help
echo
echo
curl -fsSL https://deno.land/install.sh | time sh
echo
echo
curl -fsSL https://bun.sh/install | time bash
echo
echo
export DENO_INSTALL="${HOME}/.deno"
export BUN_INSTALL="$HOME/.bun"
export PATH="${PATH}:${DENO_INSTALL}/bin:${BUN_INSTALL}/bin"
echo
echo
patchelf --print-interpreter --print-needed "$(which deno)"
echo
echo
patchelf --print-interpreter --print-needed "$(which bun)"
echo
echo
patchelf --set-rpath "${PREFIX}/glibc/lib" --set-interpreter "${PREFIX}/glibc/lib/ld-linux-aarch64.so.1" "$(which deno)"
patchelf --set-rpath "${PREFIX}/glibc/lib" --set-interpreter "${PREFIX}/glibc/lib/ld-linux-aarch64.so.1" "$(which bun)"
echo
echo
ldd "$(which deno)"
echo
echo
ldd "$(which bun)"
echo
echo
for i in deno bun ; do
cat - << EOF > ~/".${i}/bin/${i}.glibc.sh"
#!/usr/bin/env sh
_oldpwd="\${PWD}"
_dir="\$(dirname "\${0}")"
cd "\${_dir}"
if ! [ -h "${i}" ] ; then
>&2 mv -fv "${i}" "${i}.orig"
>&2 ln -sfv "${i}.glibc.sh" "${i}"
fi
cd "\${_oldpwd}"
LD_PRELOAD= exec "\${_dir}/${i}.orig" "\${@}"
# Or
#exec grun "\${_dir}/${i}.orig" "\${@}"
EOF
chmod -c u+x ~/".${i}/bin/${i}.glibc.sh"
done
echo
echo
deno.glibc.sh --version
echo
echo
bun.glibc.sh --version
echo
echo
tree -a ~/.deno ~/.bun
echo
echo
cat -n ~/.deno/bin/deno.glibc.sh
echo
echo
cat -n ~/.bun/bin/bun.glibc.sh
echo
echo
deno <<< "console.log('Hello world')"
echo
echo
file="$(mktemp -p ~/.cache --suffix .js hello-XXX)"
echo "console.log('Hello world')" > "${file}"
bun run "${file}"
echo
echo
@CodeIter
Copy link
Author

CodeIter commented May 16, 2024

NOTES:

  • When installing package with pacman on Termux and it fail due to file conflict because of that file is part of a package that was installed with apt, remember to use the pacman 's --assume-installed argument to exclude the conflicting package.

  • I added resolv-conf to the the original --assume-installed because of package conflict.

  • Unsetting LD_PRELOAD environment variable globally is not recommended because it will disable the use of the standard Gnu/Linux shebang. In that case, consider using termux-fix-shebang command before running scripts.

  • First patch your glibc binaries :

    patchelf --set-rpath "${PREFIX}/glibc/lib" --set-interpreter "${PREFIX}/glibc/lib/ld-linux-aarch64.so.1" "$(which deno)"
  • Then run it without LD_PRELOAD

    LD_PRELOAD= deno --version
  • Or run it with glibc-runner :

    grun $(which deno) --version
  • Now, this script generate a wrapper for Deno.js & Bun.js binary , backup the original binary files with .orig file extension, create symbolic link to the wrapper as the original file names. This way , we can run Deno.js or Bun.js normally with just command name, remember to at least run the wrappers (*.glibc.sh) once after installing or upgrading Deno.js & Bun.js . This approach can be done for others glibc binaries.

Reference: oven-sh/bun#8685 (comment)

Additional Notes

This doesn't intend to force developers to prioritize Termux support but serves as a guide for those with limited access to environments like Termux. Ideas and fixes presented here are mostly original, with clarifications drawn from insightful comments on related issues.

credits:

Special thanks to issue #5085 for providing insights into fixing package installations. Additionally, credit goes to Denos fix for addressing similar usage issues.

Where Did You Find It?

This issue post aims to address missing documentation for Bun on Termux. It is *not intended to compel developers to prioritize Termux support but rather serves as guidance for users with limited access to environments like Termux. The outlined solutions and insights are derived from mostly original ideas with additional clarification from comments on related issues, particularly issue #5085 and Denos fix.

@reedspool
Copy link

reedspool commented Jun 5, 2024

Thank you so much for this!!!

From relatively barebones Termux, needed to pkg install which time ldd tree before your script worked.

Also might help others that after this script was successful, I had to restart session to get bun in my path. And in order for bun install to work on my prior bun project, I had to use --backend-copyfile as per this comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment