Conda with Python (Reference Only): Difference between revisions
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
[[Category:Containers and Virtual Environments]] | |||
Due to [https://www.datacamp.com/blog/navigating-anaconda-licensing 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 [https://mamba.readthedocs.io/en/latest/index.html Mamba] | Due to [https://www.datacamp.com/blog/navigating-anaconda-licensing 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 [https://mamba.readthedocs.io/en/latest/index.html Mamba] | ||
Latest revision as of 02:58, 29 June 2026
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
Mamba (Conda) is preinstalled on every StatDS server.
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):
mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm -rf ~/miniconda3/miniconda.sh
~/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:
- To a create new environment with Python installed (note that
baseis 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.
- 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) ...$.
- 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]
- To deactivate your environment:
conda deactivate
- To share virtual environments (useful for reproducibility):
To share your virtual environment (with collaborators or yourself on the server) you can make a
.ymlfile that contains all the information about the packages you are using by first opening up the virtual environment and then runningconda 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