Skip to content

Instantly share code, notes, and snippets.

@sbrl
Created October 6, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbrl/48e6423f2e97462149ef to your computer and use it in GitHub Desktop.
Save sbrl/48e6423f2e97462149ef to your computer and use it in GitHub Desktop.
A simple bash script to update Node.js to the latest version.
#!/bin/bash
echo "> Getting latest version number"
VERSION=v${1:-$(curl https://nodejs.org/dist/index.json | sed -e 's/^.*"version":"\([^"]*\)".*$/\1/' | head -n 2 | tail -n -1 | cut -c 2-)}
NODEJS=node-${VERSION}-linux-x64
echo "> Downloading $VERSION of node.js"
curl -s https://nodejs.org/dist/${VERSION}/${NODEJS}.tar.xz | tar xvfJ -
echo "> Setting ownership of /usr/local to $USER"
sudo chown -R ${USER}:${USER} /usr/local
echo "> Moving extracted node.js binaries"
rm -rf /usr/local/lib/${NODEJS} && mv ${NODEJS} /usr/local/lib
echo "> Symlinks"
sudo rm -f /usr/local/bin/{iojs,node,npm,node-gyp}
ln -s /usr/local/lib/${NODEJS}/bin/node /usr/local/bin/node
ln -s /usr/local/lib/${NODEJS}/bin/node /usr/local/bin/node
ln -s /usr/local/lib/${NODEJS}/bin/npm /usr/local/bin/npm
echo "node.js version: $(node -v), npm version: $(npm -v)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment