Skip to content

Instantly share code, notes, and snippets.

@cainmagi
Last active October 1, 2025 16:09
Show Gist options
  • Select an option

  • Save cainmagi/f028e8ac4b06c3deefaf8ec38d5a7d8f to your computer and use it in GitHub Desktop.

Select an option

Save cainmagi/f028e8ac4b06c3deefaf8ec38d5a7d8f to your computer and use it in GitHub Desktop.

Revisions

  1. cainmagi revised this gist Oct 1, 2025. 2 changed files with 16 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions Installation-for-NodeJS.md
    Original file line number Diff line number Diff line change
    @@ -46,6 +46,10 @@ Currently, we provide the following features:

    ## Changelogs

    ### 10/1/2025

    1. Let the script to attempt to install `corepack` if it does not exist and `npm` is available. It is used only when `yarn` is to be installed.

    ### 9/9/2024

    1. Upload the script.
    12 changes: 12 additions & 0 deletions install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -153,6 +153,15 @@ function install_nodejs {
    fi
    }

    function install_corepack {
    msg "Attempt to install corepack."
    if [ "x$(echo "${BASH_PREFIX} type npm 2>/dev/null" | bash --login -i)" != "x" ]; then
    echo "${BASH_PREFIX} npm install -g corepack" | bash --login -i || fail
    else
    msg_warn "Skip installation for Corepack because NPM is not found."
    fi
    }

    function install_yarn {
    msg "Update Yarn to the version: $1"
    echo "${BASH_PREFIX} yarn set version $1" | bash --login -i || fail
    @@ -318,6 +327,9 @@ fi
    # Case when installing Yarn is specified.
    if [ "x${SET_YARN}" = "xtrue" ]; then
    msg "${COLOR_OPT}--yarn): ${COLOR_OPT_TEXT}Install yarn."
    if ! nvm_exist_corepack; then
    install_corepack || fail
    fi
    if nvm_exist_corepack; then
    CUR_YARN_VER="$(yarn_version)"
    echo "${BASH_PREFIX} corepack enable" | bash --login -i || fail
  2. cainmagi revised this gist Sep 13, 2024. 1 changed file with 16 additions and 16 deletions.
    32 changes: 16 additions & 16 deletions install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -47,7 +47,7 @@ COLOR_OPT='\033[1;34m'
    COLOR_OPT_TEXT='\033[0;34m'

    DEBIAN_FRONTEND=noninteractive
    BASH_PREFIX="PS1=# && source ~/.bashrc &&"
    BASH_PREFIX="" # PS1=# && source ~/.bashrc &&


    function msg {
    @@ -84,7 +84,7 @@ function nvm_install_dir {
    if [ -n "$NVM_DIR" ]; then
    printf %s "${NVM_DIR}"
    else
    printf %s "$(echo "${BASH_PREFIX} echo \$NVM_DIR" | bash --login)"
    printf %s "$(echo "${BASH_PREFIX} echo \$NVM_DIR" | bash --login -i)"
    fi
    }

    @@ -98,15 +98,15 @@ function install_nvm {
    }

    function nvm_exist_nvm {
    if [ "x$(echo "${BASH_PREFIX} type nvm 2>/dev/null" | bash --login)" != "x" ]; then
    if [ "x$(echo "${BASH_PREFIX} type nvm 2>/dev/null" | bash --login -i)" != "x" ]; then
    true;
    else
    false;
    fi
    }

    function nvm_exist_corepack {
    if [ "x$(echo "${BASH_PREFIX} type corepack 2>/dev/null" | bash --login)" != "x" ]; then
    if [ "x$(echo "${BASH_PREFIX} type corepack 2>/dev/null" | bash --login -i)" != "x" ]; then
    true;
    else
    false;
    @@ -116,7 +116,7 @@ function nvm_exist_corepack {
    function nodejs_version {
    RUN_NVM_DIR="$(nvm_install_dir)" || fail
    if [ -d "$RUN_NVM_DIR" ]; then
    printf %s "$(echo "${BASH_PREFIX} nvm version" | bash --login)"
    printf %s "$(echo "${BASH_PREFIX} nvm version" | bash --login -i)"
    else
    printf %s ""
    fi
    @@ -125,8 +125,8 @@ function nodejs_version {
    function yarn_version {
    RUN_NVM_DIR="$(nvm_install_dir)" || fail
    if [ -d "$RUN_NVM_DIR" ]; then
    if [ "x$(echo "${BASH_PREFIX} type yarn 2>/dev/null" | bash --login)" != "x" ]; then
    printf %s "$(echo "${BASH_PREFIX} yarn --version" | bash --login)"
    if [ "x$(echo "${BASH_PREFIX} type yarn 2>/dev/null" | bash --login -i)" != "x" ]; then
    printf %s "$(echo "${BASH_PREFIX} yarn --version" | bash --login -i)"
    else
    printf %s ""
    fi
    @@ -141,12 +141,12 @@ function install_nodejs {
    if [ "x${RUN_NVM_DIR}" != "x" ]; then
    msg "Finding NVM in: ${RUN_NVM_DIR}".
    if [ "x$1" = "xlatest" ]; then
    echo "${BASH_PREFIX} nvm install --lts" | bash --login || fail
    echo "${BASH_PREFIX} nvm use --lts" | bash --login || fail
    echo "${BASH_PREFIX} nvm install --lts" | bash --login -i || fail
    echo "${BASH_PREFIX} nvm use --lts" | bash --login -i || fail
    else
    echo "${BASH_PREFIX} nvm install v$1" | bash --login || fail
    echo "${BASH_PREFIX} nvm alias default v$1" | bash --login || fail
    echo "${BASH_PREFIX} nvm use default" | bash --login || fail
    echo "${BASH_PREFIX} nvm install v$1" | bash --login -i || fail
    echo "${BASH_PREFIX} nvm alias default v$1" | bash --login -i || fail
    echo "${BASH_PREFIX} nvm use default" | bash --login -i || fail
    fi
    else
    msg_warn "Skip installation for Node.js because NVM is not found."
    @@ -155,8 +155,8 @@ function install_nodejs {

    function install_yarn {
    msg "Update Yarn to the version: $1"
    echo "${BASH_PREFIX} yarn set version $1" | bash --login || fail
    echo "${BASH_PREFIX} yarn install" | bash --login || fail
    echo "${BASH_PREFIX} yarn set version $1" | bash --login -i || fail
    echo "${BASH_PREFIX} yarn install" | bash --login -i || fail
    }


    @@ -320,8 +320,8 @@ if [ "x${SET_YARN}" = "xtrue" ]; then
    msg "${COLOR_OPT}--yarn): ${COLOR_OPT_TEXT}Install yarn."
    if nvm_exist_corepack; then
    CUR_YARN_VER="$(yarn_version)"
    echo "${BASH_PREFIX} corepack enable" | bash --login || fail
    echo "${BASH_PREFIX} corepack prepare yarn --activate" | bash --login || fail
    echo "${BASH_PREFIX} corepack enable" | bash --login -i || fail
    echo "${BASH_PREFIX} corepack prepare yarn --activate" | bash --login -i || fail
    if [ -s "package.json" ] && [ "$(pwd)" != "/" ]; then
    if [ "x${CUR_YARN_VER}" = "x" ]; then
    install_yarn ${VER_YARN} || fail
  3. cainmagi revised this gist Sep 13, 2024. 1 changed file with 16 additions and 15 deletions.
    31 changes: 16 additions & 15 deletions install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -47,6 +47,7 @@ COLOR_OPT='\033[1;34m'
    COLOR_OPT_TEXT='\033[0;34m'

    DEBIAN_FRONTEND=noninteractive
    BASH_PREFIX="PS1=# && source ~/.bashrc &&"


    function msg {
    @@ -83,7 +84,7 @@ function nvm_install_dir {
    if [ -n "$NVM_DIR" ]; then
    printf %s "${NVM_DIR}"
    else
    printf %s "$(echo "echo \$NVM_DIR" | bash --login)"
    printf %s "$(echo "${BASH_PREFIX} echo \$NVM_DIR" | bash --login)"
    fi
    }

    @@ -97,15 +98,15 @@ function install_nvm {
    }

    function nvm_exist_nvm {
    if [ "x$(echo 'type nvm 2>/dev/null' | bash --login)" != "x" ]; then
    if [ "x$(echo "${BASH_PREFIX} type nvm 2>/dev/null" | bash --login)" != "x" ]; then
    true;
    else
    false;
    fi
    }

    function nvm_exist_corepack {
    if [ "x$(echo 'type corepack 2>/dev/null' | bash --login)" != "x" ]; then
    if [ "x$(echo "${BASH_PREFIX} type corepack 2>/dev/null" | bash --login)" != "x" ]; then
    true;
    else
    false;
    @@ -115,7 +116,7 @@ function nvm_exist_corepack {
    function nodejs_version {
    RUN_NVM_DIR="$(nvm_install_dir)" || fail
    if [ -d "$RUN_NVM_DIR" ]; then
    printf %s "$(echo "nvm version" | /bin/bash --login)"
    printf %s "$(echo "${BASH_PREFIX} nvm version" | bash --login)"
    else
    printf %s ""
    fi
    @@ -124,8 +125,8 @@ function nodejs_version {
    function yarn_version {
    RUN_NVM_DIR="$(nvm_install_dir)" || fail
    if [ -d "$RUN_NVM_DIR" ]; then
    if [ "x$(echo 'type yarn 2>/dev/null' | bash --login)" != "x" ]; then
    printf %s "$(echo "yarn --version" | /bin/bash --login)"
    if [ "x$(echo "${BASH_PREFIX} type yarn 2>/dev/null" | bash --login)" != "x" ]; then
    printf %s "$(echo "${BASH_PREFIX} yarn --version" | bash --login)"
    else
    printf %s ""
    fi
    @@ -140,12 +141,12 @@ function install_nodejs {
    if [ "x${RUN_NVM_DIR}" != "x" ]; then
    msg "Finding NVM in: ${RUN_NVM_DIR}".
    if [ "x$1" = "xlatest" ]; then
    echo "nvm install --lts" | bash --login || fail
    echo "nvm use --lts" | bash --login || fail
    echo "${BASH_PREFIX} nvm install --lts" | bash --login || fail
    echo "${BASH_PREFIX} nvm use --lts" | bash --login || fail
    else
    echo "nvm install v$1" | bash --login || fail
    echo "nvm alias default v$1" | bash --login || fail
    echo "nvm use default" | bash --login || fail
    echo "${BASH_PREFIX} nvm install v$1" | bash --login || fail
    echo "${BASH_PREFIX} nvm alias default v$1" | bash --login || fail
    echo "${BASH_PREFIX} nvm use default" | bash --login || fail
    fi
    else
    msg_warn "Skip installation for Node.js because NVM is not found."
    @@ -154,8 +155,8 @@ function install_nodejs {

    function install_yarn {
    msg "Update Yarn to the version: $1"
    echo "yarn set version $1" | bash --login || fail
    echo "yarn install" | bash --login || fail
    echo "${BASH_PREFIX} yarn set version $1" | bash --login || fail
    echo "${BASH_PREFIX} yarn install" | bash --login || fail
    }


    @@ -319,8 +320,8 @@ if [ "x${SET_YARN}" = "xtrue" ]; then
    msg "${COLOR_OPT}--yarn): ${COLOR_OPT_TEXT}Install yarn."
    if nvm_exist_corepack; then
    CUR_YARN_VER="$(yarn_version)"
    echo "corepack enable" | bash --login || fail
    echo "corepack prepare yarn --activate" | bash --login || fail
    echo "${BASH_PREFIX} corepack enable" | bash --login || fail
    echo "${BASH_PREFIX} corepack prepare yarn --activate" | bash --login || fail
    if [ -s "package.json" ] && [ "$(pwd)" != "/" ]; then
    if [ "x${CUR_YARN_VER}" = "x" ]; then
    install_yarn ${VER_YARN} || fail
  4. cainmagi revised this gist Sep 13, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -92,7 +92,7 @@ function install_nvm {
    wget -O- https://raw.githubusercontent.com/nvm-sh/nvm/$1/install.sh | bash || fail
    RUN_NVM_DIR="$(nvm_install_dir)" || fail
    if [ "x${RUN_NVM_DIR}" != "x" ]; then
    ${SUDO} chmod ugo+rwx "${RUN_NVM_DIR}" || fail
    ${SUDO} chmod ugo+rwx "${RUN_NVM_DIR}" --recursive || fail
    fi
    }

  5. cainmagi revised this gist Sep 13, 2024. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -90,6 +90,10 @@ function nvm_install_dir {
    function install_nvm {
    msg "Installing NVM, version: $1"
    wget -O- https://raw.githubusercontent.com/nvm-sh/nvm/$1/install.sh | bash || fail
    RUN_NVM_DIR="$(nvm_install_dir)" || fail
    if [ "x${RUN_NVM_DIR}" != "x" ]; then
    ${SUDO} chmod ugo+rwx "${RUN_NVM_DIR}" || fail
    fi
    }

    function nvm_exist_nvm {
  6. cainmagi revised this gist Sep 9, 2024. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -315,7 +315,6 @@ if [ "x${SET_YARN}" = "xtrue" ]; then
    msg "${COLOR_OPT}--yarn): ${COLOR_OPT_TEXT}Install yarn."
    if nvm_exist_corepack; then
    CUR_YARN_VER="$(yarn_version)"
    echo $CUR_YARN_VER
    echo "corepack enable" | bash --login || fail
    echo "corepack prepare yarn --activate" | bash --login || fail
    if [ -s "package.json" ] && [ "$(pwd)" != "/" ]; then
  7. cainmagi revised this gist Sep 9, 2024. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -310,7 +310,6 @@ if [ "x${SET_NODEJS}" = "xtrue" ]; then
    fi


    # Case when installing Yarn is specified.
    # Case when installing Yarn is specified.
    if [ "x${SET_YARN}" = "xtrue" ]; then
    msg "${COLOR_OPT}--yarn): ${COLOR_OPT_TEXT}Install yarn."
  8. cainmagi revised this gist Sep 9, 2024. 1 changed file with 9 additions and 5 deletions.
    14 changes: 9 additions & 5 deletions install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -310,20 +310,24 @@ if [ "x${SET_NODEJS}" = "xtrue" ]; then
    fi


    # Case when installing Yarn is specified.
    # Case when installing Yarn is specified.
    if [ "x${SET_YARN}" = "xtrue" ]; then
    msg "${COLOR_OPT}--yarn): ${COLOR_OPT_TEXT}Install yarn."
    if nvm_exist_corepack; then
    CUR_YARN_VER="$(yarn_version)"
    echo $CUR_YARN_VER
    echo "corepack enable" | bash --login || fail
    echo "corepack prepare yarn --activate" | bash --login || fail
    if [ "x${CUR_YARN_VER}" = "x" ]; then
    install_yarn ${VER_YARN} || fail
    else
    if [ "x${VER_YARN}" != "xstable" ] && [ "x${CUR_YARN_VER}" != "x${VER_YARN}" ]; then
    if [ -s "package.json" ] && [ "$(pwd)" != "/" ]; then
    if [ "x${CUR_YARN_VER}" = "x" ]; then
    install_yarn ${VER_YARN} || fail
    else
    msg "Skip installation for Yarn because detect the version: ${CUR_YARN_VER}"
    if [ "x${VER_YARN}" != "xstable" ] && [ "x${CUR_YARN_VER}" != "x${VER_YARN}" ]; then
    install_yarn ${VER_YARN} || fail
    else
    msg "Skip installation for Yarn because detect the version: ${CUR_YARN_VER}"
    fi
    fi
    fi
    else
  9. cainmagi revised this gist Sep 9, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -314,9 +314,9 @@ fi
    if [ "x${SET_YARN}" = "xtrue" ]; then
    msg "${COLOR_OPT}--yarn): ${COLOR_OPT_TEXT}Install yarn."
    if nvm_exist_corepack; then
    CUR_YARN_VER="$(yarn_version)"
    echo "corepack enable" | bash --login || fail
    echo "corepack prepare yarn --activate" | bash --login || fail
    CUR_YARN_VER="$(yarn_version)"
    if [ "x${CUR_YARN_VER}" = "x" ]; then
    install_yarn ${VER_YARN} || fail
    else
  10. cainmagi revised this gist Sep 9, 2024. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions Installation-for-NodeJS.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ https://github.com/nvm-sh/nvm
    To use this script, you can run:

    ```bash
    wget -O- https://gist.github.com/cainmagi/?/raw/install-nodejs.sh | bash -s -- --all
    wget -O- https://gist.github.com/cainmagi/f028e8ac4b06c3deefaf8ec38d5a7d8f/raw/install-nodejs.sh | bash -s -- --all
    ```

    ## Usage
    @@ -17,31 +17,31 @@ Currently, we provide the following features:
    1. To install everything (Node.js and Yarn), run

    ```bash
    wget -O- https://gist.github.com/cainmagi/?/raw/install-nodejs.sh | bash -s -- --all
    wget -O- https://gist.github.com/cainmagi/f028e8ac4b06c3deefaf8ec38d5a7d8f/raw/install-nodejs.sh | bash -s -- --all
    ```

    2. To install Node.js only, run

    ```bash
    wget -O- https://gist.github.com/cainmagi/?/raw/install-nodejs.sh | bash -s -- --basic --nodejs
    wget -O- https://gist.github.com/cainmagi/f028e8ac4b06c3deefaf8ec38d5a7d8f/raw/install-nodejs.sh | bash -s -- --basic --nodejs
    ```

    3. If you have already installed Node.js, you can install Yarn directly by

    ```bash
    wget -O- https://gist.github.com/cainmagi/?/raw/install-nodejs.sh | bash -s -- --yarn
    wget -O- https://gist.github.com/cainmagi/f028e8ac4b06c3deefaf8ec38d5a7d8f/raw/install-nodejs.sh | bash -s -- --yarn
    ```

    4. When you need to use sudo for installing the packages, you can use

    ```bash
    wget -O- https://gist.github.com/cainmagi/?/raw/install-nodejs.sh | bash -s -- ... --sudofix
    wget -O- https://gist.github.com/cainmagi/f028e8ac4b06c3deefaf8ec38d5a7d8f/raw/install-nodejs.sh | bash -s -- ... --sudofix
    ```

    5. To change the version of the installed pacakges, use the following options:

    ```bash
    wget -O- https://gist.github.com/cainmagi/?/raw/install-nodejs.sh | bash -s -- --all nvm=<nvm-version> nodejs=<nodejs-version> yarn=<yarn-version>
    wget -O- https://gist.github.com/cainmagi/f028e8ac4b06c3deefaf8ec38d5a7d8f/raw/install-nodejs.sh | bash -s -- --all nvm=<nvm-version> nodejs=<nodejs-version> yarn=<yarn-version>
    ```

    ## Changelogs
  11. cainmagi created this gist Sep 9, 2024.
    51 changes: 51 additions & 0 deletions Installation-for-NodeJS.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    # Installation for Node.js

    This archive is a collections of required scripts for installing Node.js. The installation relies on NVM:

    https://github.com/nvm-sh/nvm

    To use this script, you can run:

    ```bash
    wget -O- https://gist.github.com/cainmagi/?/raw/install-nodejs.sh | bash -s -- --all
    ```

    ## Usage

    Currently, we provide the following features:

    1. To install everything (Node.js and Yarn), run

    ```bash
    wget -O- https://gist.github.com/cainmagi/?/raw/install-nodejs.sh | bash -s -- --all
    ```

    2. To install Node.js only, run

    ```bash
    wget -O- https://gist.github.com/cainmagi/?/raw/install-nodejs.sh | bash -s -- --basic --nodejs
    ```

    3. If you have already installed Node.js, you can install Yarn directly by

    ```bash
    wget -O- https://gist.github.com/cainmagi/?/raw/install-nodejs.sh | bash -s -- --yarn
    ```

    4. When you need to use sudo for installing the packages, you can use

    ```bash
    wget -O- https://gist.github.com/cainmagi/?/raw/install-nodejs.sh | bash -s -- ... --sudofix
    ```

    5. To change the version of the installed pacakges, use the following options:

    ```bash
    wget -O- https://gist.github.com/cainmagi/?/raw/install-nodejs.sh | bash -s -- --all nvm=<nvm-version> nodejs=<nodejs-version> yarn=<yarn-version>
    ```

    ## Changelogs

    ### 9/9/2024

    1. Upload the script.
    21 changes: 21 additions & 0 deletions LICENSE.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    MIT License

    Copyright (c) 2024 Yuchen Jin (cainmagi)

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
    338 changes: 338 additions & 0 deletions install-nodejs.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,338 @@
    #!/bin/bash

    # NVM/NPM/NodeJS/Yarn installation script
    # by Yuchen Jin (cainmagi) / 09092024
    # ---------------------------------------------------------
    # This script has been tested on
    # `ubuntu:focal` and `python:3.12-slim`
    # docker image.
    # ---------------------------------------------------------
    # Currently, it is designed for installing:
    # nvm, npm, nodejs, and yarn
    # The version can be specified by users.
    # ---------------------------------------------------------
    # For those users who want to install all latest tools,
    # please run:
    # ```bash
    # ./install-nodejs.sh --all
    # ```
    # ------------------------
    # NodeJS is installed by NVM and NPM. For those users who
    # do not want to install yarn, please run:
    # ```bash
    # ./install-nodejs.sh --basic --nodejs
    # ```
    # ------------------------
    # For installing a specific version, please run:
    # ```bash
    # ./install-nodejs.sh --all nvm=0.40.1 nodejs=20.17.0 yarn=4.4.1
    # ```
    # Note that the version will take effect only when the tool
    # will be installed. For example, if --yarn or --all is not
    # specified, yarn=? will not take effects.
    # ------------------------
    # For those users using root mode, please add the option
    # --sudofix like this::
    # ```bash
    # ./install-nodejs.sh ... --sudofix
    # ```

    # Make bashline configurations.
    set -e
    RESET='\033[0m'
    COLOR='\033[1;32m'
    COLOR_WARN='\033[1;33m'
    COLOR_ERR='\033[1;31m'
    COLOR_OPT='\033[1;34m'
    COLOR_OPT_TEXT='\033[0;34m'

    DEBIAN_FRONTEND=noninteractive


    function msg {
    echo -e "${COLOR}$(date): $1${RESET}"
    }

    function msg_warn {
    echo -e "${COLOR_WARN}$(date): $1${RESET}"
    }

    function msg_err {
    echo -e "${COLOR_ERR}$(date): $1${RESET}"
    }

    function fail {
    msg_err "Error : $?"
    exit 1
    }

    function mcd {
    mkdir -p "$1" || fail
    cd "$1" || fail
    }

    function nvm_has {
    type "$1" > /dev/null 2>&1
    }

    function nvm_default_install_dir {
    [ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm"
    }

    function nvm_install_dir {
    if [ -n "$NVM_DIR" ]; then
    printf %s "${NVM_DIR}"
    else
    printf %s "$(echo "echo \$NVM_DIR" | bash --login)"
    fi
    }

    function install_nvm {
    msg "Installing NVM, version: $1"
    wget -O- https://raw.githubusercontent.com/nvm-sh/nvm/$1/install.sh | bash || fail
    }

    function nvm_exist_nvm {
    if [ "x$(echo 'type nvm 2>/dev/null' | bash --login)" != "x" ]; then
    true;
    else
    false;
    fi
    }

    function nvm_exist_corepack {
    if [ "x$(echo 'type corepack 2>/dev/null' | bash --login)" != "x" ]; then
    true;
    else
    false;
    fi
    }

    function nodejs_version {
    RUN_NVM_DIR="$(nvm_install_dir)" || fail
    if [ -d "$RUN_NVM_DIR" ]; then
    printf %s "$(echo "nvm version" | /bin/bash --login)"
    else
    printf %s ""
    fi
    }

    function yarn_version {
    RUN_NVM_DIR="$(nvm_install_dir)" || fail
    if [ -d "$RUN_NVM_DIR" ]; then
    if [ "x$(echo 'type yarn 2>/dev/null' | bash --login)" != "x" ]; then
    printf %s "$(echo "yarn --version" | /bin/bash --login)"
    else
    printf %s ""
    fi
    else
    printf %s ""
    fi
    }

    function install_nodejs {
    msg "Installing NPM and Node.js, version: $1"
    RUN_NVM_DIR="$(nvm_install_dir)" || fail
    if [ "x${RUN_NVM_DIR}" != "x" ]; then
    msg "Finding NVM in: ${RUN_NVM_DIR}".
    if [ "x$1" = "xlatest" ]; then
    echo "nvm install --lts" | bash --login || fail
    echo "nvm use --lts" | bash --login || fail
    else
    echo "nvm install v$1" | bash --login || fail
    echo "nvm alias default v$1" | bash --login || fail
    echo "nvm use default" | bash --login || fail
    fi
    else
    msg_warn "Skip installation for Node.js because NVM is not found."
    fi
    }

    function install_yarn {
    msg "Update Yarn to the version: $1"
    echo "yarn set version $1" | bash --login || fail
    echo "yarn install" | bash --login || fail
    }


    SET_HELP=false
    SET_DEBUG=false
    SET_SUDOFIX=false
    SET_ALL=false
    SET_BASIC=false
    SET_NODEJS=false
    SET_YARN=false

    VER_NVM="latest"
    VER_NODEJS="latest"
    VER_YARN="latest"

    LIBRARY_PATH=${LIBRARY_PATH}:/usr/local/cuda/lib64/stubs
    LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/usr/local/cuda/compat/lib.real:/usr/local/cuda/compat/lib:/usr/local/nvidia/lib:/usr/local/nvidia/lib64:/usr/lib/x86_64-linux-gnu:/usr/local/cuda/lib64:/usr/local/cuda/nvvm/lib64:/usr/local/lib64:/usr/lib:/usr/local/lib:/lib:/lib/x86_64-linux-gnu:/lib/i386-linux-gnu"
    MKL_CBWR=AUTO


    # Pass options from command line
    for ARGUMENT in "$@"
    do
    KEY=$(echo $ARGUMENT | cut -f1 -d=)
    if [[ $KEY != '--*' ]]
    then
    VALUE=$(echo $ARGUMENT | cut -f2 -d=)
    fi
    case "$KEY" in
    --help) SET_HELP=true ;;
    --debug) SET_DEBUG=true ;;
    --sudofix) SET_SUDOFIX=true ;;
    --all) SET_ALL=true ;;
    --basic) SET_BASIC=true ;;
    --nodejs) SET_NODEJS=true ;;
    --yarn) SET_YARN=true ;;
    nvm) VER_NVM=${VALUE} ;;
    nodejs) VER_NODEJS=${VALUE} ;;
    yarn) VER_YARN=${VALUE} ;;
    *)
    esac
    done


    # Help mode.
    if [ "x${SET_HELP}" = "xtrue" ]
    then
    THIS_NAME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
    HELP_MESSAGE="${COLOR}NodeJS/Yarn installation script - by Yuchen Jin (cainmagi) / 09092024${RESET}
    Use as \"${COLOR_OPT}bash ${THIS_NAME} --flag1 --flag2 ... option1=val1 option2=val2 ...${RESET}\"
    where the usages are:
    ${COLOR_OPT}--all${RESET}:
    Run the full script, installing both nodejs and yarn.
    ${COLOR_OPT}--basic${RESET}:
    Install the basic dependencies by apt.
    ${COLOR_OPT}--nodejs${RESET}:
    Install NodeJS.
    ${COLOR_OPT}--yarn${RESET}:
    Install Yarn. This flag will not take effect if --nodejs is not specified.
    ${COLOR_OPT}--sudofix${RESET} [not included by --all]:
    Install sudo (should be specified only when using root mode).
    ${COLOR_OPT}--help${RESET}:
    Display the help message.
    ${COLOR_OPT} nvm=${COLOR_OPT_TEXT}<version>${RESET}:
    Configure the version of the NVM to be installed. NVM is required by NodeJS.
    ${COLOR_OPT_TEXT}Current value${RESET}: ${VER_NODEJS}
    ${COLOR_OPT}nodejs=${COLOR_OPT_TEXT}<version>${RESET}:
    Configure the version of the NodeJS to be installed.
    ${COLOR_OPT_TEXT}Current value${RESET}: ${VER_NODEJS}
    ${COLOR_OPT} yarn=${COLOR_OPT_TEXT}<version>${RESET}:
    Configure the version of the Yarn to be installed.
    ${COLOR_OPT_TEXT}Current value${RESET}: ${VER_YARN}
    "
    echo -e "$HELP_MESSAGE"
    exit 1
    fi


    # Configure the options when {flag:SET_ALL} has been configured.
    if [ "x${SET_ALL}" = "xtrue" ]
    then
    msg "${COLOR_ERR}--all): ${COLOR_OPT_TEXT}Install all tools."
    SET_BASIC=true
    # SET_SUDOFIX=true # should not be specified when not using root mode.
    SET_NODEJS=true
    SET_YARN=true
    fi


    # If run with root and sudo is not installed
    if [ "x${SET_SUDOFIX}" = "xtrue" ]; then
    msg "${COLOR_OPT}--sudofix): ${COLOR_OPT_TEXT}Install sudo (only used in root mode)."
    apt-get update -qq || fail
    apt-get -y install sudo || fail
    fi


    # Determine whether sudo should be used.
    SUDO=""
    if nvm_has "sudo"; then
    SUDO="sudo"
    fi


    # Install apt dependencies
    if [ "x${SET_BASIC}" = "xtrue" ]; then
    msg "${COLOR_OPT}--basic): ${COLOR_OPT_TEXT}Install apt packages."
    ${SUDO} apt-get update -qq || fail
    ${SUDO} apt-get -y full-upgrade || fail
    ${SUDO} apt-get -y install wget jq tar xz-utils || fail

    ${SUDO} apt-get update -qq || fail
    ${SUDO} apt-get -y upgrade || fail
    ${SUDO} apt-get -y dist-upgrade || fail
    ${SUDO} apt-get -y autoremove || fail
    ${SUDO} apt-get -y autoclean || fail
    fi


    # Fetch the latest version
    if [ "x${VER_NVM}" = "x" ] || [ "x${VER_NVM}" = "xlatest" ]; then
    VER_NVM=$(wget -qO- https://api.github.com/repos/nvm-sh/nvm/releases/latest | jq .name -r)
    fi
    if [ "x${VER_NODEJS}" = "x" ] || [ "x${VER_NODEJS}" = "xlatest" ]; then
    VER_NODEJS="latest"
    fi
    if [ "x${VER_YARN}" = "x" ] || [ "x${VER_YARN}" = "xlatest" ]; then
    VER_YARN="stable"
    fi


    # Case when installing NodeJS is specified.
    if [ "x${SET_NODEJS}" = "xtrue" ]; then
    msg "${COLOR_OPT}--nodejs): ${COLOR_OPT_TEXT}Install nvm, npm, and nodejs."
    # Install NVM
    if ! nvm_has nvm; then
    if ! nvm_exist_nvm; then
    install_nvm ${VER_NVM} || fail
    else
    msg "Skip installation for NVM because NVM is detected."
    fi
    else
    msg "Skip installation for NVM because NVM is detected."
    fi
    # Install NodeJS
    CUR_NODEJS_VER="$(nodejs_version)"
    if [ "x${CUR_NODEJS_VER}" = "x" ] || [ "x${CUR_NODEJS_VER}" = "xnone" ] || [ "x${CUR_NODEJS_VER}" = "xN/A" ]; then
    install_nodejs ${VER_NODEJS} || fail
    else
    if [ "x${VER_NODEJS}" != "xlatest" ] && [ "x${CUR_NODEJS_VER}" != "xv${VER_NODEJS}" ]; then
    install_nodejs ${VER_NODEJS} || fail
    else
    msg "Skip installation for NodeJS because detect the version: ${CUR_NODEJS_VER}"
    fi
    fi
    fi


    # Case when installing Yarn is specified.
    if [ "x${SET_YARN}" = "xtrue" ]; then
    msg "${COLOR_OPT}--yarn): ${COLOR_OPT_TEXT}Install yarn."
    if nvm_exist_corepack; then
    echo "corepack enable" | bash --login || fail
    echo "corepack prepare yarn --activate" | bash --login || fail
    CUR_YARN_VER="$(yarn_version)"
    if [ "x${CUR_YARN_VER}" = "x" ]; then
    install_yarn ${VER_YARN} || fail
    else
    if [ "x${VER_YARN}" != "xstable" ] && [ "x${CUR_YARN_VER}" != "x${VER_YARN}" ]; then
    install_yarn ${VER_YARN} || fail
    else
    msg "Skip installation for Yarn because detect the version: ${CUR_YARN_VER}"
    fi
    fi
    else
    msg_warn "Cannot install Yarn if NodeJS with Corepack is not installed."
    fi
    fi


    msg "Installation finished."
    if [ "x${SET_NODEJS}" = "xtrue" ]; then
    msg "If you have newly installed NodeJS, rememeber to restart bash to make it take effects."
    fi