DEV Community

Cover image for How to install and run conda on Google Colab
Hetal Patel
Hetal Patel

Posted on

How to install and run conda on Google Colab

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

conda version

If yes! then you will see this output.

conda exist

If no, then this output.

no conda

# Which Python and its version

command

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.10 by default. Now it’s miniconda version 4.12.

# Check if the PYTHONPATH variable has been set

PYTHONPATH

path
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.

path command

pathss

# Installing Miniconda

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

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-py310_24.1.2-0-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).

# Which Conda and its version

conda chk

version

# 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.

sys command

Any package that you install with Conda will be installed into the directory /usr/local/lib/python3.10/site-packages

So you will need to add this directory to sys.path in order for these packages to be available for import.

import sys

sys opt

# Installing Packages

Now, install any required package as:

conda done

Yes! You are done.

Top comments (0)