Conda with Python (Reference Only)

From Stat DS Computing Wiki
Revision as of 02:40, 21 February 2025 by Ajshen (talk | contribs) (Create page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Due to licensing issues with conda, we no longer recommend using Anaconda/Miniconda for package management or virtual environment management. We instead recommend the open source alternative Mamba

See Python virtual environments for how to set up Mamba. Instructions for setting up Anaconda/Miniconda have been migrated here for reference.

Installing Conda

conda for your personal computer

To install conda on your personal computer you can install anaconda or miniconda with an installer (works for Mac, Windows and Linux) or via command line. Anaconda takes up more space and comes with a decent amount of packages installed, whereas miniconda takes up much less space but doesn't come with packages installed (see conda.io for more commentary on the differences. For either installation, check the hash (a unique identifier of the object) to make sure you're not installing a bug (see | conda.io for how to check the hash on different OS). Generally speaking, Anaconda can be useful if you're new to programming. Miniconda has all the package-manager functionalities but requires you to install every single package in the environments, which is generally better if you want to manage your projects from scratch.

To install miniconda head here: docs.conda and anaconda head here: docs.anaconda.

conda for your server

To install conda on the server you need to use the command line all the way through (you cannot download anything from a webpage. As such, I recommend installing miniconda following these steps (for Linux systems):

  1. mkdir -p ~/miniconda3
    1. wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
  2. bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
    
  3. rm -rf ~/miniconda3/miniconda.sh
  4. ~/miniconda3/bin/conda init bash

Similar steps can be followed to install miniconda from the terminal on Mac and Windows systems. See this link. N.B. for Hydras. For reasons related to how Hydras directory system is setup, you will need an extra step to make sure miniconda is initialized and available every time you log into the server. The steps above populate your .bashrc file with a small script that initialized conda at log in, but this file is not sourced by Hydras. Instead, you need to create a .profile file (if it's not already there) and add the following line to it:

source .bashrc

Conda’s virtual environments

A good tutorial on how to create a virtual environment with conda can be found at conda-io

In a nutshell:

  1. To a create new environment with Python installed (note that base is your standard environment) you should run:
    conda create -n yourenvname python=x.x


           where x.x is the python version you’d like to use. I might suggest starting with 3.11 now.

  1. Then to activate your conda environment, just run:
    conda activate yourenvname

           This will probably make your terminal show (yourenvname) ...$, whereas originally it should have read (base) ...$.

  1. To install packages in your local environment: After activating (and before deactivating your virtual environment) you can just do
    conda activate yourenvname
    conda install [package]

           An alternative to the second line is  pip install [package]

  1. To deactivate your environment:
    conda deactivate
  1. To share virtual environments (useful for reproducibility): To share your virtual environment (with collaborators or yourself on the server) you can make a .yml file that contains all the information about the packages you are using by first opening up the virtual environment and then running
     conda env export > environment.yml

           Then you can share the environment.yml file to another computer, and with that computer create a copy of the environment via   conda env create -f environment.yml

More guidance can be found here: conda.io