This is a short overview of my basic ML Ubuntu 18.04 setup. It includes NVIDIA Drivers, CUDA, OpenCV as well as a basic Python virtual environment setup. For this setup I expect a PC with a (current) NVIDIA GPU and Ubuntu 18.04 installed. I use standard paths, if necessary these must be adapted according to your installation.
First of all update your system to the latest version as follows:
sudo apt update sudo apt upgrade
Install the proprietary NVIDIA drivers as well as CUDA and CUDNN to get GPU acceleration.
We install the NVIDIA drivers by using an PPA.
sudo add-apt-repository ppa:graphics-drivers/ppa sudo apt install nvidia-driver-396
You may install a newer version than 396, all available versions are displayed when you type nvidia- followed by <tab>.
After the installation is finished, restart the PC.
CUDA 9.0 - 9.2 doesn’t officially support Ubuntu 18.04, but can be installed fine. To install CUDA you just need to use GCC 6 instead of the default GCC 7 in Ubuntu 18.04. The following code shows how to install GCC and G++ 6 and setup update-alternatives accordingly.
sudo update-alternatives --remove-all gcc sudo update-alternatives --remove-all g++ sudo apt install gcc-6 g++-6 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 10 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 20 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-6 10 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 20 sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30 sudo update-alternatives --set cc /usr/bin/gcc sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30 sudo update-alternatives --set c++ /usr/bin/g++
With this setup GCC 7 will be the default. If you want to install CUDA or compile CUDA apps you can switch GCC / G++ as follows.
sudo update-alternatives --config gcc sudo update-alternatives --config g++
Select the required version here. For the following install of CUDA 9.0 set them to version 6. After you have installed CUDA you can set it back to 7.0. If you run tensorflow / PyTorch programs it’s fine to let the compilers on version 7, the are not required to execute this programs.
Download the Installer as well as the patches from CUDA 9.0 Download.
Install the files via the following command (change file name for the two patches). When the installation procedure comes up a few questions will be asked about what to install where. It is important to NOT install the driver, only CUDA (optionally the CUDA examples). The default path /usr/local/cuda-9.0 is fine. Also let it create a symbolic link at /usr/local/cuda.
sudo sh cuda_9.0.176_384.81_linux.run
Add the following to your bashrc (~/.bashrc, .zshrc when zsh is used):
# Path export PATH="/usr/local/cuda/bin:$PATH" export LD_LIBRARY_PATH="/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH" export LIBRARY_PATH="/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64:$LIBRARY_PATH"
Download CUDNN 7.1 for CUDA 9.0. Extract the content, this will create a cuda directory, e.g. in ~/Downloads/cuda. After extracting copy the content to the CUDA folder, as follows:
sudo rsync -rl cuda/ /usr/local/cuda
Optional step for multiple CUDNN versions
If you would like to install multiple versions of CUDNN you could just create a subfolder and add the lib64 files there and later on set the (LD_)LIBRARY_PATH accordingly to which version you need. For example:
sudo mkdir /usr/local/cuda-9.0/cudnn sudo cp -R cuda/lib64 /usr/local/cuda-9.0/cudnn/7.1
Install some prerequisites:
sudo apt install python-dev python3-dev python-pip virtualenv sudo pip install virtualenv virtualenvwrapper
Add the following to your bashrc (~/.bashrc):
# virtualenv and virtualenvwrapper export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh
Close your terminal and reopen it (or just
OpenCV 3.2.0 can be installed from the Ubuntu 18.04 official repository. If you want to compile it by yourself, check out the guide in the Ubuntu 16.04 ML setup note. If you create new virtual envs with the –system-site-packages flag it will be added to the newly created virtual env automatically.
sudo apt install python3-opencv
PyTorch Development Environment
Now it is time to setup the python environment for PyTorch development.
mkvirtualenv pytorch -p python3 --system-site-packages workon pytorch pip install http://download.pytorch.org/whl/cu90/torch-0.4.0-cp36-cp36m-linux_x86_64.whl pip install torchvision
Tensorflow Development Environment
Tensorflow can be setup equivalent to PyTorch by creating a new virtual environment, adding opencv and installing it via pip.
mkvirtualenv tf -p python3 --system-site-packages workon tf pip install tensorflow-gpu