How to Easily Update to Node v4 for Ghost

Ghost recently announced that Node 0.10 will no longer be supported on October 1st. If you are running your Ghost blog on DigitalOcean or through some other self host, you will need to upgrade to Node v4.
Here is how you can do that:
Note: this may not be the same for all environments, but will work on the one-click DigitalOcean Ghost installs and should work on other Ubuntu installs as well.
-
Check to see which Node version you are running
node -v
-
If it returns with any version that isn't Node 4.x.x, then continue. If you already have Node v4, then your fine and don't need to update!
-
Check where Node is installed
which nodejs
-
If the following command returns
/usr/local/bin/node
and not/usr/bin/node
then that means you have an older version of the DigitalOcean droplet. Please make a note of this for a later step. -
Remove current Node.js
sudo apt-get purge nodejs
-
Grab the Node v4 install source
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
-
Update
apt-get
resourcesapt-get update
-
Upgrade your currently installed packages
Note: this will upgrade your installed resources. So, if you dont want to do this because you are using older resources for other stuff, then skip this step
apt-get upgrade
-
Install Node.js
apt-get install nodejs
-
Move to the Ghost folder
cd /var/www/ghost/
-
Stop Ghost
service ghost stop
-
Remove installed Node modules from last Node version
rm -rf node_modules
-
Reinstall Node modules
npm install --production
Note: Sometimes on the $5 DigitalOcean droplets this command can fail because it runs out of usable memory. You will know if it failed because it will only go a few lines (should go like 100 lines normally), and Ghost won't then startup because it says it is missing packages.
To fix this, you need to add some swap memory to your droplet. This is pretty straightforward, just follow the instructions here. Just copy and paste their commands and then run steps 10 and 11 again.
-
Set permissions correctly
chown -R ghost:ghost ./*
-
If you have the older version of the droplet as figured out from step 3, you need to do the following. If not, skip to step 16:
Edit the init file:
vim /etc/init.d/ghost
Replace all instances of
/usr/local/bin/npm
with/usr/bin/npm
Delete
/usr/local/bin/npm
and/usr/local/bin/node
-
Start Ghost
service ghost start
Note: Again, if Ghost doesn't start, check the note in step 11.
That is it! You removed Node 0.10 and installed Node v4 and should be good to for a long time. If you have any trouble, feel free to leave a comment or hit us up on twitter!