How to make/load/save a conda environment in google colab/Jupyter

Dvij Kalaria
2 min readMay 30, 2020

To install packages via conda in google colab, the following block of code can be used to setup conda:-

! 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/’)

This will setup your conda package installation path as ‘/usr/local/lib/python3.7/site-packages/’. Now whenever you install new packages they will be installed to this directory. Now, whenever you lose your connection with the server after you shut your work, these files will get deleted thereby removing all the packages you installed via conda. However, as it seems the typical command to load an environment doesn’t quite work with colab. So to avoid this problem, there is a workaround. Say for instance you want to save your environment for future use, simply compress the the directory containing temporary installed packages into a permanent directory preferably into your drive folder which will be portable and thus you can save your environment as follows :-

! zip -r /usr/local/lib/python3.7/sitepackages/ /content/drive/My\ Drive/<env_name.zip>

Now whenever, you have to load an environment uncompress the zip environment file to the local directory by following block of code :-

! unzip /content/drive/My\ Drive/<env_name.zip>

! mv /content/drive/My\ Drive/<env_name> /usr/local/lib/python3.7/site-packages/

Then run the following block of code to activate conda :-

! 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/’)

And there you go, all set, you are ready to work on your environment. Do remember to save the environment before disconnecting, else your progress will be lost

--

--

Dvij Kalaria

Don't take life so seriously, you wont come out of it alive anyways!