Itsi’s guide to environment hygeine with conda

September 9, 2022

You fool, you’ve run into more package installation problems while using conda. Are they not installing for weird reasons you can’t understand? Is the little solving wheel spinning forever?

I’m going to bet that you’re installing everything into your base environment.

Fret not, we can fix it. But it’s time to get environment hygeinic.

Key ideas

  • You should be using a different environment for each project that requires different packages

  • You should keep your base environment as clean as possible, do not install packages into it

What the heck are environments?

Environments are just collections of packages. Different projects require different packages. Packages are dependent on 100s of other packages. When conda is “solving” it is trying to work out which version of all theses packages are required to get everything working together. This problem gets exponentially harder with the more packages you try to get working together, so if you try to work all projects out of one environment you’re going to get different packages colliding into each other and eventually things will not work.

What should I actually do?

  1. Bite the bullet and uninstall Anaconda/conda. Guide can be found here, where you want to follow Full Uninstall AND Simple remove: https://docs.anaconda.com/anaconda/install/uninstall/

  2. Install miniconda. It’s like anaconda but a bare-bones version so you can just install what you need as you go. M1 versions are available: https://docs.conda.io/en/latest/miniconda.html

  3. Set conda-forge as one of your default channels (where you download stuff from): https://conda-forge.org/docs/user/introduction.html

  4. Once you are in your base environment, install the following three packages:

    conda install jupyter nb_conda mamba
  5. NEVER INSTALL ANYTHING ELSE INTO BASE EVER AGAIN (jupyter extensions e.g. jupyter_contrib_nbextensions are allowed)

  6. NEVER USE conda AGAIN. NOW YOU ARE A mamba USER. Wherever you would’ve used conda you just sub mamba in now for everything.

  7. For each new project, start a new environment like this:

    mamba create -n env_name_here ipykernel

    (the ipykernel bit will allow you to select this environment when opening jupyter from your base)

  8. open your environments by doing mamba activate env_name, this is where you install your packages for your project (you may need to follow some instructions to get mamba activate env_name working the first time)

  9. open jupyter from base, you will have an option in jupyter now to pick between your environment kernels (this is what nb_conda in your base and ipykernel in your environments allow you to do)

What the heck is mamba

mamba is a re-implementation of conda in C++ instead of python. It is much faster, downloads things in parallel, and provides more details when dependency issues arise. You also get a cool snake on your screen every time you use it and everyone comes past your desk and goes “cool what’s that”