Installing an OS and Software for Machine Learning
Tags: ai, machinelearning, software, linux
Published: 2020-03-13 19:11:08

This is an article about setting up a new computer with the software needed to do Machine Learning on it.  We’re going to use Ubuntu 18.04 LTS.  If you plan to use Windows on this machine as well, you’ll need to set up dual booting.

## OS Installation
Install Ubuntu 18.04 LTS by downloading the image from [Ubuntu’s website](https://ubuntu.com/download/) and imaging it to a USB drive.  

There are tutorials from Ubuntu to install Ubuntu from [Windows](https://ubuntu.com/tutorials/tutorial-create-a-usb-stick-on-windows#1-overview), [Mac](https://ubuntu.com/tutorials/tutorial-create-a-usb-stick-on-macos#1-overview) and [Ubuntu](https://ubuntu.com/tutorials/tutorial-create-a-usb-stick-on-macos#1-overview)  I like to use [Rufus](https://rufus.ie/) for imaging if using a Windows machine.  Boot up the computer with the USB drive and follow the installation instructions.  You may need to set the BIOS up to boot from the USB drive.  Make sure your computer is connected to the Internet before starting the installation and it will fetch and install any updates while installing.  If your computer was not connected to the internet during installation be sure to run the following commands once it is connected:

```
sudo apt update
sudo apt upgrade
```

### Why Ubuntu?
Ubuntu is a distribution of Linux maintained by Canonical.  It is free and open-source and fairly user friendly with lots of community support available on the Internet.

### Install ssh server
I recommend having a ssh server set up to log into your machine.  This will be especially useful if you plan on having the computer be headless (no monitor attached).

```
sudo apt update
sudo apt install openssh-server
cd /etc/ssh
sudo chmod 777 sshd_config
```

## GPU Acceleration
Install the proprietary NVIDIA drivers as well as CUDA and CUDNN to get the full capability of your NVidia GPUs.

You can go to the official NVIDIA website and find which version of the display driver you should use.  You can download the drivers from the website or you can install from the terminal with the following command:
```
sudo apt-get install nvidia-<version number>
```
Then reboot and ensure the drivers installed by typing:
```
nvidia-smi
```
If you’re using the GUI you can also go to Additional Drivers and select the correct driver for you GPU.

## Download and Install CUDA
You can go to the CUDA Toolkit website and get the commands for the latest version.  For 10.2 on Ubuntu 18.04 use the following commands:
```
wget http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda_10.2.89_440.33.01_linux.run
sudo sh cuda_10.2.89_440.33.01_linux.run
```
## Download and Install CUDNN
Go to the [NVIDIA cuDNN](https://developer.nvidia.com/cudnn) home page and sign in with your account or create a new one if you don't have one yet.

Download CUDNN 7.x for CUDA 10.2. 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
```

## Python Virtualenv
We’re going to install Python virtualenv to enable creating isolated Python environments.  This will help with dependency management.  

1. First make sure you have all the prerequiestes:
```
sudo apt install python-dev python3-dev python-pip virtualenv
sudo pip install virtualenv virtualenvwrapper
```
Then add the following to your bashrc:
```
# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
```
2. Create a new virtual environment named 'virtenv' and specify the python version you like to use on it. 
```
virtualenv -p python3.5 virtenv
```
3. Activate the virtual environment
```
source virtenv/bin/activate
```
4. You can deactivate the virtenv by typing 
```
deactivate
```
Make sure you are in your virtual environment and install the following with pip.  Pip is a Python package manager and makes installing software very easy.
```
pip install numpy
pip install scipy
pip install pandas
pip install matplotlib
```
Check the installed packages in the virtual environment using: 
```
pip list --format=columns
```
## Machine Learning Packages
Let's now install some of the more popular Machine Learning packages.

### Install OpenCV
OpenCV 3.2.0 can be installed from the Ubuntu 18.04 official repository. 
```
sudo apt install python3-opencv
```

### Install Scikit-learn
```
pip install scikit-learn
```

### Install Tensorflow. 
```
pip install --upgrade tensorflow-gpu
```

### Install Keras
```
pip install keras
```

### PyTorch Development Environment
Now it is time to setup the python environment for PyTorch development.
```
mkvirtualenv pytorch -p python3 --system-site-packages
```
Switch to the pytorch virtual environment and then install the packages:
```
pip install torch torchvision
```

## Summary
Hopefully this guide was useful in getting your Machine Learning Box with Ubuntu set up.  If you run into trouble be sure to ask the Ubuntu community for help, they are usually a friendly group and more than happy to help.

<a href="https://dev.to/leading-edje">
  ![Smart EDJE Image](https://dev-to-uploads.s3.amazonaws.com/i/5uo60qforg9yqdpgzncq.png)
<a/>