Skip to content

Instantly share code, notes, and snippets.

@shane5ul
Created September 28, 2023 15:33
Show Gist options
  • Save shane5ul/39c63b89a19a8a5f574f6745f64fdc5b to your computer and use it in GitHub Desktop.
Save shane5ul/39c63b89a19a8a5f574f6745f64fdc5b to your computer and use it in GitHub Desktop.
My Conda Cheatsheet

Conda Package Manager

conda is a package manager like pip. However, there are important differences: (i) it is not restricted to python (via PyPI), (ii) has better dependency resolution, and (iii) has excellent environment management capabilities.

This last difference allows us to create isolated environments that can contain different versions of Python and packages, making it easier to manage dependencies and avoid conflicts between packages.

Installing conda

There are two broad ways to get conda. The first involves installing Anaconda which comes with a large number of other packages (7500+) relevant for ML and scientific computing. If you want just the basics then you should get miniconda.

Detailed instructions are here. For example, here are specific instructions to install on Linux.

Terminal Prompt

After installation, the terminal prompt shows the conda environment (base) by default. Suppose you find this appearance annoying, or you want use the system-wide base python install.

If you want to default to the system-wide base python install on startup, run the following command in your terminal once. conda config --set auto_activate_base false

When you activate a conda environment, you will see that before the prompt

$ conda activate myenv
(myenv) $ 

If you want to use conda, but never see the environment in parenthesis (base or otherwise), then run the following command once. conda config --set changeps1 False

Jupyter Notebooks

By default jupyter notebook will use the system-wide python installation. To use a particular conda environment in jupyter notebooks there are several strategies.

The most useful (to me) seems to be option 2. It avoids a separate jupyter installation in each conda environment (option 3). You install nb_conda_kernels in the base conda distribution, and ipykernel in each conda environment.

conda activate my-conda-env    # this is the environment for your project and code
conda install ipykernel
conda deactivate

conda activate base            # could be also some other environment
conda install nb_conda_kernels
jupyter notebook

Useful conda commands

Environments

  • Create a new environment conda create --name myenv python=3.8

  • Activate an environment conda activate myenv

  • Deactivate the current environment conda deactivate

  • List all environments conda env list

  • Remove an environment conda env remove --name myenv

  • Export an environment to a YAML file conda env export --name myenv > environment.yml

Create an environment from a YAML file conda env create --file environment.yml

Packages

  • Install a package in the active environment conda install package-name

  • Install a specific version of a package conda install package-name=1.2.3

  • Update a package conda update package-name

  • Remove a package conda remove package-name

  • List installed packages in the active environment conda list

  • Search for packages conda search package-name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment