Skip to content

Instantly share code, notes, and snippets.

@n8henrie
Last active October 3, 2017 21:25
Quick bash script to download, extract, and set permissions on the latest nodejs 4.x for use on my Raspberry Pi B+.
#! /bin/bash
# get_nodejs.sh
# Quick script to help me download and install latest nodejs 4.x on my
# Raspberry Pi B+. May require sudo / root permissions depending on what
# destination directories (`node_dest`) you use. Don't forget to `chmod +x` the
# script.
set -euf -o pipefail
armv="$(grep '^model name' /proc/cpuinfo | \
head -n 1 | \
grep -i -o 'armv[0-9]' | \
tr '[:upper:]' '[:lower:]'\
)"
version=linux-"${armv}"l
# Destination directory for binaries
node_dest="/opt/nodejs"
node_version="$(curl -s https://nodejs.org/dist/ | grep -o 'latest-v[0-9]\.x/' | sort | tail -n 1)"
# Search for the latest compiled binary for the architecture specified by $version
binary="$(curl -sL https://nodejs.org/dist/"${node_version}" | grep -o \
"\"node.*${version}.tar.gz\"" | tr -d '"')"
# Download that binary
echo "Downloading ${binary}..."
curl -O "https://nodejs.org/dist/${node_version}/${binary}"
# Create dest if it doesn't exist
mkdir -p "$node_dest"
# Extract to $dest, stripping out the top level folder
tar --strip-components 1 -xzf "${binary}" -C "$node_dest"
rm "${binary}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment