How to install Ghost on Ubuntu 16.04

Ghost is an opensource lightweight publishing platform for blogging. It uses NodeJS, Ember.js and Handlebars under the hood.

In this tutorial I will describe how to install Ghost on Ubuntu 16.04 VPS instance. If VPS instance has less than 1 GB of RAM installation could be a little bit tricky.

Without further ado let's get started.

Update Ubuntu

Before installing any new software it's better to have our system up to date . Otherwise package conflicts may occur.

sudo apt-get update  
sudo apt-get upgrade -y  

Install required tools

Because Ghost is built on NodeJS we need nodejs and npm to be installed. Also we need curl, zip, wget for further installation.

sudo apt-get install curl wget zip  

Install NodeJS and NPM

curl -sL https://deb.nodesource.com/setup | sudo bash -  
sudo apt-get install -y nodejs nodejs-legacy  

We also need nodejs-legacy because in some cases during the installation we can get an error something like that:

/usr/bin/env: ‘node’: No such file or directory and also

npm ERR! Linux 4.4.0-34-generic  
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install" "--production"  
npm ERR! node v4.2.6  
npm ERR! npm  v3.5.2  
npm ERR! file sh  
npm ERR! code ELIFECYCLE  
npm ERR! errno ENOENT  
npm ERR! syscall spawn

npm ERR! sqlite3@3.1.4 install: `node-pre-gyp install --fallback-to-build`  
npm ERR! spawn ENOENT  
npm ERR!  
npm ERR! Failed at the sqlite3@3.1.4 install script 'node-pre-gyp install --fallback-to-build'.  
npm ERR! Make sure you have the latest version of node.js and npm installed.  
npm ERR! If you do, this is most likely a problem with the sqlite3 package,  
npm ERR! not with npm itself.  
npm ERR! Tell the author that this fails on your system:  
npm ERR!     node-pre-gyp install --fallback-to-build  
npm ERR! You can get information on how to open an issue for this project with:  
npm ERR!     npm bugs sqlite3  
npm ERR! Or if that isn't available, you can get their info via:  
npm ERR!     npm owner ls sqlite3  
npm ERR! There is likely additional logging output above.  

Download Ghost

We will install Ghost into /var/www/ghost folder. In case when there are no such folders we should create them

sudo mkdir -p /var/www  

Now let's got to /var/www folder and download Ghost

cd /var/www  
sudo wget https://ghost.org/zip/ghost-latest.zip  
sudo unzip -d ghost ghost-latest.zip  
sudo rm ghost-latest.zip  

As a result a new folder ghost should appear insto /var/www/ folder.
Now Ghost is ready for installing. But before that let's create a user ghost which will be used for installing and running ghost

Create a ghost user

sudo adduser --shell /bin/bash --gecos 'User for ghost application' ghost  

By default the owner of /var/www/ folder is root. ghost folder also will have root user as owner. We need to change ghost's folder owner to the ghost user.

sudo chown -R ghost:ghost /var/www/ghost/  

Install Ghost

Now we are ready for installing ghost.

su - ghost  
cd /var/www/ghost  
npm install --production  

Configure Ghost

cp config.example.js config.js  

To have access to our blog from outside of our instance we need to change host property from 127.0.0.1 to 0.0.0.0 into configuration file. If we already have a domain name for our blog the url property also should be changed into production section. Configuration may look like this:

    production: {
        url: 'http://dev-pages.info',
        mail: {},
        database: {
            client: 'sqlite3',
            connection: {
                filename: path.join(__dirname, '/content/data/ghost.db')
            },
            debug: false
        },

        server: {
            host: '0.0.0.0',
            port: '2368'
        }
    },

Starting ghost

from /var/www/ghost folder run the following command under ghost user:

npm start --production  

Trouble shouting

In case when we get an error like
Killedt:iconv-lite → gunz most probable it's not enough RAM on VPS instance. To resolve this issue we need to increase memory for the instance or create a swap.

For creating a swap file on Ubuntu refer to this tutorial: How to add a swap file on Ubuntu 16.04