Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bnielsen1965/722e8883c3b373d9c783e6b85e659a5f to your computer and use it in GitHub Desktop.
Save bnielsen1965/722e8883c3b373d9c783e6b85e659a5f to your computer and use it in GitHub Desktop.

Revisions

  1. bnielsen1965 revised this gist Apr 28, 2017. 1 changed file with 218 additions and 40 deletions.
    258 changes: 218 additions & 40 deletions fedora-install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -1,45 +1,223 @@
    #!/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.
    #

    func_check_for_root() {
    if [ ! $( id -u ) -eq 0 ]; then
    echo "ERROR: $0 Must be run as root, Script terminating" ;exit 7
    fi
    }
    func_check_for_root

    #SETUP PARAMS
    # 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"
    NODE_INT_VERSION="5101"
    NODE_FILE=node-${NODE_VERSION}-linux-x64.tar.gz
    NODE_FOLDER=node-${NODE_VERSION}-linux-x64
    NODE_URI="https://nodejs.org/dist/${NODE_VERSION}/${NODE_FILE}"

    cd /usr/lib

    if [[ ! -e "${NODE_FOLDER}" ]]; then
    #install DevTools for NPM
    dnf groupinstall -y 'Development Tools'
    dnf install -y gcc-c++

    # 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}

    # update $PATH to make `npm install -g` visible
    cat <<EOF >> /home/${SUDO_USER:-$USER}/.bashrc
    # added by https://gist.github.com/trajakovic/ad9f91776dea3b495db0 - NodeJs 'installer'


    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
    else
    echo Looks like node is already installed:
    echo ""
    update-alternatives --display node
    fi
    }


    # 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
  2. @trajakovic trajakovic revised this gist Apr 19, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fedora-install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,7 @@ if [[ ! -e "${NODE_FOLDER}" ]]; then
    update-alternatives --install /usr/bin/npm npm /usr/lib/${NODE_FOLDER}/bin/npm ${NODE_INT_VERSION}

    # update $PATH to make `npm install -g` visible
    cat <<EOF >> /home/${SUDO_USER:-$USER}/.bash_profile
    cat <<EOF >> /home/${SUDO_USER:-$USER}/.bashrc
    # added by https://gist.github.com/trajakovic/ad9f91776dea3b495db0 - NodeJs 'installer'
    export PATH=\$PATH:/usr/lib/${NODE_FOLDER}/bin
  3. @trajakovic trajakovic revised this gist Apr 19, 2016. No changes.
  4. @trajakovic trajakovic revised this gist Apr 19, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions fedora-install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -8,8 +8,8 @@ func_check_for_root() {
    func_check_for_root

    #SETUP PARAMS
    NODE_VERSION="v5.7.0"
    NODE_INT_VERSION="570"
    NODE_VERSION="v5.10.1"
    NODE_INT_VERSION="5101"
    NODE_FILE=node-${NODE_VERSION}-linux-x64.tar.gz
    NODE_FOLDER=node-${NODE_VERSION}-linux-x64
    NODE_URI="https://nodejs.org/dist/${NODE_VERSION}/${NODE_FILE}"
  5. @trajakovic trajakovic revised this gist Feb 26, 2016. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions fedora-install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,13 @@ if [[ ! -e "${NODE_FOLDER}" ]]; then
    # 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}

    # update $PATH to make `npm install -g` visible
    cat <<EOF >> /home/${SUDO_USER:-$USER}/.bash_profile
    # added by https://gist.github.com/trajakovic/ad9f91776dea3b495db0 - NodeJs 'installer'
    export PATH=\$PATH:/usr/lib/${NODE_FOLDER}/bin
    EOF
    else
    echo Looks like node is already installed:
    echo ""
  6. @trajakovic trajakovic revised this gist Feb 26, 2016. No changes.
  7. @trajakovic trajakovic revised this gist Feb 26, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions fedora-install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -8,8 +8,8 @@ func_check_for_root() {
    func_check_for_root

    #SETUP PARAMS
    NODE_VERSION="v5.0.0"
    NODE_INT_VERSION="500"
    NODE_VERSION="v5.7.0"
    NODE_INT_VERSION="570"
    NODE_FILE=node-${NODE_VERSION}-linux-x64.tar.gz
    NODE_FOLDER=node-${NODE_VERSION}-linux-x64
    NODE_URI="https://nodejs.org/dist/${NODE_VERSION}/${NODE_FILE}"
  8. @trajakovic trajakovic revised this gist Nov 9, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fedora-install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ cd /usr/lib
    if [[ ! -e "${NODE_FOLDER}" ]]; then
    #install DevTools for NPM
    dnf groupinstall -y 'Development Tools'
    dnf install -qy gcc-c++
    dnf install -y gcc-c++

    # get node
    wget ${NODE_URI}
  9. @trajakovic trajakovic revised this gist Nov 8, 2015. No changes.
  10. @trajakovic trajakovic revised this gist Nov 2, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fedora-install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ cd /usr/lib

    if [[ ! -e "${NODE_FOLDER}" ]]; then
    #install DevTools for NPM
    dnf groupinstall -qy 'Development Tools'
    dnf groupinstall -y 'Development Tools'
    dnf install -qy gcc-c++

    # get node
  11. @trajakovic trajakovic revised this gist Nov 2, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions fedora-install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -8,8 +8,8 @@ func_check_for_root() {
    func_check_for_root

    #SETUP PARAMS
    NODE_VERSION="v4.1.1"
    NODE_INT_VERSION="411"
    NODE_VERSION="v5.0.0"
    NODE_INT_VERSION="500"
    NODE_FILE=node-${NODE_VERSION}-linux-x64.tar.gz
    NODE_FOLDER=node-${NODE_VERSION}-linux-x64
    NODE_URI="https://nodejs.org/dist/${NODE_VERSION}/${NODE_FILE}"
  12. @trajakovic trajakovic revised this gist Oct 30, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions fedora-install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,7 @@ cd /usr/lib
    if [[ ! -e "${NODE_FOLDER}" ]]; then
    #install DevTools for NPM
    dnf groupinstall -qy 'Development Tools'
    dnf install -qy gcc-c++

    # get node
    wget ${NODE_URI}
  13. @trajakovic trajakovic revised this gist Oct 2, 2015. No changes.
  14. @trajakovic trajakovic revised this gist Oct 2, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion fedora-install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -31,5 +31,7 @@ if [[ ! -e "${NODE_FOLDER}" ]]; then
    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}
    else
    echo Looks like node is already installed
    echo Looks like node is already installed:
    echo ""
    update-alternatives --display node
    fi
  15. @trajakovic trajakovic created this gist Oct 2, 2015.
    35 changes: 35 additions & 0 deletions fedora-install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!/usr/bin/env bash

    func_check_for_root() {
    if [ ! $( id -u ) -eq 0 ]; then
    echo "ERROR: $0 Must be run as root, Script terminating" ;exit 7
    fi
    }
    func_check_for_root

    #SETUP PARAMS
    NODE_VERSION="v4.1.1"
    NODE_INT_VERSION="411"
    NODE_FILE=node-${NODE_VERSION}-linux-x64.tar.gz
    NODE_FOLDER=node-${NODE_VERSION}-linux-x64
    NODE_URI="https://nodejs.org/dist/${NODE_VERSION}/${NODE_FILE}"

    cd /usr/lib

    if [[ ! -e "${NODE_FOLDER}" ]]; then
    #install DevTools for NPM
    dnf groupinstall -qy 'Development Tools'

    # 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}
    else
    echo Looks like node is already installed
    fi