A Machine Learning Environment with Ubuntu and GPU acceleration in 5 steps

Bram Bloks
11 min readMar 24, 2018

--

Where hardware and intelligent software come together

This step-by-step guide covers BIOS settings, installing Ubuntu OS, GPU acceleration software, Python, Machine and Deep Learning Package and create Virtual Environments.

Whether you are new to machine learning, work with it on a daily basis or even craft dedicated ML computers, a good working system is essential. However setting up the environment can be time consuming and nerve breaking (speaking form personal experience).

To make it even more complex there are many ways to to customize your environment and many programs to include. The past year I have set up countless ML environments, on different computers and with varying configurations. I found myself looking on many websites and reading different guides for all separate parts of the process. Therefore I decided to bundle the essential steps in creating a ML environment and have written them down in this post. There are many additional options for you to include, however this setup is simple and robust.

We go from starting up a new computer for the first time to a working environment on this same machine in 5 steps. Furthermore this system setup is outlined with GPU enhancement, which you definitely want to use when working on large ML models. The steps are:

  1. Setting the computer BIOS
  2. Install the Ubuntu operating system
  3. Install GPU acceleration software (CUDA and cuDNN)
  4. Create a virtual environment and install Python
  5. Install Machine Learning packages

If you haven't switched to Linux/Ubuntu yet or maybe never even heard of it, this guide is for you! Ubuntu is a powerful and user friendly operating system for Machine Learning and working with the terminal/command line is not as hard as it may seem. I hope to show in this guide that it is actually pretty easy.

Let's kick off!

1. BIOS Settings

A BIOS is the Basic Input Output System of your computer. The BIOS initializes and tests the system hardware components and loads an operating system, or boot loader when no operating system is installed. It is the system you see when you start a (new) computer for the first time and no operating system (OS) is yet installed.

If you have an OS installed you can enter the BIOS by pressing one of the BIOS keys when starting up your PC, before the OS is loaded. Usually the key is the ESC key or one of the F1-F12 keys.

This is how my BIOS Home looks like…

ASUS B150i PRO AURA Motherboard BIOS

Navigating trough the BIOS you can see your system details and change preferences. For example the connected hardware specifications.

1.1. Disable secure boot

To be able to install and run Ubuntu on your system you have to disable secure boot and set OS type (operating system type) to 'Other OS'. The location an display of this settings menu in your BIOS may look different. However make sure it allows to run Linux. Here is how I configured that in my BIOS:

2. The operating system: Ubuntu

2.1. Why Ubuntu?

For this setup I use Ubuntu as an operating system (OS). Ubuntu is one of the most popular and best maintained Linux distro’s (distributions). There are various reasons to use Linux, as mentioned in this Quora question. Read in this Quora question why to use Ubuntu over other Linux disto’s.

For me the most important reasons to go with Linux over Windows or Mac are the speed of the OS and easy and powerful use of the command line and repositories, which make setting up and maintaining your ML system very easy. I prefer to use Ubuntu because of its great community and support.

2.2. Create a bootable USB

Installing an OS on your pc can be done by using a bootable USB stick, which you insert in an USB port and access when starting your pc. You need a 2GB or larger USB stick/drive.

Let's create the bootable USB. Go to the downloads section on the official Ubuntu website and download the Ubuntu ISO file. You want to go for a LTS (Life Time Support) version. At the time of writing this post I got the latest version, which was Ubuntu 16.04 LTS.

Download the file and follow the instructions to create a bootable USB containing this file. Depending on the operation system that you are currently using, follow the Windows or MAC guide. If you are somehow already on Ubuntu use this guide.

2.3. Install Ubuntu

Now that we have a bootable USB drive it is time to insert it to a USB port and start your system. For some systems the bootable drive is recognized automatically. On others your first have to enter the BIOS. Enter the BIOS and click on the drive you just inserted to initiate the installation.

Navigating manually to your bootable drive in your bios may look like displayed in the image below. Double click on your drive to initiate the installation.

Ubuntu gives you the option to first try it running from the USB. Play around to see if everything works fine and then install Ubuntu on your system.

Follow the instructions in the installation menu or have a look at the official Ubuntu installation guide. This guide is very clear, so I will not bother to repeat all individual steps here. If it is your first time working with Ubuntu you can mostly stick to the default options as suggested by the installer. Moreover the community has made some great guides and posts with detailed explanations of all the options. All can be found online using google ;)

Tip: Ubuntu also allows you to encrypt your system and Home folder for better security.

After installation restart your computer as is prompted to finish the installation. When starting up you will be asked to remove the installation medium (your usb). Remove this USB and login to Ubuntu with the password that you have picked during the installation.

2.4. Check the installation

In Linux systems actions are easily performed using the Terminal. This is the same as the Mac Terminal. On Windows it is called the Command Prompt or Command Line.

Open a new terminal via the navigation menu or use the hotkeys:

CTRL + ALT + T

Enter the following command in the terminal to check the installed Ubuntu version:

lsb_release -a

And enter the command below to see the Linux details of the distribution:

uname -a

The result should look something like this:

Ubuntu installation details

3. Install GPU acceleration software

A number of Machine Learning tools uses GPU Accelerated Software to train and run models. In particular for Deep Learning GPUs are used for their higher computational power compared to CPUs, thereby reducing valuable training time of the models. A more detailed explanation by NVIDIA can be read here.

At time of writing this post, the most popular GPU acceleration software for ML/DL is CUDA in combination with cuDNN, both developed by NVIDIA. We will first install CUDA. After that cuDNN which actually is just a matter of adding some files to the CUDA directory.

3.1. Prerequisites

A detailed description of the prerequisites for installing CUDA is found in the Official CUDA Installation Guide for Linux. The main steps are:

Verify you have a CUDA capable GPU by entering the following commands in your terminal:

lspci | grep -i nvidia

Verify you have a supported version of Linux.

uname -m && cat /etc/*release

Verify the system has GCC installed.

gcc --version

Install the kernel headers and development packages (you need to insert your password for this command, since you have to execute it as a root-user using 'sudo').

sudo apt-get install linux-headers-$(uname -r)

Altogether in your terminal the result should look something like this:

3.2. Install latest NVIDIA drivers

To be able to run GPU accelerated software the latest NVIDIA drivers need to be installed. Assuming that you have a NVIDIA GPU installed in your pc — which is currently the way to go in machine learning.

Go to the official NVIDIA website and select the specifications of the GPU you have. On this system I have a GeForce GTX 1060, so the selection for me is as follows:

You don’t have to download the drivers here. Just remember the version number. The latest drivers can be easily installed using the command line.

Open your terminal and install the latest nvidia driver. Input the required version number. You don't have to specify the numbers after the digits, the right version is found automatically.

sudo apt-get install nvidia-<version number>

Then update the Advanced Packaging Tool (apt) and reboot the systeem to get everything to work.

sudo apt-get update
sudo reboot -h now

Check if the latest drivers are installed. The output should look something like the screen below.

nvidia-smi

Or use the GUI and search your computers for the Additional Drivers program and select the correct driver.

3.3. Download and Install CUDA

Go to the official CUDA download page and choose your system specifications. Download the deb(local) files of the base installer and available patches.

Make sure you download a CUDA version that is compatible with the ML packages you will use later. At the time of writing this post the latest CUDA version is 9.1, however for example Tensorflow — a popular ML package we install later — only supports CUDA up to version 9.0. (I will show how to installing Tensorflow and other packages in the next chapter).

In your terminal, navigate to the folder where you saved the deb files. This probably is the downloads folder, or a folder that you have specified.

1.Verify the downloads using their checksums. Verify the download by comparing the md5 checksum of the file with the sums given at: http://developer.nvidia.com/cuda-downloads/checksums.

md5sum <file> 

2. Now install the repositories and CUDA. First install the repository meta-date of the base installer with its .deb file with the following command:

sudo dpkg -i cuda-repo-<distro>_<version>_<architecture>.deb

Tip: use TAB key to autocomplete when typing in your terminal!

3. Install the CUDA public GPG key:

sudo apt-key add /var/cuda-rep-<version>/<key mentioned in the documentation> 

4. Update the APT repository cache:

sudo apt-get update

5. Install CUDA:

sudo apt-get install cuda

6. Include /usr/local/cuda-<version>/bin to the PATH :

export PATH=/usr/local/cuda-<version>/bin${PATH:+:${PATH}} 

7. Install the patches in order. For each patch perform the following steps:

sudo dpkg -i cuda-repo-<package_details>
sudo apt-get update
sudo apt-get upgrade cuda

8. Check the installed CUDA version by running in your terminal:

cat /usr/local/cuda/version.txt 

You can read the official CUDA Installation Documentation for more information.

3.4. Download and Install cuDNN

Go to the NVIDIA cuDNN home page. Sign in with your account or create a new one if you don't have one yet. Then click to the download section and choose the correct cuDNN version. cuDNN has to match your installed CUDA version and pay attention to the latest compatible version with the ML packages you plan to install later! Have a look at their installation guides and prerequisites documentation. (Trust me, if there are any compatibility issues you want to know them as soon as possible and not at the end of the process.)

Download the cuDNN Library for Linux. Navigate to the directory where you saved the Tar file and unzip the package.

tar -xzvf cudnn-<version>-linux-x64-v7.tgz

Then copy the following files to your CUDA directory:

sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h
/usr/local/cuda/lib64/libcudnn*

More information can be found on the official cuDNN guide.

Now reboot your system using sudo reboot -h now .

Congratulations! You have completed the hardest part of getting the core components of you Machine Learning system in place.

4. Create a Virtual Environment

Virtual environments enable you to create separate systems in your system, from where you can install and run packages. Installing packages in virtual environments instead on your native/root system allows you to work with different package versions without conflicts. Perform the following steps to create a virtual environment for Python. (I use Python3 as a programming language in this setup, since it currently is the most popular language for ML)

  1. Install pip and a virtual environment
sudo apt-get install python3-pip python3-dev python-virtualenv

2. Create a new virtual environment named 'venv' and specify the python version you like to use on it. You can choose any name for the virtual environment.

virtualenv -p python3.5 venv

3. Activate the virtual environment

source venv/bin/activate

You are now in the virtual environment. The running environment is displayed in your terminal between brackets.

(venv) me@pc:~$: 

5. You can deactivate the venv by typing deactivate and press ENTER .

In the next chapter we install some popular Machine and Deep Learning packages in the virtual environment.

5. Install Machine and Deep Learning packages

Go into your virtual environment and check the currently installed packages using the pip list --format=columnscommand. You can see that a number of packages came along with the installation.

PIP is a package management system used to install and manage software packages written in Python.

Let's install some Python packages that are frequently used, using pip. Pip makes this process really smooth and easy. Pip also directly accounts for the correct python version for your packages. — Make sure you are in your virtual environment!

pip install numpy
pip install scipy
pip install pandas
pip install matplotlib

Check the installed packages in the virtualenvironment using pip list --format=columns . As you see in the list, some packages bring additional packages. This is because most packages are built on other packages and therefore need also to import their base packages to function.

Now it is time to install some popular Machine and Deep Learning packages.

  1. Install Scikit-learn
pip install scikit-learn

2. Install Tensorflow. We can now install the GPU enhanced version, given the steps we have taken in the previous chapters. If you are having trouble installing Tensorflow, you can read the official installation guide for virtualenv on Ubuntu here.

pip3 install --upgrade tensorflow-gpu

3. Install Keras

pip install keras

Wow, that was really easy! No downloads and dragging with files..

Using the above methods you can create other virtual environments and install ML packages. If a package does not have a pip install option yet, follow the instructions in it's documentation. Some interesting ML packages to look at are: PyTorch, XGBoost, LightGBM and CatBoost.

That's it! If you followed through the actions of this guide, congratulations! You have completely set up a Machine Learning environment on Ubuntu with GPU acceleration on your computer.

I hope this was useful to you and good luck on your Machine Learning journey! If you have any questions of comments, feel free to comment below. I will try to answer them. Also, feel free to share this story.

--

--

Bram Bloks

Entrepreneur, founder, developer & open source software contributor. Tech enthusiast and active AI/ML community member.