How to install and run conda on Google Colab

Ambu Vijayan
Towards Dev
Published in
3 min readAug 6, 2022

--

Conda is an open-source system that allows you to manage independent environments and install libraries/packages. It is the recommended environment and package management solution for a number of popular bioinformatics and data science tools.

Google Colab is a free Jupyter notebook environment that runs entirely in the cloud.

Okay, Enough Chit-chat. Let's Get Real.

#Are you sure conda is not already installed in your google colab?

!conda --version

If yes! then you will see this output.

If no, then this output.

# Which Python and its version

!which python
!python --version

This means that, in order to use all of the preinstalled Google Colab packages, you will need to install a version of Miniconda that is compatible with Python 3.7 by default. Now it’s miniconda version 4.12

# Check if the PYTHONPATH variable has been set

!echo $PYTHONPATH

This directory doesn’t seem to exist within the Google Colab filesystem. So it is a good idea to unset the PYTHONPATH variable before installing Miniconda as it can cause problems if there are packages installed.

%env PYTHONPATH=

# Installing Miniconda

Adjust the miniconda to the latest version by checking your python version as I showed above.

# Which Conda and its version

!which conda
!conda --version

# Updating Conda

!conda install --channel defaults conda python=3.7 --yes
!conda update --channel defaults --all --yes

Now you can confirm the update by checking the version number for Conda.

# Appending to the sys.path

You can see the current list of directories that Python will search when looking for modules to import by inspecting the sys.path.

import sys
sys.path

Get an idea of what packages are available by simply listing the contents of this directory. Make sure your python version in the below command is correct.

!ls /usr/local/lib/python3.7/dist-packages

Add this directory to sys.path in order for these packages to be available for import.

import sys
sys.path.append("/usr/local/lib/python3.7/site-packages")

# Adding Channels to Conda

!conda config --add channels bioconda!conda config --add channels conda-forge

# Installing Packages

!conda install trinity -y

# Running Packages

!Trinity

Download this Google Colab from here: https://github.com/ambuvjyn/Codes-for-Medium-Articles/blob/main/Google_Colab_Conda/conda_colabs.ipynb

Remember: It's Developing, You will face bugs.

But for now.

Yes! This Works!

--

--