Skip to content

Instantly share code, notes, and snippets.

@ianblenke
Created September 7, 2023 20:57
Show Gist options
  • Save ianblenke/38a12e2d61534614248510da9c5d4931 to your computer and use it in GitHub Desktop.
Save ianblenke/38a12e2d61534614248510da9c5d4931 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Idempotent Ubuntu distribution agnostic docker-ce and container runtime installation script.
# Author: github.com/ianblenke
# Allow bash clobbering of files on redirect
set +o noclobber
# Install docker-ce
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --batch --yes --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# Add the repository to Apt sources:
echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# Add the current user to the docker group so they can run the docker cli
sudo usermod -aG docker $(whoami)
# Install nvidia-container-toolkit
# Unfortunately, they're in the process of moving things around for some reason.
## This method uses 18.04 as the distribution for that and anything newer, as per the nvidia-container documentation.
#distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
# && curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --batch --yes --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
# && curl -s -L https://nvidia.github.io/libnvidia-container/$distribution/libnvidia-container.list | \
# sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
# sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
# This method doesn't include a distribution version like above, as per the repo's direct suggestion
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --batch --yes --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
echo 'deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://nvidia.github.io/libnvidia-container/stable/deb/$(ARCH) /' | sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit-base nvidia-docker2
# Get the version
nvidia-ctk --version
# Generate a CDI specification
sudo nvidia-ctk cdi generate --output=/etc/cdi/nvidia.yaml
# Check the names of the generated devices:
grep " name:" /etc/cdi/nvidia.yaml
# Configure the Docker daemon to recognize the NVIDIA Container Runtime:
sudo nvidia-ctk runtime configure --runtime=docker
# Restart the Docker daemon to complete the installation after setting the default runtime:
sudo systemctl restart docker
if id | grep docker ; then
echo "You may now run docker with the nvidia runtime"
else
echo "Now exit your shell and login again so that your shell has the docker group and can run the docker cli"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment