Set up ubuntu workstation for nodejs development

I have recently been engaged in a number of projects utilising nodejs and my primary development Operating System has been ubuntu Linux. I also have a number of laptops I use for software development purposes, which I frequently teardown and rebuild during the course of projects. I have a number of reasons for doing this, which relate to the fact that I treat my software development laptops or virtual machines like cattle rather than pets , although this phrase is usually associated with Cloud computing, I find it relates to a software developers laptop/workstation too, especially if you’re engaged in multiple projects.

 

 

During a typical software project, you may install a number of different libraries or DLL’s, Explore numerous different new applications, download any number of scripts or executables which on occasion even though the best possible precautions are taken may not always be from the most reliable of sources. When starting a new project I typically prefer to start with a clean slate, and that typically means a “clean” laptop. With all the technology at our disposal today it just doesn’t make sense to try use one machine for everything. Especially if that one machine that has the capability to run multiple virtual machines or docker containers etc.

 

There is a slight overhead involved in continually tearing down and rebuilding machines, but fortunately there technology also comes to our rescue here too. We can easily automate a lot of this process to reduce the friction to a minimum.

 

Typically I use my  self rolled script I use to prepare my ubuntu workstations for nodejs & Mongo development. The basics of this script can be extended to include things like setting up angular , Meteor, or any number of JavaScript libraries.

 

[ebs_panel style=”panel-info”] [ebs_panel-header] [ebs_icon type=”glyphicon glyphicon-download-alt” color=”#1e73be”] Download
[/ebs_panel-header] [ebs_panel-content] [wpdm_package id=’6063′] [/ebs_panel-content] [ebs_panel-footer] [ebs_icon type=”glyphicon glyphicon-question-sign” color=”#1e73be”] How to execute Shell Scripts
[/ebs_panel-footer] [/ebs_panel]

 

[ebs_panel style=”panel-info”] [ebs_panel-header] [ebs_icon type=”glyphicon glyphicon-info-sign” color=”#1e73be”] Update     09/11/2015
[/ebs_panel-header] [ebs_panel-content] To update the script to install Version 5 just update the line 12 to

curl --silent --location https://deb.nodesource.com/setup_5.x | sudo bash -
[/ebs_panel-content] [/ebs_panel]

 

#!/bin/bash

if [[ $EUID -ne 0 ]]; then
	echo "This script must be run as root" 1>&2
	exit 1
fi

echo "Update & upgrade"
apt-get update && apt-get upgrade -y && apt-get dist-upgrade -y
echo "Install Nodejs"
apt-get install curl
curl --silent --location https://deb.nodesource.com/setup_4.x | sudo bash -
sudo apt-get install --yes nodejs

echo"Install Mongo DB"
apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/10gen.list
apt-get update
apt-get install mongodb-org -y

echo "Install git"
apt-get update
apt-get install git -y

usage()
{
cat << EOF
usage: $0 [-d] [-v]

This script is intended to fix the common problem where npm users
are required to use sudo to install global packages.

It will backup a list of your installed packages remove all but npm,
then create a local directory, configure node to use this for global installs
whilst also fixing permissions on the .npm dir before, reinstalling the old packages.

OPTIONS:
   -h   Show this message
   -d   debug
   -v   Verbose
EOF
}


DEBUG=0
VERBOSE=0
while getopts "dv" OPTION
do
     case $OPTION in
         d)
             DEBUG=1
             ;;
         v)
             VERBOSE=1
             ;;
         ?)
             usage
             exit
             ;;
     esac
done

to_reinstall='/tmp/npm-reinstall.txt'

if [ 1 = ${VERBOSE} ];  then
    printf "\nSaving list of existing global npm packages\n"
fi

#Get a list of global packages (not deps)
#except for the npm package
#save in a temporary file.
npm -g list --depth=0 --parseable --long | cut -d: -f2 | grep -v '^npm@\|^$' >$to_reinstall

if [ 1 = ${VERBOSE} ];  then
    printf "\nRemoving existing packages temporarily - you might need your sudo password\n\n"
fi
#List the file
#replace the version numbers
#remove the newlines
#and pass to npm uninstall

uninstall='sudo npm -g uninstall'
if [ 1 = ${DEBUG} ];    then
    printf "Won't uninstall\n\n"
    uninstall='echo'
fi
if [ -s $to_reinstall ]; then
    cat $to_reinstall | sed -e 's/@.*//' | xargs $uninstall
fi

defaultnpmdir="${HOME}/.npm-packages"
npmdir=''

read -p "Choose your install directory. Default (${defaultnpmdir}) : " npmdir

if [ -z ${npmdir} ]; then
    npmdir=${defaultnpmdir}
else
    if [ ! -d ${npmdir} ]; then
        echo "${npmdir} is not a directory."
        exit
    fi
    npmdir="${npmdir}/.npm-packages"
fi

if [ 1 = ${VERBOSE} ];  then
    printf "\nMake a new directory ${npmdir} for our "-g" packages\n"
fi

if [ 0 = ${DEBUG} ];    then
    mkdir -p ${npmdir}
    npm config set prefix $npmdir
fi

if [ 1 = ${VERBOSE} ];  then
    printf "\nFix permissions on the .npm directories\n"
fi

me=`whoami`
sudo chown -R $me ~/.npm

if [ 1 = ${VERBOSE} ];  then
    printf "\nReinstall packages\n\n"
fi

#list the packages to install
#and pass to npm
install='npm -g install'
if [ 1 = ${DEBUG} ];    then
    install='echo'
fi
if [ -s $to_reinstall ]; then
    cat $to_reinstall | xargs $install
fi

envfix='
export NPM_PACKAGES="%s"
export NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
export PATH="$NPM_PACKAGES/bin:$PATH"
# Unset manpath so we can inherit from /etc/manpath via the `manpath`
# command
unset MANPATH  # delete if you already modified MANPATH elsewhere in your config
export MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
'

fix_env() {
    if [ -f "${HOME}/.bashrc" ];    then
        printf "${envfix}" ${npmdir} >> ~/.bashrc
        printf "\nDon't forget to run 'source ~/.bashrc'\n"
    fi
    if [ -f "${HOME}/.zshrc" ]; then
        printf "${envfix}" ${npmdir} >> ~/.zshrc
        printf "\nDon't forget to run 'source ~/.zshrc'\n"
    fi

}

echo_env() {
    printf "\nYou may need to add the following to your ~/.bashrc / .zshrc file(s)\n\n"
    printf "${envfix}\n\n" ${npmdir}
}

printf "\n\n"
read -p "Do you wish to update your .bashrc/.zshrc file(s) with the paths and manpaths? [yn] " yn
case $yn in
    [Yy]* ) fix_env;;
    [Nn]* ) echo_env;;
    * ) echo "Please answer 'y' or 'n'.";;
esac

rm $to_reinstall

printf "\nDone - current package list:\n\n"
npm -g list -depth=0

echo "Install yeoman generator"

npm install -g bower 
npm install -g grunt-cli 
npm install -g gulp

echo "install jasmine"
npm install -g jasmine

PHANTOM_VERSION="phantomjs-1.9.8"
ARCH=$(uname -m)

if ! [ $ARCH = "x86_64" ]; then
	$ARCH="i686"
fi

PHANTOM_JS="$PHANTOM_VERSION-linux-$ARCH"

apt-get update
apt-get install build-essential chrpath libssl-dev libxft-dev -y
apt-get install libfreetype6 libfreetype6-dev -y
apt-get install libfontconfig1 libfontconfig1-dev -y

cd ~
wget https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2
sudo tar xvjf $PHANTOM_JS.tar.bz2

sudo mv $PHANTOM_JS /usr/local/share
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin

 

Gary Woodfine

Freelance Full Stack Developer at threenine.co.uk
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.

Latest posts by Gary Woodfine (see all)