UCL Department of Electronic and Electrical Engineering
Staff/Student Intranet

Deep/Machine Learning

Installing tools on CentOS 7

If you want to do some machine/deep learning and you wish to make use of departmental GPU services, then you are probably going to be using python and  modules such as Tensorflow, Keras,  pytorch, etc.

All of these modules can be installed either in your home directory or your scratch directory by creating a python virtual environment.

If python support for GPU's is required you will need to add some environment variables to your ~/.cshrc file.

You can find the CUDA toolkit and CUDANN libraries at /apps/cuda, and you must use the version of cudnn that is the same version number as the cuda version

To find out what shell you are using, login to a linux server and type the command:

echo $SHELL
if this returns /bin/csh, then you will need to add the following to your ~/.cshrc file:
########################
# Entries for cuda/cudnn
########################
setenv CUDA_HOME /apps/cuda/cuda-11.2.0
setenv CNN_HOME /apps/cuda/cudnn-11.2-linux-x64-v8.1.1.33
setenv PATH {$CUDA_HOME}/bin:{$PATH}
setenv LD_LIBRARY_PATH {$CUDA_HOME}/lib64:{$CNN_HOME}/lib64:{$LD_LIBRARY_PATH}

A handy thing is to add the end of your .cshrc:

## activate python virtual env
alias activate 'source /home/$USER/\!*/bin/activate.csh'

This will allow you to activate different virtualenv's by just typing activate name-of-virtual-env (The virtual env name will be set below).

if your shell returns as /bin/bash you will need to add the following to the end of your ~/.bashrc file:
########################
# Entries for cuda/cudnn
########################
export CUDA_HOME=/apps/cuda/cuda-11.2.0
export CNN_HOME=/apps/cuda/cudnn-11.2-linux-x64-v8.1.1.33
export PATH=$CUDA_HOME/bin:$PATH
export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$CNN_HOME/lib64:$LD_LIBRARY_PATH

whichever shell you are using, you should logout and login again after making these changes to make sure that you can login with no errors and these environment variables are active in your session

You can choose to use a standard python3 virtual environment, or one created using the Anaconda python distribution.

Using Python 3

The default version of python on CentOS 7  is still python 2, but any new activities on python should be conducted with python3.

To create a python3 virtual environment:

$ python3 -m venv ~/p3-env

The "p3-env" used above is the name of the newly created virtual environment. You are welcome to change this to whatever you want.

if you are using /bin/csh, you activate the virtual environment by using the following command
$ source ~/p3-env/bin/activate.csh
if you are using /bin/bash, you activate the virtual environment by using the following command
$ source ~/p3-env/bin/activate
The rest of this section is the same for /bin/csh or /bin/bash

The source commands should add the following prefix to your prompt:

(p3-env)$

You will need to update the version of pip installed in your virtualenv:

(p3-env)$ pip3 install -U pip

You will also need to update the setuptools installed in your virtualenv:

(p3-env)$ pip3 install --upgrade setuptools

You should be able to get tensorflow installed from the pip repositories by using the command:

(p3-env)$ pip3 install tensorflow-gpu

When you are done using TensorFlow, you may deactivate the environment by invoking the deactivate function as follows:

(p3-env)$ deactivate 

Using Anaconda

Some people prefer to use the anaconda python distribution. This is also installed on all of the GPU servers.

You can create a python3 anaconda virtual environment by activating the system version:

if you are using /bin/csh
source /opt/anaconda3/etc/profile.d/conda.csh
if you are using /bin/bash
.  /opt/anaconda3/etc/profile.d/conda.sh
The rest of this section is the same for /bin/csh or /bin/bash

Then you can use the conda command to create the virtual environment

conda create -y -p ~/p3-env anaconda

Then you need to activate your environment:

conda activate ~/p3-env

You can then use the conda command to install additional modules. i.e.

conda install -y keras matplotlib pandas spyder tensorflow pillow

When you have finished using the virtual environment there is no uninstall process. To remove a python3 virtual environment you can just remove the directory:

rm -rf ~/p3-env

To remove an anaconda virtual environment:

conda remove --name p3-env