Skip to content

Commit

Permalink
Merge pull request #486 from johnnynunez/master
Browse files Browse the repository at this point in the history
Simplify Installation Cuda and latest Cuda
  • Loading branch information
Tom94 authored Jan 14, 2025
2 parents 0b85840 + d643f24 commit 1e38444
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 156 deletions.
19 changes: 18 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ jobs:
strategy:
matrix:
include:
- os: ubuntu-24.04
cuda: "12.6"
arch: 89
- os: ubuntu-24.04
cuda: "12.5"
arch: 86
- os: ubuntu-22.04
cuda: "11.8"
arch: 89
- os: ubuntu-22.04
cuda: "11.7"
arch: 89
Expand Down Expand Up @@ -43,7 +52,7 @@ jobs:
steps:
- name: Install dependencies
run: sudo apt-get update && sudo apt-get install cmake gcc g++
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Install CUDA
Expand All @@ -63,6 +72,14 @@ jobs:
strategy:
matrix:
include:
- os: windows-2025
visual_studio: "Visual Studio 17 2022"
cuda: "12.6.3"
arch: 89
- os: windows-2025
visual_studio: "Visual Studio 17 2022"
cuda: "12.5.0"
arch: 86
- os: windows-2019
visual_studio: "Visual Studio 16 2019"
cuda: "11.5.1"
Expand Down
4 changes: 2 additions & 2 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2020-2023, NVIDIA CORPORATION. All rights reserved.
Copyright (c) 2020-2025, NVIDIA CORPORATION. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand All @@ -18,4 +18,4 @@ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAG
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ sudo apt-get install build-essential git
```

We also recommend installing [CUDA](https://developer.nvidia.com/cuda-toolkit) in `/usr/local/` and adding the CUDA installation to your PATH.
For example, if you have CUDA 11.4, add the following to your `~/.bashrc`
For example, if you have CUDA 12.6.3, add the following to your `~/.bashrc`
```sh
export PATH="/usr/local/cuda-11.4/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-11.4/lib64:$LD_LIBRARY_PATH"
export PATH="/usr/local/cuda-12.6.3/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-12.6.3/lib64:$LD_LIBRARY_PATH"
```


Expand Down
Original file line number Diff line number Diff line change
@@ -1,181 +1,129 @@
# @todo - better / more robust parsing of inputs from env vars.
#!/bin/bash

## -------------------
## Constants
## Configuration
## -------------------

# @todo - apt repos/known supported versions?

# @todo - GCC support matrix?

# List of sub-packages to install.
# @todo - pass this in from outside the script?
# @todo - check the specified subpackages exist via apt pre-install? apt-rdepends cuda-9-0 | grep "^cuda-"?

# Ideally choose from the list of meta-packages to minimise variance between cuda versions (although it does change too)
CUDA_PACKAGES_IN=(
"command-line-tools"
"libraries-dev"
# CUDA sub-packages to install.
declare -a CUDA_PACKAGES_IN=(
"cuda-command-line-tools"
"cuda-libraries-dev"
"cuda-nvcc"
)

## -------------------
## Bash functions
## Helper Functions
## -------------------
# returns 0 (true) if a >= b
function version_ge() {
[ "$#" != "2" ] && echo "${FUNCNAME[0]} requires exactly 2 arguments." && exit 1
[ "$(printf '%s\n' "$@" | sort -V | head -n 1)" == "$2" ]
}
# returns 0 (true) if a > b
function version_gt() {
[ "$#" != "2" ] && echo "${FUNCNAME[0]} requires exactly 2 arguments." && exit 1
[ "$1" = "$2" ] && return 1 || version_ge $1 $2
}
# returns 0 (true) if a <= b
function version_le() {
[ "$#" != "2" ] && echo "${FUNCNAME[0]} requires exactly 2 arguments." && exit 1
[ "$(printf '%s\n' "$@" | sort -V | head -n 1)" == "$1" ]
}
# returns 0 (true) if a < b
function version_lt() {
[ "$#" != "2" ] && echo "${FUNCNAME[0]} requires exactly 2 arguments." && exit 1
[ "$1" = "$2" ] && return 1 || version_le $1 $2

# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}

## -------------------
## Select CUDA version
## Input Validation and Environment Setup
## -------------------

# Get the cuda version from the environment as $cuda.
CUDA_VERSION_MAJOR_MINOR=${cuda}
# Get CUDA version from environment variable 'cuda'
CUDA_VERSION_MAJOR_MINOR="${cuda}"

# Split the version.
# We (might/probably) don't know PATCH at this point - it depends which version gets installed.
CUDA_MAJOR=$(echo "${CUDA_VERSION_MAJOR_MINOR}" | cut -d. -f1)
CUDA_MINOR=$(echo "${CUDA_VERSION_MAJOR_MINOR}" | cut -d. -f2)
CUDA_PATCH=$(echo "${CUDA_VERSION_MAJOR_MINOR}" | cut -d. -f3)
# use lsb_release to find the OS.
UBUNTU_VERSION=$(lsb_release -sr)
UBUNTU_VERSION="${UBUNTU_VERSION//.}"

echo "CUDA_MAJOR: ${CUDA_MAJOR}"
echo "CUDA_MINOR: ${CUDA_MINOR}"
echo "CUDA_PATCH: ${CUDA_PATCH}"
# echo "UBUNTU_NAME: ${UBUNTU_NAME}"
echo "UBUNTU_VERSION: ${UBUNTU_VERSION}"

# If we don't know the CUDA_MAJOR or MINOR, error.
if [ -z "${CUDA_MAJOR}" ] ; then
echo "Error: Unknown CUDA Major version. Aborting."
exit 1
# Validate CUDA version
if [ -z "$CUDA_VERSION_MAJOR_MINOR" ]; then
echo "Error: CUDA version not specified. Please set the 'cuda' environment variable (e.g., cuda=12.2)."
exit 1
fi
if [ -z "${CUDA_MINOR}" ] ; then
echo "Error: Unknown CUDA Minor version. Aborting."
exit 1

CUDA_MAJOR=$(echo "$CUDA_VERSION_MAJOR_MINOR" | cut -d. -f1)
CUDA_MINOR=$(echo "$CUDA_VERSION_MAJOR_MINOR" | cut -d. -f2)

# Check for root/sudo
if ! command_exists sudo && [ "$EUID" -ne 0 ]; then
echo "Error: This script requires root privileges. Please run with sudo."
exit 1
fi
# If we don't know the Ubuntu version, error.
if [ -z ${UBUNTU_VERSION} ]; then
echo "Error: Unknown Ubuntu version. Aborting."
exit 1

SUDO_CMD=""
if [ "$EUID" -ne 0 ]; then
SUDO_CMD="sudo"
fi

# Get Ubuntu version
UBUNTU_VERSION=$(lsb_release -sr)

## ---------------------------
## GCC studio support check?
## ---------------------------
# Validate Ubuntu version
if [ -z "$UBUNTU_VERSION" ]; then
echo "Error: Could not determine Ubuntu version."
exit 1
fi

# @todo
# Format Ubuntu version for URLs (e.g., 20.04 -> 2004)
UBUNTU_VERSION_FORMATTED=$(echo "$UBUNTU_VERSION" | tr -d '.')

## -------------------------------
## Select CUDA packages to install
## -------------------------------
CUDA_PACKAGES=""
for package in "${CUDA_PACKAGES_IN[@]}"
do :
# @todo This is not perfect. Should probably provide a separate list for diff versions
# cuda-compiler-X-Y if CUDA >= 9.1 else cuda-nvcc-X-Y
if [[ "${package}" == "nvcc" ]] && version_ge "$CUDA_VERSION_MAJOR_MINOR" "9.1" ; then
package="compiler"
elif [[ "${package}" == "compiler" ]] && version_lt "$CUDA_VERSION_MAJOR_MINOR" "9.1" ; then
package="nvcc"
fi
# Build the full package name and append to the string.
CUDA_PACKAGES+=" cuda-${package}-${CUDA_MAJOR}-${CUDA_MINOR}"
done
echo "CUDA_PACKAGES ${CUDA_PACKAGES}"
echo "CUDA Version: $CUDA_VERSION_MAJOR_MINOR"
echo "Ubuntu Version: $UBUNTU_VERSION"
echo "Ubuntu Version Formatted: $UBUNTU_VERSION_FORMATTED"

## -----------------
## Prepare to install
## -----------------
## -------------------
## Install CUDA
## -------------------

PIN_FILENAME="cuda-ubuntu${UBUNTU_VERSION}.pin"
PIN_URL="https://developer.download.nvidia.com/compute/cuda/repos/ubuntu${UBUNTU_VERSION}/x86_64/${PIN_FILENAME}"
APT_KEY_URL="https://developer.download.nvidia.com/compute/cuda/repos/ubuntu${UBUNTU_VERSION}/x86_64/3bf863cc.pub"
REPO_URL="https://developer.download.nvidia.com/compute/cuda/repos/ubuntu${UBUNTU_VERSION}/x86_64/"
# Download and install the CUDA keyring
KEYRING_URL="https://developer.download.nvidia.com/compute/cuda/repos/ubuntu${UBUNTU_VERSION_FORMATTED}/x86_64/cuda-keyring_1.1-1_all.deb"
echo "Downloading CUDA keyring from: $KEYRING_URL"
wget -nv "$KEYRING_URL" -O cuda-keyring.deb
$SUDO_CMD dpkg -i cuda-keyring.deb
rm cuda-keyring.deb

echo "PIN_FILENAME ${PIN_FILENAME}"
echo "PIN_URL ${PIN_URL}"
echo "APT_KEY_URL ${APT_KEY_URL}"
# Update package list
echo "Updating package list..."
$SUDO_CMD apt-get update

## -----------------
## Check for root/sudo
## -----------------
# Construct the list of CUDA packages to install
CUDA_PACKAGES=""
for package in "${CUDA_PACKAGES_IN[@]}"; do
CUDA_PACKAGES+=" ${package}-${CUDA_MAJOR}-${CUDA_MINOR}"
done

# Detect if the script is being run as root, storing true/false in is_root.
is_root=false
if (( $EUID == 0)); then
is_root=true
fi
# Find if sudo is available
has_sudo=false
if command -v sudo &> /dev/null ; then
has_sudo=true
fi
# Decide if we can proceed or not (root or sudo is required) and if so store whether sudo should be used or not.
if [ "$is_root" = false ] && [ "$has_sudo" = false ]; then
echo "Root or sudo is required. Aborting."
exit 1
elif [ "$is_root" = false ] ; then
USE_SUDO=sudo
else
USE_SUDO=
# Special handling for nvcc in older versions
if (( $(echo "$CUDA_MAJOR < 9" | bc -l) )); then
CUDA_PACKAGES+=" cuda-nvcc-${CUDA_MAJOR}-${CUDA_MINOR}"
fi

## -----------------
## Install
## -----------------
echo "Adding CUDA Repository"
wget ${PIN_URL}
$USE_SUDO mv ${PIN_FILENAME} /etc/apt/preferences.d/cuda-repository-pin-600
$USE_SUDO apt-key adv --fetch-keys ${APT_KEY_URL}
$USE_SUDO add-apt-repository "deb ${REPO_URL} /"
$USE_SUDO apt-get update

echo "Installing CUDA packages ${CUDA_PACKAGES}"
$USE_SUDO apt-get -y install ${CUDA_PACKAGES}

if [[ $? -ne 0 ]]; then
echo "CUDA Installation Error."
exit 1
# Install CUDA packages
echo "Installing CUDA packages: $CUDA_PACKAGES"
$SUDO_CMD apt-get install -y --no-install-recommends $CUDA_PACKAGES

if [ $? -ne 0 ]; then
echo "Error: Failed to install CUDA packages."
exit 1
fi
## -----------------
## Set environment vars / vars to be propagated
## -----------------

CUDA_PATH=/usr/local/cuda-${CUDA_MAJOR}.${CUDA_MINOR}
echo "CUDA_PATH=${CUDA_PATH}"
export CUDA_PATH=${CUDA_PATH}
## -------------------
## Environment Variables
## -------------------

CUDA_PATH="/usr/local/cuda-${CUDA_MAJOR}.${CUDA_MINOR}"
echo "CUDA_PATH=$CUDA_PATH"

# Quick test. @temp
# Update environment variables for the current shell
export CUDA_PATH
export PATH="$CUDA_PATH/bin:$PATH"
export LD_LIBRARY_PATH="$CUDA_PATH/lib:$LD_LIBRARY_PATH"
nvcc -V

# If executed on github actions, make the appropriate echo statements to update the environment
if [[ $GITHUB_ACTIONS ]]; then
# Set paths for subsequent steps, using ${CUDA_PATH}
echo "Adding CUDA to CUDA_PATH, PATH and LD_LIBRARY_PATH"
echo "CUDA_PATH=${CUDA_PATH}" >> $GITHUB_ENV
echo "${CUDA_PATH}/bin" >> $GITHUB_PATH
echo "LD_LIBRARY_PATH=${CUDA_PATH}/lib:${LD_LIBRARY_PATH}" >> $GITHUB_ENV
export LD_LIBRARY_PATH="$CUDA_PATH/lib64:$LD_LIBRARY_PATH"

# Verify installation (optional)
echo "Verifying installation..."
if command_exists nvcc; then
nvcc --version
else
echo "Warning: nvcc not found. Installation might be incomplete."
fi

# Update environment variables for GitHub Actions (if applicable)
if [ -n "$GITHUB_ACTIONS" ]; then
echo "Setting environment variables for GitHub Actions..."
echo "CUDA_PATH=$CUDA_PATH" >> "$GITHUB_ENV"
echo "$CUDA_PATH/bin" >> "$GITHUB_PATH"
echo "LD_LIBRARY_PATH=$CUDA_PATH/lib64:$LD_LIBRARY_PATH" >> "$GITHUB_ENV"
fi

echo "CUDA installation complete."
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Dictionary of known cuda versions and thier download URLS, which do not follow a consistent pattern :(
$CUDA_KNOWN_URLS = @{
"8.0.44" = "http://developer.nvidia.com/compute/cuda/8.0/Prod/network_installers/cuda_8.0.44_win10_network-exe";
"8.0.44" = "http://developer.nvidia.com/compute/cuda/8.0/Prod/network_installers/cuda_8.0.44_win10_network-exe";
"8.0.61" = "http://developer.nvidia.com/compute/cuda/8.0/Prod2/network_installers/cuda_8.0.61_win10_network-exe";
"9.0.176" = "http://developer.nvidia.com/compute/cuda/9.0/Prod/network_installers/cuda_9.0.176_win10_network-exe";
"9.1.85" = "http://developer.nvidia.com/compute/cuda/9.1/Prod/network_installers/cuda_9.1.85_win10_network";
Expand All @@ -25,7 +25,10 @@ $CUDA_KNOWN_URLS = @{
"11.3.0" = "https://developer.download.nvidia.com/compute/cuda/11.3.0/network_installers/cuda_11.3.0_win10_network.exe";
"11.3.1" = "https://developer.download.nvidia.com/compute/cuda/11.3.1/network_installers/cuda_11.3.1_win10_network.exe";
"11.5.0" = "https://developer.download.nvidia.com/compute/cuda/11.5.0/network_installers/cuda_11.5.0_win10_network.exe";
"11.5.1" = "https://developer.download.nvidia.com/compute/cuda/11.5.1/network_installers/cuda_11.5.1_windows_network.exe"
"11.5.1" = "https://developer.download.nvidia.com/compute/cuda/11.5.1/network_installers/cuda_11.5.1_windows_network.exe";
"11.8.0" = "https://developer.download.nvidia.com/compute/cuda/11.8.0/network_installers/cuda_11.8.0_windows_network.exe";
"12.5.0" = "https://developer.download.nvidia.com/compute/cuda/12.5.0/network_installers/cuda_12.5.0_windows_network.exe";
"12.6.3" = "https://developer.download.nvidia.com/compute/cuda/12.6.3/network_installers/cuda_12.6.3_windows_network.exe";
}

# @todo - change this to be based on _MSC_VER intead, or invert it to be CUDA keyed instead?
Expand Down

0 comments on commit 1e38444

Please sign in to comment.