Created
July 28, 2017 13:54
-
-
Save rainder/a2f95c0ff54ef034e7840b9a4666b5d9 to your computer and use it in GitHub Desktop.
install node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
get_os() { | |
local UNAME | |
UNAME="$(command uname -a)" | |
local OS | |
case "$UNAME" in | |
Linux\ *) OS=linux ;; | |
Darwin\ *) OS=darwin ;; | |
SunOS\ *) OS=sunos ;; | |
FreeBSD\ *) OS=freebsd ;; | |
AIX\ *) OS=aix ;; | |
esac | |
echo "${OS-}" | |
} | |
get_arch() { | |
local HOST_ARCH | |
local OS | |
local EXIT_CODE | |
OS="$(get_os)" | |
# If the OS is SunOS, first try to use pkgsrc to guess | |
# the most appropriate arch. If it's not available, use | |
# isainfo to get the instruction set supported by the | |
# kernel. | |
if [ "_$OS" = "_sunos" ]; then | |
if HOST_ARCH=$(pkg_info -Q MACHINE_ARCH pkg_install); then | |
HOST_ARCH=$(echo "${HOST_ARCH}" | command tail -1) | |
else | |
HOST_ARCH=$(isainfo -n) | |
fi | |
elif [ "_$OS" = "_aix" ]; then | |
HOST_ARCH=ppc64 | |
else | |
HOST_ARCH="$(command uname -m)" | |
fi | |
local ARCH | |
case "$HOST_ARCH" in | |
x86_64 | amd64) ARCH="x64" ;; | |
i*86) ARCH="x86" ;; | |
aarch64) ARCH="arm64" ;; | |
*) ARCH="$HOST_ARCH" ;; | |
esac | |
echo "${ARCH}" | |
} | |
NODE_VERSION=$1 | |
OS=$(get_os) | |
ARCH=$(get_arch) | |
if [ -z ${NODE_VERSION} ]; then | |
echo "Please specify node version as an argument to install" | |
exit 1; | |
fi | |
DIR=node-v${NODE_VERSION}-${OS}-${ARCH} | |
TAR=${DIR}.tar.xz | |
URL=https://nodejs.org/dist/v${NODE_VERSION}/${TAR} | |
echo "Downloading ${DIR}" | |
curl -k -s -O $URL | |
echo "Extracting..." | |
tar -xf ${TAR} | |
rm ${TAR} | |
cd $DIR | |
ln -sf $PWD/bin/node /usr/local/bin/node | |
ln -sf $PWD/bin/npm /usr/local/bin/npm | |
INSTALLED_VERSION=$(node --version) | |
if [ -z $INSTALLED_VERSION ]; then | |
echo "Node.js could not be installed" | |
exit 1 | |
fi; | |
echo "Installed ${INSTALLED_VERSION}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment