Skip to content

Instantly share code, notes, and snippets.

@JettIsOnTheNet
Last active August 21, 2023 23:56
Show Gist options
  • Save JettIsOnTheNet/992e1713001f5d2a433832a02d167684 to your computer and use it in GitHub Desktop.
Save JettIsOnTheNet/992e1713001f5d2a433832a02d167684 to your computer and use it in GitHub Desktop.

Conda Cheat Sheet

About Conda

Conda is an open-source package management and environment management system that helps you install, manage, and organize packages and their dependencies. Conda allows you to create isolated environments for different projects, each with its own set of packages and dependencies, helping to avoid conflicts and ensure reproducibility.

Getting out of the way some quick nomenclature and differences.

  • Conda:

    • Conda is essentially the package manager. Your new best friend. It can be invoked by calling the command conda.
  • Anaconda:

    • This is a full system, including sci packages, designed for python. It is a large, multi gigabyte install.
  • Miniconda:

    • This is the mini-installer of Conda and probably what you want. It contains conda package manager and will require you to install each package manually. This is usually the preferred way to use conda. If you are in doubt still, install Miniconda.

Installing

Mac

To install Anaconda or preferably Miniconda on a Mac, the most common way is through Brew. Install brew. Then open a terminal and install Miniconda.

brew install --cask miniconda

or

brew install --cask anaconda

Windows

To install on Windows download the package for Miniconda from conda.io here: Miniconda Installer

Basics

  1. Create a new Conda environment: (stick with lowercase env names for filesystem compat)

    conda create --name myEnvName
  2. Activate an environment:

    conda activate myEnvName
  3. Deactivate the current environment:

    conda deactivate
  4. Clone an environment:

    conda create --clone myEnvName --name newEnvName

Package Management

  1. Install a package:

    conda install packageName
  2. Install a specific package version:

    conda install packageName=1.2.3
  3. Update a package:

    conda update packageName
  4. Update all packages in the current environment:

    conda update --all
  5. Remove a package:

    conda remove packageName
  6. List installed packages:

    conda list

Environment Management

  1. Export environment to a YAML file:

    conda env export > environment.yml
  2. Create an environment from a YAML file or install into existing environment from a YAML file:

    conda env create -f environment.yml
    conda env update -n existing_env_name -f environment.yml
  3. Remove an environment:

    conda env remove --name myEnvName
  4. List all environments:

    conda info --envs

Channels and Sources

Channels and sources are remote repositories where packages are stored. The default channel will work properly in most cases, but here are some additional commands for channel / source management.

  1. Add a channel:

    conda config --add channels channelName
  2. Search for packages in a specific channel:

    conda search -c channelName packageName
  3. Specify channel priority:

    conda config --set channel_priority strict

Managing Python

  1. Install a specific Python version:

    conda install python==3.8
  2. Create a virtual environment for a specific Python version:

    conda create --name pyEnvName python==3.8

Conda with other languages

Many people are not aware, but Conda is not just an environment and package manager for python. It also supports languages like R, Lua and C. Here are some basics to get you started using Conda with other languages.

Conda with RLang

  1. Install R language in an environment:

    conda install -c r r-base
  2. Create a Conda environment with R:

    conda create --name r_env r=4.1.1
  3. Install R packages using Conda:

    conda install -c r r-essentials

Conda with Ruby

  1. Install Ruby in an environment:

    conda install -c conda-forge ruby
  2. Create a Conda environment with Ruby:

    conda create --name rubyEnvName ruby=2.7
  3. Install Ruby gems using Conda:

    conda install -c conda-forge rb-gemName

Conda with Lua

  1. Install Lua in an environment:

    conda install -c conda-forge lua
  2. Create a Conda environment with Lua:

    conda create --name luaEnvName lua=5.4
  3. Install LuaRocks packages using Conda:

    conda install -c conda-forge lr-rockName

Conda with C++

  1. Install C++ tools and libraries:

    conda install -c conda-forge gxx cmake
  2. Create a Conda environment for C++ development:

    conda create --name cppEnvName gxx cmake
  3. Use CMake to build C++ projects in Conda environment:

    mkdir build && cd build
    conda activate cppEnvName
    cmake ..
    make

Miscellaneous Helpful Commands

  1. Show Conda information:

    conda info
  2. Display environment details:

    conda info --env
  3. Check for package updates:

    conda search --outdated
  4. Create a virtual environment for Python 3.9:

    conda create --name py3EnvName python=3.9
  5. Enable Jupyter Notebook or Jupyter Lab in an environment:

    conda install -c conda-forge notebook
    conda install -c conda-forge nb_conda_kernels
    conda install -c conda-forge jupyterlab
    conda install -c conda-forge nb_conda_kernels
  6. List available Conda commands:

    conda --help
  7. Update Conda itself:

    conda update conda
  8. Export environment variables to a file:

    conda env export --no-builds > environment.yml
  9. Create an environment with specific packages:

    conda create --name myEnvName packageName1 packageName2
  10. Create a new environment with a specific path: (stick with lowercase names for Conda envs)

    conda create --prefix /path/to/myEnvName
  11. Install packages from a requirements file:

    conda install --file requirements.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment