Installing packages with Conda

Last modified by Mikko Halmela on 2024/02/14 09:29

Cubbli user does not usually have administrator privileges that would allow them to install packages (such as libraries and software for development) without manually building them from source and managing a bunch of dependencies, or contacting IT support.
In this manual we offer an alternative:

Conda, a language agnostic virtual environment and package manager. It's repositories have packages from multiple languages, but you might be most interested in Python, R and Java.


Well how does one use it?

Basic usage

1. Make sure that the desired package is available on conda:

https://anaconda.org/anaconda/repo or

conda search package


2. Make a directory for the project (you can and use a different name) and go there:


mkdir projectcd project


3. Create a virtual environment: 

conda create -yn project

Virtual environment is not dependent on the directory, and thus does not need to have a same name.
If for some reason the name of your environment is lost, you can list them with

conda info --envs


4. Activate the virtual environment

conda activate project


5. Install the desired packages


conda search packageconda install package

Some packages have their own instructions. They usually happen at this point and you should follow them.

Note that his might take a long time. It is rather opaque operation (especially "Solving environment"), so you kind of just have to trust it and wait, maybe grab a cup of coffee.


6. Do what you want


7. Close the virtual environment

conda deactivate

Later when you revisit the project, you can just activate the virtual environment, develop and deactivate.

It is a good practice to create own virtual environments for each project, because handling dependencies can be quite a mess. Sometimes though it can be easier to use same environment multiple times, if the dependencies do not change. In that case the environment's name should reflect it's use cases.

Sharing

If you want to share your environment, you can export it with

conda env export --name project > environment.yml

The receiver should create a own directory (perhaps you can also share the whole directory) for the project, put environment.yml there, go there and create conda environment with

conda env create 

Deleting

You can delete your virtual environment and all the packages alongside it with

conda remove --name project --all

Changing channels

By default conda uses official Anaconda repositories, but sometimes they don't have the desired packages. In that case, turning to community managed channels, such as conda-forge or bioconda, might help.

If you know that a package is in a channel, you can install it with

conda install -c channel package


Parting words

If you want to know conda better, good place to start is their documentation:  https://docs.conda.io/projects/conda/en/latest/user-guide/index.html
Cheat sheet of conda's main commands: https://docs.conda.io/projects/conda/en/latest/_downloads/843d9e0198f2a193a3484886fa28163c/conda-cheatsheet.pdf
List of available Anaconda/default/official packages: https://anaconda.org/anaconda/repo 
Note that Anaconda maintains its own repositories, so the latest versions of your software may or may not be available there.

An alternative (better or worse?) for conda when using Python are its own tools venv (virtual environment) and pip (package manager). Using them is mostly the same as with conda, but as it is not language agnostic, their usage is not elaborated here (but here). Other languages, such as Rust, have their own package managers too, and they should be preferred over using Conda, since they most likely have the most recent versions of each package. Learning and using these is left as an exercise for the reader.


If you want to send regards, critiques, corrections, topics for manuals or anything else really, use the comment box below or contact me personally. You can find my contact info on my profile.