DEV Community

Cover image for Install NVIDIA CUDA on Linux
Batkhuu
Batkhuu

Posted on

Install NVIDIA CUDA on Linux

Official pages


Prerequisites

Check current Linux system specification

  • Check NVIDIA GPU exists:
## Use lshw:
lshw -C display

# Only for Ubuntu/Debian:
sudo apt-get update --fix-missing -o Acquire::CompressionTypes::Order::=gz
sudo apt-get install -y pciutils mesa-utils
# Or use lspci:
lspci | grep -i vga && lspci | grep -i nvidia
# Or use glxinfo:
glxinfo | egrep "OpenGL vendor|OpenGL renderer"
Enter fullscreen mode Exit fullscreen mode
  • Check linux OS information:
## Check linux OS version and distro:
uname -a && cat /etc/*release

## Check kernel version:
uname -r
# Or:
hostnamectl

## Check GCC and make compiler version:
gcc --version && make -v

## Check glibc version:
ldd --version
Enter fullscreen mode Exit fullscreen mode

Check CUDA version compatibility

Install NVIDIA GPU driver

Install CUDA toolkit

Download CUDA

  • IMPORTANT! Download and use the .RUN file! It can prevent installing incompatible NVIDIA drivers and prevent overwriting the previous /usr/local/cuda (if you installed another CUDA already) symbolic link.
  • IMPORTANT! Don't download or use the .DEB file! It will overwrite the current default CUDA (if you installed it already) directory (/usr/local/cuda) and also it will overwrite the already installed latest compatible NVIDIA driver version into the old or incompatible driver. Moreover, if you want to use a different version of CUDA, never install CUDA.DEB file!
  • Search and download the required version of CUDA (cuda_***_linux.run) installer file from this link: https://developer.nvidia.com/cuda-toolkit-archive
  • If you're using a command-line interface, just copy the download link from the NVIDIA website and use wget to download. For example:
mkdir -v ~/Downloads
cd ~/Downloads
# 1. (Skip this, if you've already downloaded) Download CUDA .run installer file by wget:
wget https://developer.download.nvidia.com/compute/cuda/[CUDA_VERSION]/local_installers/cuda_[CUDA_VERSION]_[DRIVER_VERSION]_linux.run
# For example:
wget https://developer.download.nvidia.com/compute/cuda/11.0.3/local_installers/cuda_11.0.3_450.51.06_linux.run
Enter fullscreen mode Exit fullscreen mode

Install CUDA.run file

When installing CUDA.run, follow the below steps:

  • Type 'accept' and ENTER.
  • IMPORTANT! Uncheck and disable NVIDIA driver installation to prevent overwriting already installed latest compatible version of NVIDIA driver into an old or incompatible driver.
  • IMPORTANT! Uncheck and disable CUDA symbolic link installation to prevent overwriting the current default CUDA (if you installed it already) directory (/usr/local/cuda).
  • Choose install and press it!
# Change directory to cuda_***.run file downloaded directory. For example:
cd ~/Downloads

# 2. IMPORTANT! Install cuda_***.run file:
sudo sh cuda_[CUDA_VERSION]_[DRIVER_VERSION]_linux.run
# For example:
sudo sh cuda_11.0.3_450.51.06_linux.run

# 3. Remove downloaded file:
rm -vrf cuda_[CUDA_VERSION]_[DRIVER_VERSION]_linux.run
# For example:
rm -vrf cuda_11.0.3_450.51.06_linux.run
Enter fullscreen mode Exit fullscreen mode

Post-installation

Edit the ~/.bashrc file:

# 4. Add CUDA environment variables to current user .bashrc file:
# (It loads environment variables, whenever current user opens a new bash session/new terminal)
nano ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

and add the following lines into the ~/.bashrc file (don't forget to change [CUDA_VERSION] into the installed version):

## CUDA ##
# Change to installed CUDA version:
# export CUDA_VERSION=[CUDA_VERSION]
export CUDA_VERSION=11.0
export CUDA_HOME="/usr/local/cuda-${CUDA_VERSION}"
export CUDA_PATH="${CUDA_HOME}"
export PATH="${CUDA_PATH}/bin:${PATH}"
export LIBRARY_PATH="${CUDA_PATH}/lib64:${LIBRARY_PATH}"
export LD_LIBRARY_PATH="${CUDA_PATH}/lib64:${LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH="${CUDA_PATH}/extras/CUPTI/lib64:${LD_LIBRARY_PATH}"
export NVCC="${CUDA_PATH}/bin/nvcc"
export CFLAGS="-I${CUDA_PATH}/include ${CFLAGS}"
Enter fullscreen mode Exit fullscreen mode

save changes and exit from the file editor.

# 5. Load .bashrc file to init environment variables into the current bash session:
source ~/.bashrc

# 6. Check CUDA compiler version:
nvcc -V
# Or:
cat ${CUDA_PATH}/version.txt
# Or:
cat ${CUDA_PATH}/version.json
Enter fullscreen mode Exit fullscreen mode

[RECOMMENDED] Install cuDNN (For Deep Learning)

Download cuDNN

  • IMPORTANT! Download and use .TGZ or .TAR.XZ file! You can install cuDNN into a custom directory, which means you can control which version of cuDNN you want to use and where to install it.
  • IMPORTANT! Don't download and use .DEB file! It will overwrite the cuDNN library into the current default CUDA directory (/usr/local/cuda).
  • Download cuDNN .TGZ or .TAR.XZ file from this link: https://developer.nvidia.com/rdp/cudnn-download

Install cuDNN .tgz or .tar.xz package files

# Change directory to cudnn_***.tgz or .tar.xz file downloaded directory. For example:
cd ~/Downloads

## Extract cuDNN .tgz or .tar.xz package file:
# New versions:
tar -xf cudnn-linux-x86_64-[CUDNN_VERSION]_cuda[CUDA_VERSION]-archive.tar.xz
# For example:
tar -xf cudnn-linux-x86_64-8.3.1.22_cuda11.5-archive.tar.xz

# Or older versions:
tar -xzf cudnn-[CUDA_VERSION]-linux-x64-v[CUDNN_VERSION].tgz
# For example:
tar -xzf cudnn-11.0-linux-x64-v8.0.5.39.tgz


# Copy extracted files into specific CUDA directory:
sudo cp -v cud*/include/cudnn*.h /usr/local/cuda-${CUDA_VERSION}/include
sudo cp -v cud*/lib*/libcudnn* /usr/local/cuda-${CUDA_VERSION}/lib64

# Add read permissions to extracted files:
sudo chmod a+r /usr/local/cuda-${CUDA_VERSION}/include/cudnn*.h /usr/local/cuda-${CUDA_VERSION}/lib64/libcudnn*

# Remove downloaded files:
rm -rf cud*

# Check installed cuDNN version:
cat /usr/local/cuda-${CUDA_VERSION}/include/cudnn.h | grep CUDNN_MAJOR -A 2
# Or:
cat /usr/local/cuda-${CUDA_VERSION}/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

# cuDNN installation complete!
Enter fullscreen mode Exit fullscreen mode

[OPTIONAL] Install TensorRT

Download TensorRT

  • IMPORTANT! Download and use the .TAR.GZ file! You can install TensorRT into a custom directory, which means you can control which version of TensorRT you want to use and where to install it.
  • IMPORTANT! Don't download and use .DEB file! You can't control TensorRT libraries.
  • Download the cuDNN .TAR.GZ file from this link: https://developer.nvidia.com/nvidia-tensorrt-download

Install TensorRT .tar.gz package files

# Change directory to TensorRT-***.tar.gz file downloaded directory. For example:
cd ~/Downloads

# Extract TensorRT .tar.gz package file:
tar -xzf TensorRT-[TENSORRT_VERSION].[OS_DISTRO].x86_64-gnu.cuda-[CUDA_VERSION].cudnn[CUDNN_VERSION].tar.gz
# For example:
tar -xzf TensorRT-7.2.0.14.Ubuntu-18.04.x86_64-gnu.cuda-11.0.cudnn8.0.tar.gz

# Create TensorRT base directory:
sudo mkdir -vp /opt/tensorrt

# Copy extracted files into TensorRT directory:
sudo mv -vf TensorRT-[TENSORRT_VERSION] /opt/tensorrt/TensorRT-[TENSORRT_VERSION]
# For example:
sudo mv -vf TensorRT-7.2.0.14 /opt/tensorrt/TensorRT-7.2.0.14

# Remove downloaded file:
rm -vrf TensorRT-[TENSORRT_VERSION].[OS_DISTRO].x86_64-gnu.cuda-[CUDA_VERSION].cudnn[CUDNN_VERSION].tar.gz
# For example:
rm -vrf TensorRT-7.2.0.14.Ubuntu-18.04.x86_64-gnu.cuda-11.0.cudnn8.0.tar.gz
Enter fullscreen mode Exit fullscreen mode

Edit the ~/.bashrc file:

# Add TensorRT environment variables into current user .bashrc file:
# (It loads environment variables, whenever current user opens a new bash session/new terminal)
nano ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

and add the following lines into the ~/.bashrc file (don't forget to change [TENSORRT_VERSION] into the installed version):

## TensorRT ##
# Change to your installed TensorRT version:
# export TENSORRT_VERSION=[TENSORRT_VERSION]
export TENSORRT_VERSION=7.2.0.14
export TENSORRT_PATH="/opt/tensorrt/TensorRT-${TENSORRT_VERSION}"
export PATH="${TENSORRT_VERSION}/bin:${PATH}"
export LD_LIBRARY_PATH="${TENSORRT_PATH}/lib:${LD_LIBRARY_PATH}"
Enter fullscreen mode Exit fullscreen mode

save changes and exit from the file editor.

# Load .bashrc file to init environment variables into current bash session:
source ~/.bashrc

# (Only for Python) Install TensorRT python packages:
cd ${TENSORRT_PATH}/python
pip install tensorrt-[TENSORRT_VERSION]-cp[PYTHON_VERSION]-none-linux_x86_64.whl
# For example:
pip install tensorrt-7.2.0.14-cp38-none-linux_x86_64.whl

cd ${TENSORRT_PATH}/uff
pip install uff-*-py2.py3-none-any.whl

cd ${TENSORRT_PATH}/graphsurgeon
pip install graphsurgeon-*-py2.py3-none-any.whl

cd ${TENSORRT_PATH}/onnx_graphsurgeon
pip install onnx_graphsurgeon-*-py2.py3-none-any.whl

# TensorRT installation complete!
Enter fullscreen mode Exit fullscreen mode

Test CUDA

# Install sample source codes to test CUDA:
cuda-install-samples-[CUDA_VERSION].sh ~
# For example:
cuda-install-samples-11.0.sh ~

# Change directory to CUDA sample source codes installed directory:
cd ~/NVIDIA_CUDA-[CUDA_VERSION]_Samples
# For example:
cd ~/NVIDIA_CUDA-11.0_Samples

# Build sample source codes:
make clean && make -j8

# Change directory to built binary directory:
cd bin/x86_64/linux/release

# Run CUDA test binaries:
./deviceQuery
./bandwidthTest

cd ~
# Remove sample source codes:
rm -rf ~/NVIDIA_CUDA-[CUDA_VERSION]_Samples
# For example:
rm -rf ~/NVIDIA_CUDA-11.0_Samples
Enter fullscreen mode Exit fullscreen mode

👍 ✨

INSTALLATION COMPLETE

Top comments (0)