Is there a method to run an Anaconda environment in Google Colab?

Question:

I am having a YML file for a Conda environment that run with Python 3.8.15 (environment.yml). I am currently trying to load that file into my Google Colab, based on this answer:
conda environment in google colab [google-colaboratory].

!wget -c https://repo.anaconda.com/archive/Anaconda3-2022.10-Windows-x86_64.exe
!chmod +x Anaconda3-2022.10-Windows-x86_64.exe
!bash ./Anaconda3-2022.10-Windows-x86_64.exe -b -f -p /usr/local

And while the executable file for Anaconda was installed in my Google Drive folder, when I run the code, it turms out that Colab could not execute that file:

./Anaconda3-2022.10-Windows-x86_64.exe: ./Anaconda3-2022.10-Windows-x86_64.exe: cannot execute binary file

Is there any other method that I could use to install Anaconda to work with it in Google Colab? And furthermore, how should I load my environment.yml file after getting Anaconda to run in Colab?

Asked By: Hoang Cuong Nguyen

||

Answers:

Google Colab runs on Linux rather than Windows. Miniconda, a stripped down version of Anaconda, can be installed:

!wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
!chmod +x Miniconda3-latest-Linux-x86_64.sh
!bash ./Miniconda3-latest-Linux-x86_64.sh -b -f -p /usr/local

Then:

!conda env create -f environment.yml
!source activate myenv && conda env list
Answered By: Amir194

You cannot run a Jupyter notebook (Colab session) with a new Conda environment, but you can use Conda to augment the packages in the existing Python installation. Installation is streamlined with condacolab. See the condacolab documentation.

An quick example would go something like:

First Cell

!pip install -q condacolab
import condacolab
condacolab.install() # expect a kernel restart

Second Cell

mamba install [pkg1 pkg2 ...]

# or, if you have a YAML
mamba env update -n base -f env.yaml

In the YAML case, you must match the same version of Python with the Colab version.

Answered By: merv