@@ -44,14 +44,11 @@ error() {
44
44
printf " \e[31m[ERROR]\e[0m %s\n" " $1 " >&2
45
45
}
46
46
47
- # Function to check if the script is run as root
48
- check_root () {
49
- if [[ " $EUID " -ne 0 ]]; then
50
- error " This script must be run with sudo or as root."
51
- exit 1
52
- fi
47
+ is_package_installed () {
48
+ dpkg -s " $1 " & > /dev/null
53
49
}
54
50
51
+
55
52
# Function to check if the operating system is Ubuntu
56
53
check_os () {
57
54
if [[ -f /etc/os-release ]]; then
@@ -77,8 +74,8 @@ check_os() {
77
74
update_system () {
78
75
info " Updating and upgrading the system packages..."
79
76
{
80
- apt update -y
81
- apt upgrade -y
77
+ sudo apt update -y
78
+ sudo apt upgrade -y
82
79
} >> " $LOG_FILE " 2>&1
83
80
success " System packages updated and upgraded successfully."
84
81
}
@@ -99,7 +96,7 @@ install_packages() {
99
96
100
97
info " Installing essential packages: ${packages[*]} ..."
101
98
{
102
- apt install -y " ${packages[@]} "
99
+ sudo apt install -y " ${packages[@]} "
103
100
} >> " $LOG_FILE " 2>&1
104
101
success " Essential packages installed successfully."
105
102
}
@@ -108,7 +105,7 @@ install_packages() {
108
105
install_gpu_drivers () {
109
106
info " Detecting and installing appropriate GPU drivers..."
110
107
{
111
- ubuntu-drivers install
108
+ sudo ubuntu-drivers install
112
109
} >> " $LOG_FILE " 2>&1
113
110
success " GPU drivers installed successfully."
114
111
}
@@ -136,7 +133,7 @@ install_rust() {
136
133
137
134
# Function to install CUDA Toolkit
138
135
install_cuda () {
139
- if dpkg -l | grep -q " ^ii cuda-toolkit" ; then
136
+ if is_package_installed " cuda-toolkit" ; then
140
137
info " CUDA Toolkit is already installed. Skipping CUDA installation."
141
138
else
142
139
info " Installing CUDA Toolkit and dependencies..."
@@ -145,10 +142,10 @@ install_cuda() {
145
142
distribution=$( grep ' ^ID=' /etc/os-release | cut -d' =' -f2 | tr -d ' "' ) $( grep ' ^VERSION_ID=' /etc/os-release | cut -d' =' -f2 | tr -d ' "' | tr -d ' \.' )
146
143
info " Installing Nvidia CUDA keyring and repo"
147
144
wget https://developer.download.nvidia.com/compute/cuda/repos/$distribution /$( /usr/bin/uname -m) /cuda-keyring_1.1-1_all.deb
148
- dpkg -i cuda-keyring_1.1-1_all.deb
145
+ sudo dpkg -i cuda-keyring_1.1-1_all.deb
149
146
rm cuda-keyring_1.1-1_all.deb
150
- apt-get update
151
- apt-get install -y cuda-toolkit
147
+ sudo apt-get update
148
+ sudo apt-get install -y cuda-toolkit
152
149
} >> " $LOG_FILE " 2>&1
153
150
success " CUDA Toolkit installed successfully."
154
151
fi
@@ -162,25 +159,25 @@ install_docker() {
162
159
info " Installing Docker..."
163
160
{
164
161
# Install prerequisites
165
- apt install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
162
+ sudo apt install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
166
163
167
164
# Add Docker’s official GPG key
168
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
165
+ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
169
166
170
167
# Set up the stable repository
171
- echo " deb [arch=$( dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $( lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
168
+ echo " deb [arch=$( dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $( lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
172
169
173
170
# Update package index
174
- apt update -y
171
+ sudo apt update -y
175
172
176
173
# Install Docker Engine, CLI, and Containerd
177
- apt install -y docker-ce docker-ce-cli containerd.io
174
+ sudo apt install -y docker-ce docker-ce-cli containerd.io
178
175
179
176
# Enable Docker
180
- systemctl enable docker
177
+ sudo systemctl enable docker
181
178
182
179
# Start Docker Service
183
- systemctl start docker
180
+ sudo systemctl start docker
184
181
185
182
} >> " $LOG_FILE " 2>&1
186
183
success " Docker installed and started successfully."
@@ -197,7 +194,7 @@ add_user_to_docker_group() {
197
194
else
198
195
info " Adding user '$username ' to the 'docker' group..."
199
196
{
200
- usermod -aG docker " $username "
197
+ sudo usermod -aG docker " $username "
201
198
} >> " $LOG_FILE " 2>&1
202
199
success " User '$username ' added to the 'docker' group."
203
200
info " To apply the new group membership, please log out and log back in."
@@ -206,23 +203,30 @@ add_user_to_docker_group() {
206
203
207
204
# Function to install NVIDIA Container Toolkit
208
205
install_nvidia_container_toolkit () {
206
+ info " Checking NVIDIA Container Toolkit installation..."
207
+
208
+ if is_package_installed " nvidia-docker2" ; then
209
+ success " NVIDIA Container Toolkit (nvidia-docker2) is already installed."
210
+ return
211
+ fi
212
+
209
213
info " Installing NVIDIA Container Toolkit..."
210
214
211
215
{
212
216
# Add the package repositories
213
217
local distribution
214
218
distribution=$( grep ' ^ID=' /etc/os-release | cut -d' =' -f2 | tr -d ' "' ) $( grep ' ^VERSION_ID=' /etc/os-release | cut -d' =' -f2 | tr -d ' "' )
215
- curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | apt-key add -
216
- curl -s -L https://nvidia.github.io/nvidia-docker/" $distribution " /nvidia-docker.list | tee /etc/apt/sources.list.d/nvidia-docker.list
219
+ curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
220
+ curl -s -L https://nvidia.github.io/nvidia-docker/" $distribution " /nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
217
221
218
222
# Update the package lists
219
- apt update -y
223
+ sudo apt update -y
220
224
221
225
# Install the NVIDIA Docker support
222
- apt install -y nvidia-docker2
226
+ sudo apt install -y nvidia-docker2
223
227
224
228
# Restart Docker to apply changes
225
- systemctl restart docker
229
+ sudo systemctl restart docker
226
230
} >> " $LOG_FILE " 2>&1
227
231
228
232
success " NVIDIA Container Toolkit installed successfully."
@@ -234,10 +238,10 @@ configure_docker_nvidia() {
234
238
235
239
{
236
240
# Create Docker daemon configuration directory if it doesn't exist
237
- mkdir -p /etc/docker
241
+ sudo mkdir -p /etc/docker
238
242
239
243
# Create or overwrite daemon.json with NVIDIA runtime configuration
240
- cat > /etc/docker/daemon.json << EOF
244
+ sudo tee /etc/docker/daemon.json << EOF
241
245
{
242
246
"default-runtime": "nvidia",
243
247
"runtimes": {
@@ -250,7 +254,7 @@ configure_docker_nvidia() {
250
254
EOF
251
255
252
256
# Restart Docker to apply the new configuration
253
- systemctl restart docker
257
+ sudo systemctl restart docker
254
258
} >> " $LOG_FILE " 2>&1
255
259
256
260
success " Docker configured to use NVIDIA runtime by default."
@@ -272,12 +276,20 @@ verify_docker_nvidia() {
272
276
cleanup () {
273
277
info " Cleaning up unnecessary packages..."
274
278
{
275
- apt autoremove -y
276
- apt autoclean -y
279
+ sudo apt autoremove -y
280
+ sudo apt autoclean -y
277
281
} >> " $LOG_FILE " 2>&1
278
282
success " Cleanup completed."
279
283
}
280
284
285
+ init_git_submodules () {
286
+ info " ensuring submodules are initialized..."
287
+ {
288
+ git submodule update --init --recursive
289
+ } >> " $LOG_FILE " 2>&1
290
+ success " git submodules initialized successfully"
291
+ }
292
+
281
293
# =============================================================================
282
294
# Main Script Execution
283
295
# =============================================================================
@@ -288,12 +300,12 @@ exec > >(tee -a "$LOG_FILE") 2>&1
288
300
# Display start message with timestamp
289
301
info " ===== Script Execution Started at $( date) ====="
290
302
291
- # Check for root privileges
292
- check_root
293
-
294
303
# Check if the operating system is Ubuntu
295
304
check_os
296
305
306
+ # ensure all the require source code is present
307
+ init_git_submodules
308
+
297
309
# Update and upgrade the system
298
310
update_system
299
311
0 commit comments