How to Setup a Python Environment for Machine Learning on Linux or Ubuntu Machine
This tutorial covers a short and straight forward step by step procedure required to set up a flexible machine learning environment on a Linux/ubuntu machine using Anaconda.
Installing Anaconda
- Download the Anaconda package from the Anaconda official website
cd /tmp
curl -O https://repo.anaconda.com/archive/Anaconda3-2019.10-Linux-x86_64.sh
Remember, you can change the <2019.10>
to the latest version number
2. To check the integrity of the installer
sha256sum Anaconda3-2019.10-Linux-x86_64.sh
3. Install Anaconda by running the downloaded installer script
bash Anaconda3-2019.10-Linux-x86_64.sh
Save(write down) the displayed directory: we will use it later
Follow the below steps to complete the installation
- Press Enter
- Type “yes”
- Confirm the installation location
- Initialize Anaconda by answering “yes” when it gets to the following step
If you forget to say yes to the step shown below , use the code below to modify your shell scripts
eval "$(/home/cc/anaconda3/bin/conda shell.bash hook)"
4. Acitavate installation
source ~/.bashrc
You successfully installed and activated Anaconda
Test installation
Test the installation by listing the installed packages
conda list
Creating Anaconda Environment for Machine Learning Projects
In this section, we will create an environment called “torch_env” that will hold various machine learning libraries and packages.
- Creating the Anaconda environment and we name it “torch_env”. You can give any other name
conda create -n torch_env python=3.6
2. Activate the newly created environment: Once it finishes creating the environment activate it as follows
conda activate torch_env
Don’t forget to always activate your environment before you install anything or start to work on your project
Now we are in the environment, so let’s start installing important machine learning libraries and frameworks. The environment helps to structure your work, to easily share it, to easily replicate it.
- Install pip to your environment directory
conda install pip
Now we will install the machine learning libraries! We’ll go with the most commonly used ones:
- NumPy: for any work with matrices, especially math operations
- pandas: data handling, manipulation, and analysis
- Pytorch: Deep learning framework
Here’s a simple trick to install all of those libraries in one quick shot! Create a requirements.txt
file and list all of the packages you wish to install. Your requirements file looks like this
Write down the following libraries and save them as requirements.txt
(use this link to learn how to use vim editor for Linux)
vim requirements.txt
requirements.txt
Then install the requirements file
python3 -m pip install -r requirements.txt
To verify you have installed the packages
- Open Python
python
- Import the libraries