How to install conda on Google Colab

Easily install Python libraries and their dependencies

Introduction

Conda is a Python library management tool that can help you to effortless install libraries and their dependencies. In this article, I will show you how you can install conda on Google Colab (also works for Jupyter notebooks as well). Let’s dive in!

1. Installing conda in Google Colab

Step 1

The first thing that you want to do is launch a new Google Colab notebook.

Step 2

Next, insert a code cell and paste the following block of code:

################################################################################
# INSTALL CONDA ON GOOGLE COLAB
################################################################################
! wget https://repo.anaconda.com/miniconda/Miniconda3-py37_4.8.2-Linux-x86_64.sh
! chmod +x Miniconda3-py37_4.8.2-Linux-x86_64.sh
! bash ./Miniconda3-py37_4.8.2-Linux-x86_64.sh -b -f -p /usr/local
import sys
sys.path.append('/usr/local/lib/python3.7/site-packages/')

Just in case you’re wondering what did the above code just did?

  • Lines 1–3 — are just comments that serve as a note to self for telling us what this block of code is doing.

  • Line 4 — The wget command is run in the BASH terminal to download the installation script file Miniconda3-py37_4.8.2-Linux-x86_64.sh directly from the Anaconda website.

  • Line 5 — The chmod +x command is run in the BASH terminal to add executable permission (allow the file to be run).

  • Line 6 — The installation script file is run in order to install conda.

  • Lines 7–8 — The site-packages directory is added to path

Step 3

Click on the play button found to the left of the code cell to run this code cell.

Screenshot of how to install Conda in Google Colab.

After a few moments, you should see that conda has been successfully installed.

Screenshot of conda installing on Google Colab.

2. Installing a library in conda

It should be noted that originally you had only one option for installing libraries in Python, which is

pip install pandas

where in the example above we are installing the pandas library.

Now that conda is installed in Google Colab, you have just activated a second option. So how does one go about installing libraries using conda?

To install libraries in Python type the following:

conda install pandas

However, some libraries are hosted on specific conda channels and therefore you need to specify the channel with the -c tag. For example, the rdkit library normally require us to perform a lengthy installation of several prerequisite libraries. However, instead we can use conda to easily install the rdkit library as shown below:

conda install -c conda-forge rdkit

where -c conda-forge represents a conda channel called conda-forge as rdkit is the name of the library to be installed.

3. Conclusion

In a typical use-case I would leave the above code cells for installing conda and necessary libraries at the top portion of the notebook, prior to the actual code that I have for performing the analytics workflow.