Skip to content

Instantly share code, notes, and snippets.

@rvagg
Last active April 2, 2024 16:51
Show Gist options
  • Save rvagg/742f811be491a49ba0b9 to your computer and use it in GitHub Desktop.
Save rvagg/742f811be491a49ba0b9 to your computer and use it in GitHub Desktop.
install-node.sh - simple script to fetch and install Node.js releases and nightlies on Linux (x86, x64 & ARM) and OS X
#!/bin/bash
# Note your /usr/local will need to be writable by the user running it,
# alternatively run it with `sudo`
hasxz=$(which xzcat)
os=$(uname | tr '[A-Z]' '[a-z]')
arch=$(uname -m)
targetdir="/usr/local/"
domain=nodejs.org
tarargs=" --strip-components=1 --exclude ./CHANGELOG.md --exclude ./README.md --exclude ./LICENSE -C ${targetdir}"
[[ "$arch" == "x86_64" ]] && arch=x64
[[ "$arch" == "x686" ]] && arch=x86
[[ "$arch" == "x86" ]] && domain=unofficial-builds.nodejs.org
type=release
line=""
yorn=""
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
echo "Usage: install-node.sh [-h|--nightly] [-l|--line <release line>] [-y|--yes]"
echo " Where:"
echo " --nightly will fetch the latest nightly version instead of the latest release version"
echo " --line <release line> will let you select a major version, e.g. --line 10"
echo " (note --line will match as much of the version as you want, e.g. --line 10.12 or --line 10.12.0)"
echo " --yes will not promot you for confirmation before installing"
exit 1
;;
-n|--nightly)
shift
;;
-y|--yes)
yorn="y"
shift
;;
-l|--line)
line="$2"
shift
shift
;;
*)
shift
;;
esac
done
latest=$(curl -sL https://${domain}/download/${type}/index.tab | grep "^v${line}" | awk '/^v[0-9]/{ print $1; exit }')
while true; do
if [ "X${yorn}" == "Xn" ]; then
break
fi
if [ "X${yorn}" == "Xy" ]; then
url="https://${domain}/download/${type}/$latest/node-${latest}-${os}-${arch}.tar"
if [ "X${hasxz}" == "X" ]; then
url="${url}.gz"
pipecmd="| tar -zx ${tarargs}"
else
url="${url}.xz"
pipecmd="| xzcat | tar -x ${tarargs}"
fi
echo "Downloading and unpacking ${url} to ${targetdir}..."
bash -c "curl -sL '${url}' ${pipecmd}"
break
fi
echo -n "Download and install Node.js ${latest} (${os}/${arch})? [y/n] "
yorn=""
read yorn
done
@hemanth
Copy link

hemanth commented Jun 25, 2016

BTW node-nightly at your finger tips! https://git.io/node-nightly

@rvagg
Copy link
Author

rvagg commented Jun 29, 2016

@hemanth thanks for picking that up, I've fixed the ${arch} bit and removed ${auth}. At one point the script was also able to do N|Solid downloads which were Auth protected at one stage. That's redundant now.

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