Setting up Ubuntu and Docker for deep learning

Background

This is Part 2 of 3 in a tutorial series for getting started with GPU-driven deep learning. These tutorials are intended for anyone who is interested in figuring out what it takes to get started with deep learning on a personal machine that costs less than $2000. This assumes you have some familiarity with machine learning and deep learning. This series will cover:

  1. Part 1: How to build a GPU deep learning machine
  2. Part 2: Setting up Ubuntu and Docker for deep learning
  3. Part 3: Using deep learning with style transfer

Recommended Reading

Other Useful References

Linux Shortcuts

  • ctrl+alt+arrow_key will switch to another workspace.
  • ctrl+alt+t will open a terminal window.
  • ctrl+alt+f1 will take you to the console log in.
  • ctrl+alt+f7 will take you to your current xorg session.

Terminal Shortcuts

  • Pressing the up and down arrows will cycle through previous commands.
  • mkdir <new_directory_name> will create a new folder in your current directory.
  • cd <target> will move you to the target in your directory tree. <target> could be the name of a folder in your current directory, it could be .. to go to the parent directory, or it could be a file path.
  • ls or ls -l will list the contents of your current directory.
  • ctrl+shift+c and ctrl+shift+v will cut and paste, respectively.
  • ctrl+c will cancel a command.
  • ctrl+shift+t will open a new tab in your current terminal window.
  • nvidia-smi will show you information about your NVIDIA GPU.
  • More shortcuts

Installing Linux

If the only operating system (OS) you’ve worked with is Mac OS or Windows, Linux can be frustrating and intimidating. Mac and Windows are largely designed to allow a user to interact with the computer through a graphical user interface (GUI) while certain system-level changes are done unbeknownst to the user. Even though different versions of Linux (called “flavors” in Linux jargon) have various GUIs available, using Linux typically means having to become familiar with the command line, Terminal.

Ubuntu 16.04 Installation Instructions

In this section I’ll be showing you how to set up Ubuntu 16.04.1 LTS. You’ll need the following items:

  • An ancillary computer that’s connected to the internet.
  • A USB flash drive that can be erased and formatted to FAT32.
  • Internet connection available for your deep learning machine.

Before we begin I should be clear that at some point you’re going to encounter a problem that I haven’t experienced or addressed. In fact, this paragraph has been added to this tutorial after recently logging in to my machine, only to encounter a login loop that occurred when my NVIDIA driver somehow lost its connection with the GPU. I solved it using the first comment suggested on this ask ubuntu forum. If you encounter an issue, follow the rubber ducky guide for debugging your problem. Chances are, someone else has encountered the same problem you’re experiencing, they posted a question about it, and someone else who encountered the same problem provided a solution! Sometimes, you will try many solutions before you figure it out, and sometimes you will have to start all over. :)

On an ancillary computer (I’ll be using a Mac) complete the following steps:

  1. Download the Ubuntu 16.04.1 LTS ISO image. You may want to verify the download, but I skip this step.
  2. Insert your USB flash drive. Erase the drive, formatting it to FAT32, and name it “FAT32”. On Mac, you can do this in the “Disk Utility” program, and you’ll want to select the “MS-DOS (FAT)” format. Note, naming the drive “FAT32” is a potential solution to a problem that can occur in Step 3.
  3. Download UNetbootin. Once it’s downloaded, insert your flash drive and open UNetbootin. Check to see if your flash drive is visible under “drives”. If it is, select it. If it is not, there could be several problems.
  4. Once your flash drive is detected in UNetbootin, select the ellipses to navigate to the location of the Ubuntu ISO download. Note that no files appeared when I selected the “Downloads” folder from my “Favorites”. Instead, I could access the file by navigating through the Users directory that first appeared when I clicked on the ellipses or by using the search bar.
  5. Select “Ok” to install the ISO on your flash drive. The installation process can take a few minutes. If everything goes well, you should have something that looks similar to the following:
Before and After — Successful UNetbootin install of Ubuntu 16.04 ISO on a flash drive

Now you’re ready to install Ubuntu on your deep learning machine! Note, the next section will assume your deep learning machine is starting off where Part 1: Building a GPU-focused Machine left off.

On your deep learning machine:

  1. Insert your flash drive that has the Ubuntu ISO installed on it. Power the computer on, and pull up the BIOS by pressing the appropriate key during boot up. To find the correct key, consult your motherboard booklet. In my case, it’s f11.
  2. Depending on your motherboard, behavior at this point can vary. You want to navigate to the flash drive and boot it up. In my case, a menu with the available boot devices appears after pressing f11, and I then select the flash drive with the ISO image, which happens to be called “UEFI OS (SanDisk Cruzer Glide 1.27)”.
  3. The GNU GRUB menu will appear. Select “Install Ubuntu” and continue.
  4. The installing menu will appear. Select your language and continue.
  5. You have two options under “Preparing to install Ubuntu.” I will select “Download updates while installing Ubuntu”, but I will not be selecting the other option. Continue.
  6. You have to select the Installation type. If you have other drives with operating systems already installed, you may be able to see them here. I will select “Erase disk and install Ubuntu” since the installer will allow me to select my empty SSD in the next step. Continue.
  7. Be very careful to select the appropriate drive for the target OS installation. If you only have one SSD plugged in to your computer, then you’re fine. However, if you have other drives plugged in then you could accidentally erase one unintentionally. If you’ve selected an entirely empty drive, then advanced partitioning options should not come up. Once you have the appropriate drive chosen, select “Install Now”. Then, when it asks you to “write changes to disk”, select “Continue”.
  8. Select your time zone and then continue.
  9. Select your keyboard layout and then continue.
  10. Enter your name, computer name, and username. Your name can be whatever you want it to be. Your username will be the name of the account that you use to log in to the operating system. I recommend giving your computer a different name than your username. When using the command line, it will provide greater clarity for what permission-levels are being used. Select “Continue”.
  11. Ubuntu is now being installed on your SSD. This process can take a few minutes. Once it is complete, you will be prompted to restart your computer. Restart and log in to Ubuntu.

If you’ve made it this far, then congratulations! You’ve successfully installed the OS. Now we’re going to set up the appropriate drivers for the GPU.

Installing the NVIDIA Display Driver

When you log in to the OS, everything should appear a little big and fuzzy. This is because we need to install the NVIDIA driver for our GPU so that the OS can process graphical information on it rather than on the motherboard.

There are several ways you can install the NVIDIA driver, and I’ve had most consistent success with the following:

  1. In your browser go to the NVIDIA downloads page.
  2. Enter the information that is appropriate for your GPU. In my case, I’m selecting “GeForce”, “GeForce 10 Series”, “GeForce GTX 1070”, “Linux 64-bit”, and “English” for the respective fields. Select “Search”.
  3. The appropriate display driver will come up. In my case, it is Version “375.26”, Release Date “2016.12.14”, and Operating System Linux “64-bit”.
  4. Select “Download” then “Agree and Download”. Save the file. In my case, the name of the file is NVIDIA-Linux-x86_64–375.26.run.

Now we’re going to use Terminal to install the display driver.

  1. On your keyboard, select ctrl+alt+f1 to switch to console login.
  2. You will be prompted to log in to your computer. Type your user name and select enter. Next, type the password for your user and select enter.

Now, you will see a command prompt that looks like the following:

<user_name>@<computer_name>:~$

The user name is whichever user you’re currently using, and the computer name is whatever machine you’re currently logged into.

Now type:

sudo service lightdm stop

The sudo command tells the computer that you want to take an action with root-level privileges. In general, you should be cautious any time you read instructions that tell you to use sudo, and you should have a good idea of what the commands you’re issuing are doing. We’re telling the computer to stop service and lightdm so that we can turn off x-server according to these NVIDIA driver installation instructions.

X-server and lightdm should now be turned off. You can confirm this by selecting ctrl+alt+f7 on the keyboard to switch to your current xorg session. The GUI that you normally have should be turned off, and you should just see a text description of the drive you’re currently logged in to.

Now, switch back to the console window by selecting ctrl+alt+f1 on the keyboard. Now type ls or ls -l to display the contents of the directory that you’re currently in. Navigate to the folder that contains your NVIDIA driver (see Terminal Shortcuts above). In my case, I’ll type cd Downloads. Note that the names of folders and files will be case sensitive.

Now, type ls to confirm that the NVIDIA display driver is in the current directory. If it is, type sudo sh <name_of_display_driver>, select enter, and then enter your password. In my case, I will type sudo sh NVIDIA-Linux-x86_64-375.26.run. Now the NVIDIA display driver installation will begin.

  • You will be prompted with the Terms of Service. Select “Accept”.
  • You may receive the following alert: “The distribution-provided pre-install script failed! Are you sure you want to continue?”. If you receive this, simply select “Continue Installation”.
  • You may receive an alert telling you that the 32-bit compatibility libraries will not be installed. Select “Ok”.
  • You will be prompted with the following question: “Would you like to run the nvidia-xconfig utility to automatically update your X configuration file so that the NVIDIA X driver will be used when you restart X? Any pre-existing X configuration file will be backed up.” Select “Yes”.
  • You should receive a prompt telling you that the X configuration file has been successfully updated. Select “Ok”.

At this point the driver should be successfully installed, and now we need to restart our system. Type sudo reboot and select enter.

Now when your operating system loads, the GUI should appear to have a much higher resolution than it did before. Log in to your account. To verify that the driver was successfully installed:

  1. Open a terminal a terminal window by selecting ctrl+alt+t.
  2. Type nvidia-smi and select enter.

If the display driver was installed properly, then you will see information about your GPU that looks like the following:

If you’ve made it this far then your display drivers are successfully installed, and you’re ready to move on to installing Docker!

Optional Installations & Configurations

At this point you may want to install some preferred software, or you may want to configure the OS settings to be more comfortable. Here are some of the changes that I make:

Installing Docker

Prior to using Docker, I managed the various NVIDIA CUDA drivers and deep learning libraries manually. This was a painful process, and I had to wipe my drive and reinstall the OS several times before finally getting it to work. Even then, updating a single library was treacherous territory.

Docker is a fantastic resource that “containerizes” your software environments. It allows you to create a complete and independent file system that runs software without making any system level changes to the host computer. It’s also convenient because you can share a Docker “image” with someone else, and they can run the container on their local machine without going through the burden of setting up their system to match your own. In fact, you can use a container to run an entire operating system that’s different than the operating system on the host.

Docker Installation Steps

Follow the official instructions to install Docker. Note, in Step 6 of “Update your apt sources” I used the following command:

echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" | sudo tee /etc/apt/sources.list.d/docker.list

Install the recommended packages under “Prerequisites by Ubuntu Version”.

Once you get to the header, “Install a specific version”, you can stop. I recommend taking one more step to configure Docker as non-root user, so that you don’t have to issue the sudo command when you run Docker.

Installing NVIDIA Docker Plugin

NVIDIA Docker is a plugin that grants Docker more robust use of NVIDIA GPUs. Install NVIDIA Docker by following the Ubuntu distribution directions. Note, if you’re using a Fish shell, you’ll want to switch to a Bash shell by typing bash before you begin the NVIDIA Docker installation process.

Becoming Familiar with Docker

The Docker page offers a thorough tutorial for learning how to use Docker. I recommend going through it. I also highly recommend going through this other tutorial that has a data science focus and provides some useful tips.

Congratulations! Now you’re ready to dive into deep learning using neural style transfer! Part 3: Using deep learning with style transfer.

This is a living document, subject to change. If you find any inaccuracies or have any comments, reply below!
Like what you read? Give Socratic Datum a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.