Installing Anaconda to Setup a Machine Learning Environment

I am trying to set up a machine learning environment on my laptop. For this purpose, I am primarily a Python user. So I will need all the Python libraries. I will also setup things in an python environment. So that I can keep my workplace neat. Creating a pyhton environnment is necessary specially if different project has different version requirements. Specially if we want to work on some legacy project, which I plan to work on. I am running Ubuntu 18.04 LTS. I am documenting for my own reference, so that I know what I have done previously.

Step 1 – Download

I start with googling and going to the anaconda page. I click on the linux under the regular installation thing. Aparently, as shown in the page, we have two options for installing Anaconda miniconda (needs around 400MB disk space) and full anaconda (needs around 3GB disk space). Anaconda has all the necessary packages, while miniconda will have only the basic thing installed, so that we can install everything later. We will not have a lots of unnecessary things installed. I decided to install miniconda, so I go to this link for downloading miniconda. As shown in the following figure, I download the 64-bit (bash installer) on my Desktop for Python 3.6 as shown below.

Step 2 – Install

Now we need to install miniconda. Lets open a terminal and change directory to Desktop (or the location of the file)

shant@shanto:~$ cd ~/Desktop/
shant@shanto:~/Desktop$ ls
Miniconda3-latest-Linux-x86_64.sh
shant@shanto:~/Desktop$ clear

shant@shanto:~/Desktop$ bash Miniconda3-latest-Linux-x86_64.sh 

Welcome to Miniconda3 4.5.4

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>> 

Pressing ENTER will let the installation process continue as shown below. I will continue with all the default options as shown highlighed in the following.

===================================
Miniconda End User License Agreement
===================================

Copyright 2015, Anaconda, Inc.

All rights reserved under the 3-clause BSD License:
... ...
... ...
for client/server applications by using secret-key cryptography.

cryptography
    A Python library which exposes cryptographic recipes and primitives.


Do you accept the license terms? [yes|no]
>>> yes
Miniconda3 will now be installed into this location:
/home/shant/miniconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/home/shant/miniconda3] >>> 
PREFIX=/home/shant/miniconda3
installing: python-3.6.5-hc3d631a_2 ...
Python 3.6.5 :: Anaconda, Inc.
installing: ca-certificates-2018.03.07-0 ...
... ...
... ...
installing: requests-2.18.4-py36he2e5f8d_1 ...
installing: conda-4.5.4-py36_0 ...
installation finished.
Do you wish the installer to prepend the Miniconda3 install location
to PATH in your /home/shant/.bashrc ? [yes|no]
[no] >>> yes

Appending source /home/shant/miniconda3/bin/activate to /home/shant/.bashrc
A backup will be made to: /home/shant/.bashrc-miniconda3.bak


For this change to become active, you have to open a new terminal.

Thank you for installing Miniconda3!
shant@shanto:~/Desktop$ 

Miniconda should already be installed. We can check the installation by typying any conda command as shown below. We then update conda to the latest version.

shant@shanto:~$ conda --version
conda 4.5.4
shant@shanto:~$ conda update conda
Solving environment: done

## Package Plan ##

  environment location: /home/shant/miniconda3

  added / updated specs: 
    - conda


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ... ...
    ------------------------------------------------------------
                                           Total:         4.6 MB

The following packages will be UPDATED:

    ... ...

Proceed ([y]/n)? y


Downloading and Extracting Packages
conda-4.5.10         |  1.0 MB | ############################################################################################### | 100% 
openssl-1.0.2p       |  3.5 MB | ############################################################################################### | 100% 
certifi-2018.8.13    |  138 KB | ############################################################################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
shant@shanto:~$ 

Step 3 – Setting up Environment

Now we can create and manage environments using Anaconda. It is always better to use different environment for different purpose and install necessary package in a particular environment, so that each environment is orthogonal and does not interfere with each other. We can see the existing environments using the command conda info –envs. As we have just installed Anaconda there should just be only one ‘base’ environment, and we can activate the ‘base’ environment using source activate base, as shown below (the activated environment will be shown within a first bracket). In the activated environment we can type which python, to check that Python is running from our miniconda installation.

shant@shanto:~$ conda info --envs
# conda environments:
#
base                  *  /home/shant/miniconda3

shant@shanto:~$ source activate base
(base) shant@shanto:~$ which python
/home/shant/miniconda3/bin/python
(base) shant@shanto:~$ 

Now we can add a new environmet. First lets deactivate the current environment ‘base’ by typing source deactivate. While creating a new environment using conda, we can proceed with the original version of our Python (3.6 in this case) installation or we can specify any other version of Python (say 3.5 or 2.7). We will create two different environments for python 3.6 and python 2.7 namely ml36 and ml27 respectively. The primary purpose of these environments are doing machine learning. After installation we can check if they are really installed using the command for getting the environment names. Then we can switch between different environments and check if we have the right version of Python. The commands for all these tasks are highlighted.

(base) shant@shanto:~$ source deactivate
shant@shanto:~$ conda create --name ml36 python=3.6
Solving environment: done

## Package Plan ##

... ...
... ...
# To deactivate an active environment, use
#
#     $ conda deactivate

shant@shanto:~$ conda create --name ml27 python=2.7
Solving environment: done

## Package Plan ##

  environment location: /home/shant/miniconda3/envs/ml27

  added / updated specs: 
    - python=2.7

... ...
... ...
shant@shanto:~$ conda info --envs
# conda environments:
#
base                  *  /home/shant/miniconda3
ml27                     /home/shant/miniconda3/envs/ml27
ml36                     /home/shant/miniconda3/envs/ml36

shant@shanto:~$ source activate ml36
(ml36) shant@shanto:~$ python --version
Python 3.6.6 :: Anaconda, Inc.
(ml36) shant@shanto:~$ source deactivate
shant@shanto:~$ source activate ml27
(ml27) shant@shanto:~$ python --version
Python 2.7.15 :: Anaconda, Inc.
(ml27) shant@shanto:~$  

Step 4 – Installing Packages

Now we can install the necessary packages into any environment depending on our need. Lets activate ml36 and install some necessary packages for machine learning. First we will install Jupyter notebook, which is an essential tool for interactive data analysis and experimentation.

The command conda list will provide a list of the already installed packages. After installing Jupyter notebook, we will install pandas, spyder, numpy, scikit-learn, tensorflow, keras, pyyaml, h5pymatplotlibseaborn, argparse, pytorch,  keras.

shant@shanto:~$ source activate ml36
(ml36) shant@shanto:~$ conda list
# packages in environment at /home/shant/miniconda3/envs/ml36:
#
# Name                    Version                   Build  Channel
ca-certificates           2018.03.07                    0  
certifi                   2018.8.13                py36_0  
... ...
... ...

xz                        5.2.4                h14c3975_4  
zlib                      1.2.11               ha838bed_2  
(ml36) shant@shanto:~$ conda install jupyter
Solving environment: done

## Package Plan ##

  environment location: /home/shant/miniconda3/envs/ml36
... ...
... ...

Verifying transaction: done
Executing transaction: done
(ml36) shant@shanto:~$ jupyter-notebook
[I 02:27:50.100 NotebookApp] Writing notebook server cookie secret to /run/user/1002/jupyter/notebook_cookie_secret
[I 02:27:50.332 NotebookApp] Serving notebooks from local directory: /home/shant
[I 02:27:50.332 NotebookApp] The Jupyter Notebook is running at:

At this moment Jupyter notebook installation is done and it should open in a tab on our default browser after the command jupyter-notebook as shown in the following Figure.

However, if we click on the down arrow on the New button on the top right corner, we do not see the new environment (ml36) in here. To solve this issue we need to instal nb_conda using the command conda install nb_conda on the terminal. Now if we open Jupyter by typing jupyter-notebook on the terminal we see that we have our environment listed in Jupyter notebook as shown below.

Now lets install the packages using the following commands on the terminal while the conda environment is active.

(ml36) shant@shanto:~$ conda install scipy
... ...
(ml36) shant@shanto:~$ conda install pandas
... ...
(ml36) shant@shanto:~$ conda install spyder
... ...
(ml36) shant@shanto:~$ conda install -c conda-forge tensorflow
... ...
(ml36) shant@shanto:~$ conda install -c conda-forge keras
... ...
(ml36) shant@shanto:~$ pip install matplotlib seaborn argparse
... ...
(ml36) shant@shanto:~$ conda install scikit-learn
... ...
(ml36) shant@shanto:~$ conda install -c anaconda xlrd
... ...
(ml36) shant@shanto:~$ conda install -c anaconda beautifulsoup4
... ...
(ml36) shant@shanto:~$ conda install -c bokeh bokeh
... ...
(ml36) shant@shanto:~$ conda install -c bokeh/label/dev bokeh
... ...
(ml36) shant@shanto:~$ conda install -c conda-forge ipywidgets
... ...
(ml36) shant@shanto:~$ conda install pytorch-cpu torchvision-cpu -c pytorch
... ...

I just went with all the default options while installing the packages. As shown in the gist below, all of them working perfectly. We need to remember that, depending on the environment (with or without GPU) we need to install the right version of PyTorch. As I am installing everything on my laptop which does not have a GPU, I selected a CPU version of PyTorch. The necessary command depending on the environment can be generated from the PyTorch official website.