Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kolszewskieu/e8efc3dc3c217905b9a6bde95110d57d to your computer and use it in GitHub Desktop.
Save kolszewskieu/e8efc3dc3c217905b9a6bde95110d57d to your computer and use it in GitHub Desktop.
Install node.js on fedora (23+).(v5.10.1)
#!/usr/bin/env bash
#
# NodeJS Installer based on https://gist.github.com/trajakovic/ad9f91776dea3b495db0
#
# Added version option and distinct commands for install, uninstall, path setting,
# and removepath setting.
#
# https://gist.github.com/bnielsen1965
#
# THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
# EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
# PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
# PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
# NECESSARY SERVICING, REPAIR OR CORRECTION.
#
# default parameters
NODEJS_DIST_URI="https://nodejs.org/dist"
PATH_HEADER="#### added by fedora-install-nodejs - DO NOT EDIT ####"
PATH_FOOTER="#### fedora-install-nodejs end - DO NOT EDIT ####"
INSTALL_PATH="/usr/lib"
INSTALL_COMMAND="install"
NODE_VERSION="v5.10.1"
PACKAGE=`basename $0`
# display usage help
function Usage()
{
cat <<-ENDOFMESSAGE
$PACKAGE - Install a specific nodejs version from ${NODEJS_DIST_URI}.
Run as root to install or uninstall specific versions of nodejs.
Run as a normal user to set or remove the user's PATH settings.
$PACKAGE [command] [options]
arguments:
command - the command to execute, i|install (default), u|uninstall, p|path, r|removepath
options:
-h, --help show brief help
-v, --version NODE_VERSION the version to install, i.e. v4.4.7
NOTE: This command must be executed as root. The command will fail if the nodejs package is installed.
ENDOFMESSAGE
exit
}
# die with message
function Die()
{
echo "$* Use -h option to display help."
exit 1
}
# process command line arguments into values to use
function ProcessArguments() {
# separate options from arguments
while [ $# -gt 0 ]
do
opt=$1
shift
case ${opt} in
-v|--version)
if [ $# -eq 0 -o "${1:0:1}" = "-" ]; then
Die "The ${opt} option requires an version number, i.e. -v v4.4.7."
fi
export NODE_VERSION=$1
shift
;;
-h|--help)
Usage;;
*)
if [ "${opt:0:1}" = "-" ]; then
Die "${opt}: unknown option."
fi
ARGV+=(${opt});;
esac
done
if [ ${#ARGV[@]} -gt 0 ]; then
export INSTALL_COMMAND=${ARGV[0]}
fi
}
# build environment
function BuildEnvironment()
{
export NODE_INT_VERSION=`echo $NODE_VERSION | sed 's/[\.v]//g'`
export NODE_FILE=node-${NODE_VERSION}-linux-x64.tar.gz
export NODE_FOLDER=node-${NODE_VERSION}-linux-x64
export NODE_URI="${NODEJS_DIST_URI}/${NODE_VERSION}/${NODE_FILE}"
}
# check that user is root
function IsRoot() {
if [ ! $( id -u ) -eq 0 ]; then
Die "$0 Must be run as root."
fi
}
# check package is installed
function IsInstalled() {
if dnf list installed "$@" >/dev/null 2>&1; then
true
else
false
fi
}
# install nodejs
function InstallNodeJS() {
IsRoot
if IsInstalled "nodejs"; then Die "The nodejs package is installed! It must be removed first."; fi
cd $INSTALL_PATH
if [[ -e "${NODE_FOLDER}" ]]; then Die "Looks like this nodejs version (${NODE_FOLDER}) is already installed."; fi
if ! IsInstalled "gcc-c++"; then
# install DevTools for NPM
echo "Installing Development Tools for NPM"
dnf groupinstall -y 'Development Tools'
dnf install -y gcc-c++
fi
# get node
wget ${NODE_URI}
# extract
tar xvf ${NODE_FILE}
# remove tar
rm ${NODE_FILE} -f
# install alternatives
update-alternatives --install /usr/bin/node node /usr/lib/${NODE_FOLDER}/bin/node ${NODE_INT_VERSION}
update-alternatives --install /usr/bin/npm npm /usr/lib/${NODE_FOLDER}/bin/npm ${NODE_INT_VERSION}
echo "Install complete. Update user PATH value by running the following command as the user: $0 -v ${NODE_VERSION} path"
}
# uninstall nodejs
function UninstallNodeJS() {
IsRoot
cd $INSTALL_PATH
if [[ ! -e "${NODE_FOLDER}" ]]; then Die "This version of nodejs (${NODE_FOLDER}) is not installed."; fi
# remove install
rm -r ${NODE_FOLDER};
update-alternatives --remove node /usr/lib/${NODE_FOLDER}/bin/node
update-alternatives --remove npm /usr/lib/${NODE_FOLDER}/bin/npm
echo "Uninstall complete. Update user PATH value by running the following command as the user: $0 -v ${NODE_VERSION} removepath"
}
# update $PATH to make `npm install -g` visible
function UpdatePath() {
if [[ ! -e "${INSTALL_PATH}/${NODE_FOLDER}" ]]; then Die "This version of nodejs (${NODE_FOLDER}) is not installed."; fi
# remove any pre-existing path setting
RemovePath
# update $PATH to make `npm install -g` visible
cat <<EOF >> "${HOME}/.bashrc"
$PATH_HEADER
export PATH=\$PATH:/usr/lib/${NODE_FOLDER}/bin
$PATH_FOOTER
EOF
}
# remove $PATH from bashrc
function RemovePath() {
# remove any pre-existing path setting
RemoveEntry "${PATH_HEADER}" "${PATH_FOOTER}" "${HOME}/.bashrc"
}
# remove an entry with the specified header and footer from the specified file
function RemoveEntry(){ # $1 -> Header, $2 -> Footer , $3 -> Name of file to edit
sed -i.bak "/$1/,/$2/d" "$3"
}
# prepare to execute command
ProcessArguments $*
BuildEnvironment
# process command
case ${INSTALL_COMMAND} in
i|install)
InstallNodeJS
;;
u|uninstall)
UninstallNodeJS
;;
p|path)
UpdatePath
;;
r|removepath)
RemovePath
;;
*)
Die "${INSTALL_COMMAND} is an unknown command."
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment