Setting Up a PC for Machine Learning

In the fast-evolving landscape of machine learning, having a well-equipped PC is essential for seamless development. This guide outlines the step-by-step process to set up your machine learning workstation, starting from the installation of the Ubuntu operating system to configuring essential tools like Conda, Nvidia drivers, and text editors.

Here’s the table of contents:

  1. Installing Operating System
  2. Disk Partitions
  3. Setting up Browser
  4. Installing Nvidia Drivers and CUDA
  5. Installing Python with Conda
  6. Navigating Jupyter and Conda Environments
  7. Testing CUDA
  8. Installing Kinto (Optional for Mac Keyboard Layout)
  9. Choosing a Text Editor
  10. Conclusion

Installing Operating System

The first step is to select an operating system. For tech enthusiasts, the choice between Ubuntu and Windows is straightforward. Ubuntu, as an open-source and free software, is actively maintained and inherently safer than Windows.

When installing Ubuntu for the first time, it’s advisable to opt for a stable version. I initially chose 22.04, but encountered GPU issues. Researching online, I discovered that 20.04 currently enjoys comprehensive software support. I installed the Desktop version as I require a Graphical User Interface. Here’s how to set up Ubuntu Desktop:

  1. Download the Ubuntu .iso file. For 20.04, I obtained the Ubuntu Desktop from this link.

  2. Install BalenaEtcher and have a USB pendrive handy. Creating a bootable pendrive with BalenaEtcher’s intuitive GUI takes approximately 10-20 minutes.

  3. Plug the Pendrive into your PC, start it up, and press F11 multiple times to access the boot device selection. Choose the pendrive to begin the Ubuntu installation.

Disk Partitions

In Ubuntu most of your data will be saved in /home folder where users data is present. Linux systems can break anytime and separating /home data from OS data is recommended so that whenever system break we reinstall OS without touching /home dir. It is supported using disk partitions. While installing Ubuntu it gives nice GUI for creating partitions in disk using that mount a separate partition for /home and / [root].

If you are late like me then don’t worry. flow below blog post from ubuntu to setup it

Ubuntu Partitioning/Home/Moving

While installing Ubuntu I had 512GB drive from which I assigned 150GB to root [/] and 32GB as swp memory and remaining for /home dir. Somehow /home was not mounted and I found out when I got disk full. Following above blog solved my issue.

Setting up Browser

A programmer’s initial task on a new operating system is to install a browser. While Ubuntu comes preloaded with Firefox, I prefer Chrome due to its seamless integration with Google accounts. After logging into Chrome with my Gmail, activating sync restores my extensions, bookmarks, and login credentials within just 5 minutes. To install Chrome, open Firefox and visit the Chrome website, then click the download button. On Ubuntu, a .dmg file will be downloaded. Double-clicking it opens the Ubuntu Software window with an install button. After installation, turning on sync brings back all your settings.

Installing Nvidia Drivers and CUDA

On Ubuntu 20.04, installing Nvidia drivers is as simple as a single line:

sudo apt install nvidia-driver-440

Installing Python with Conda

Conda serves as an environment management system for Python, simplifying version management. Using a single environment for everything can eventually lead to version conflicts. Conda is ideal for creating separate environments for individual projects. Follow these steps to install Conda: link.

Download the Anaconda-latest-Linux-x86_64.sh file and execute the following command:

bash Anaconda-latest-Linux-x86_64.sh

To create a new Python environment with a specific version, use this one-liner:

conda create -n <env_name> python=3.10

To activate environment

conda activate <env_name>

Optional: Similar to Conda for Python, consider installing Node Version Manager (NVM) and Ruby Version Manager (RVM) for Node.js and Ruby. It’s safer to use version managers instead of direct installations.

Navigating Jupyter and Conda Environments

Jupyter comes pre-installed within the conda base environment. However, there are instances where we might want to utilize Jupyter in conjunction with a specific environment we’ve created. Simply activating the desired environment and launching Jupyter won’t suffice. A series of steps must be taken to seamlessly integrate your environment with Jupyter.

Commence by installing the ipykernel package to your designated environment:

conda activate <env_name>
conda install -c anaconda ipykernel

Having completed the installation, proceed to incorporate your environment into Jupyter:

python -m ipykernel install --user --name=<env_name>

With this integration, your <env_name> environment becomes accessible within Jupyter. To utilize it, navigate to Kernel > Change Kernel and select your preferred kernel before executing your code.

Testing CUDA

Before proceeding, it’s wise to test the Nvidia driver installation. Run the following command:

nvidia-smi

If this command displays GPU statistics, the driver installation is successful. Next, check CUDA compatibility.

Install PyTorch in the base Conda environment:

pip install torch

Open the Python interpreter in interactive mode:

python

Run the following lines one by one:

import torch
torch.cuda.is_available()
# Output: True
example = torch.tensor([0.2, 0.5])
example.to('cuda')
# Output: tensor([0.2000, 0.5000], device='cuda:0')

If there are no errors and the output matches, your system is ready for handling large models on the GPU.

Installing Kinto (Optional for Mac Keyboard Layout)

Having used a Mac for some time, I became accustomed to its keybindings. Switching to Windows keyboard layouts was a challenge, so I opted to use a Mac keyboard with Ubuntu. Kinto.sh is a software that enables Mac-style shortcuts on Ubuntu. Installing Kinto requires running this relatively lengthy command:

/bin/bash -c "$(wget -qO- https://raw.githubusercontent.com/rbreaves/kinto/HEAD/install/linux.sh || curl -fsSL https://raw.githubusercontent.com/rbreaves/kinto/HEAD/install/linux.sh)"

Choosing a Text Editor

Next, install your preferred text editor—whether it’s VS Code, Vim, NeoVim, or PyCharm. You can directly install VS Code from the Ubuntu Software Store. For Vim and NeoVim, one line in the terminal is sufficient:

sudo apt-get install vim

sudo apt-get install neovim

For PyCharm, visit their website.

Conclusion

Setting up a PC for machine learning is a vital step in establishing a robust development environment. With Ubuntu as the chosen operating system, the foundation is laid for a safe and actively maintained platform. The process of installation, from selecting the version to configuring the desktop, ensures a smooth start to your journey.

This guide offers a comprehensive roadmap for initial setting up a machine learning-ready PC, equipping you with the essential tools to embark on your coding and learning endeavors with confidence and efficiency.

Written on July 22, 2023