1+ #! /bin/bash
2+ # -------------------------------------------------------------------------------------------------------------
3+ # Copyright (c) Microsoft Corporation. All rights reserved.
4+ # Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
5+ # -------------------------------------------------------------------------------------------------------------
6+ #
7+ # Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/node.md
8+ # Maintainer: The VS Code and Codespaces Teams
9+ #
10+ # Syntax: ./node-debian.sh [directory to install nvm] [node version to install (use "none" to skip)] [non-root user] [Update rc files flag] [install node-gyp deps]
11+
12+ export NVM_DIR=${1:- " /usr/local/share/nvm" }
13+ export NODE_VERSION=${2:- " lts" }
14+ USERNAME=${3:- " automatic" }
15+ UPDATE_RC=${4:- " true" }
16+ INSTALL_TOOLS_FOR_NODE_GYP=" ${5:- true} "
17+ export NVM_VERSION=" 0.38.0"
18+
19+ set -e
20+
21+ if [ " $( id -u) " -ne 0 ]; then
22+ echo -e ' Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
23+ exit 1
24+ fi
25+
26+ # Ensure that login shells get the correct path if the user updated the PATH using ENV.
27+ rm -f /etc/profile.d/00-restore-env.sh
28+ echo " export PATH=${PATH// $(sh -lc ' echo $PATH' )/ \$ PATH} " > /etc/profile.d/00-restore-env.sh
29+ chmod +x /etc/profile.d/00-restore-env.sh
30+
31+ # Determine the appropriate non-root user
32+ if [ " ${USERNAME} " = " auto" ] || [ " ${USERNAME} " = " automatic" ]; then
33+ USERNAME=" "
34+ POSSIBLE_USERS=(" vscode" " node" " codespace" " $( awk -v val=1000 -F " :" ' $3==val{print $1}' /etc/passwd) " )
35+ for CURRENT_USER in ${POSSIBLE_USERS[@]} ; do
36+ if id -u ${CURRENT_USER} > /dev/null 2>&1 ; then
37+ USERNAME=${CURRENT_USER}
38+ break
39+ fi
40+ done
41+ if [ " ${USERNAME} " = " " ]; then
42+ USERNAME=root
43+ fi
44+ elif [ " ${USERNAME} " = " none" ] || ! id -u ${USERNAME} > /dev/null 2>&1 ; then
45+ USERNAME=root
46+ fi
47+
48+ updaterc () {
49+ if [ " ${UPDATE_RC} " = " true" ]; then
50+ echo " Updating /etc/bash.bashrc and /etc/zsh/zshrc..."
51+ if [[ " $( cat /etc/bash.bashrc) " != * " $1 " * ]]; then
52+ echo -e " $1 " >> /etc/bash.bashrc
53+ fi
54+ if [ -f " /etc/zsh/zshrc" ] && [[ " $( cat /etc/zsh/zshrc) " != * " $1 " * ]]; then
55+ echo -e " $1 " >> /etc/zsh/zshrc
56+ fi
57+ fi
58+ }
59+
60+ # Function to run apt-get if needed
61+ apt_get_update_if_needed ()
62+ {
63+ if [ ! -d " /var/lib/apt/lists" ] || [ " $( ls /var/lib/apt/lists/ | wc -l) " = " 0" ]; then
64+ echo " Running apt-get update..."
65+ apt-get update
66+ else
67+ echo " Skipping apt-get update."
68+ fi
69+ }
70+
71+ # Checks if packages are installed and installs them if not
72+ check_packages () {
73+ if ! dpkg -s " $@ " > /dev/null 2>&1 ; then
74+ apt_get_update_if_needed
75+ apt-get -y install --no-install-recommends " $@ "
76+ fi
77+ }
78+
79+ # Ensure apt is in non-interactive to avoid prompts
80+ export DEBIAN_FRONTEND=noninteractive
81+
82+ # Install dependencies
83+ check_packages apt-transport-https curl ca-certificates tar gnupg2 dirmngr
84+
85+ # Install yarn
86+ if type yarn > /dev/null 2>&1 ; then
87+ echo " Yarn already installed."
88+ else
89+ # Import key safely (new method rather than deprecated apt-key approach) and install
90+ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor > /usr/share/keyrings/yarn-archive-keyring.gpg
91+ echo " deb [arch=$( dpkg --print-architecture) signed-by=/usr/share/keyrings/yarn-archive-keyring.gpg] https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list
92+ apt-get update
93+ apt-get -y install --no-install-recommends yarn
94+ fi
95+
96+ # Adjust node version if required
97+ if [ " ${NODE_VERSION} " = " none" ]; then
98+ export NODE_VERSION=
99+ elif [ " ${NODE_VERSION} " = " lts" ]; then
100+ export NODE_VERSION=" lts/*"
101+ fi
102+
103+ # Create a symlink to the installed version for use in Dockerfile PATH statements
104+ export NVM_SYMLINK_CURRENT=true
105+
106+ # Install the specified node version if NVM directory already exists, then exit
107+ if [ -d " ${NVM_DIR} " ]; then
108+ echo " NVM already installed."
109+ if [ " ${NODE_VERSION} " != " " ]; then
110+ su ${USERNAME} -c " . $NVM_DIR /nvm.sh && nvm install ${NODE_VERSION} && nvm clear-cache"
111+ fi
112+ exit 0
113+ fi
114+
115+ # Create nvm group, nvm dir, and set sticky bit
116+ if ! cat /etc/group | grep -e " ^nvm:" > /dev/null 2>&1 ; then
117+ groupadd -r nvm
118+ fi
119+ umask 0002
120+ usermod -a -G nvm ${USERNAME}
121+ mkdir -p ${NVM_DIR}
122+ chown :nvm ${NVM_DIR}
123+ chmod g+s ${NVM_DIR}
124+ su ${USERNAME} -c " $( cat << EOF
125+ set -e
126+ umask 0002
127+ # Do not update profile - we'll do this manually
128+ export PROFILE=/dev/null
129+ ls -lah /home/${USERNAME} /.nvs || :
130+ curl -so- https://raw.githubusercontent.com/nvm-sh/nvm/v${NVM_VERSION} /install.sh | bash
131+ source ${NVM_DIR} /nvm.sh
132+ if [ "${NODE_VERSION} " != "" ]; then
133+ nvm alias default ${NODE_VERSION}
134+ fi
135+ nvm clear-cache
136+ EOF
137+ ) " 2>&1
138+ # Update rc files
139+ if [ " ${UPDATE_RC} " = " true" ]; then
140+ updaterc " $( cat << EOF
141+ export NVM_DIR="${NVM_DIR} "
142+ [ -s "\$ NVM_DIR/nvm.sh" ] && . "\$ NVM_DIR/nvm.sh"
143+ [ -s "\$ NVM_DIR/bash_completion" ] && . "\$ NVM_DIR/bash_completion"
144+ EOF
145+ ) "
146+ fi
147+
148+ # If enabled, verify "python3", "make", "gcc", "g++" commands are available so node-gyp works - https://github.com/nodejs/node-gyp
149+ if [ " ${INSTALL_TOOLS_FOR_NODE_GYP} " = " true" ]; then
150+ echo " Verifying node-gyp OS requirements..."
151+ to_install=" "
152+ if ! type make > /dev/null 2>&1 ; then
153+ to_install=" ${to_install} make"
154+ fi
155+ if ! type gcc > /dev/null 2>&1 ; then
156+ to_install=" ${to_install} gcc"
157+ fi
158+ if ! type g++ > /dev/null 2>&1 ; then
159+ to_install=" ${to_install} g++"
160+ fi
161+ if ! type python3 > /dev/null 2>&1 ; then
162+ to_install=" ${to_install} python3-minimal"
163+ fi
164+ if [ ! -z " ${to_install} " ]; then
165+ apt_get_update_if_needed
166+ apt-get -y install ${to_install}
167+ fi
168+ fi
169+
170+ echo " Done!"
0 commit comments