Skip to content

Instantly share code, notes, and snippets.

@fabito
Last active November 30, 2022 09:18
Show Gist options
  • Save fabito/965b7d3e32307a5a0497f9f759e8bc83 to your computer and use it in GitHub Desktop.
Save fabito/965b7d3e32307a5a0497f9f759e8bc83 to your computer and use it in GitHub Desktop.
Craete a Google Compute Engine instance with GPU , docker , cuda and nvidia-docker pre installed via startup shell script
#!/usr/bin/env bash
PROJECT=$1
INSTANCE_NAME=$2
MACHINE_TYPE=${3:-n1-standard-4}
ZONE=${4:-us-central1-c}
NUM_GPUS=${5:-1}
gcloud compute --project ${PROJECT} instances create ${INSTANCE_NAME} \
--zone ${ZONE} \
--machine-type "${MACHINE_TYPE}" \
--subnet "default" \
--maintenance-policy "TERMINATE" \
--scopes "https://www.googleapis.com/auth/cloud-platform" \
--accelerator type=nvidia-tesla-k80,count=${NUM_GPUS} \
--min-cpu-platform "Automatic" \
--tags "http-server" \
--image-family "ubuntu-1604-lts" \
--image-project "ubuntu-os-cloud" \
--boot-disk-size "50" \
--boot-disk-type "pd-ssd" \
--boot-disk-device-name "${INSTANCE_NAME}-disk" \
--metadata-from-file startup-script=startup.sh
#!/bin/bash
set -e
echo "Installing docker"
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt-get update
apt-cache policy docker-ce
apt-get install -y docker-ce make
echo "Checking for CUDA and installing."
# Check for CUDA and try to install.
if ! dpkg-query -W cuda-9-0; then
# The 16.04 installer works with 16.10.
curl -O http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.0.176-1_amd64.deb
# apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
wget -qO - https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub | sudo apt-key add -
dpkg -i ./cuda-repo-ubuntu1604_9.0.176-1_amd64.deb
apt-get update
apt-get install cuda -y
fi
# Enable persistence mode
nvidia-smi -pm 1
nvidia-smi --auto-boost-default=DISABLED
echo "Installing nvidia docker"
# Add the package repositories
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/ubuntu16.04/amd64/nvidia-docker.list | tee /etc/apt/sources.list.d/nvidia-docker.list
apt-get update
# Install nvidia-docker2 and reload the Docker daemon configuration
apt-get install -y nvidia-docker2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment