Fundamental Operations within a Conda Environment

Updated on October 16, 2023
Fundamental Operations within a Conda Environment header image

Introduction

Conda is an open-source utility tool used for machine learning and scientific computing tasks with the following key functionalities:

  • Environment management: Independent environments allow you to install different versions of the same software package without any dependency conflicts with other system packages
  • Package management: Conda handles complex dependency chains to install necessary packages for use in an active environment

This guide explains the fundamental operations in a Conda environment. You will apply Conda commands to manage environments, install packages, and handle different package versions on a server.

Prerequisites

Before you begin, you need to:

  • Deploy a Miniconda or Anaconda server using the Vultr Marketplace application

    To manually install Conda on your existing server, either install Minicondaor Anaconda

  • Using SSH, access the server

  • Create a new non-root sudo user

  • Switch to the non-root sudo user account

      # su pythonuser

    > This guide uses pythonuser, replace the user account with your actual sudo user

Conda Command Usage

  1. View the available Conda Version on your server

     $ conda --version 

    Output:

     conda 23.7.2
  2. View information about the Conda installation on your server

     $ conda info

    Your output should look like the one below:

     active environment : env1 
     active env location : /home/pythonuser/miniconda3/envs/env1
     shell level : 1
     user config file : /home/pythonuser/.condarc
     conda version : 23.3.1
     python version : 3.10.10.final.0
  3. Update Conda

     $ conda update conda -y
  4. To view information on the usage of a specific Conda command, use the following syntax

     $ conda COMMAND --help

    For example, view the env Conda command usage

     $ conda env --help

    Output:

     usage: conda-env [-h] command ...                                                                                                  
    
     positional arguments:                                                                                                              
       command     

    View information about the env create sub-command usage

     $ conda env create --help

    Output:

     usage: conda-env create [-h] [-f FILE] [-n ENVIRONMENT | -p PATH] [-C] [-k] [--offline] [--force] [--no-default-packages]
                             [--json] [-v] [-q] [-d] [-y] [--solver {classic,libmamba} | --experimental-solver {classic,libmamba}]
                             [remote_definition]
    
     Create an environment based on an environment definition file.

Manage Conda Environments

  1. Create a new Conda environment. For example, env1

     $ conda create --name env1 -y
  2. Activate the env1 Conda environment

     $ conda activate env1
  3. When successful, verify that your terminal prompt changes to the Conda environment

     (env1) pythonuser@SERVER:~$ 
  4. Deactivate the active Conda environment:

     $ conda deactivate

    When deactivated, your server terminal prompt changes back to its normal version. When using nested Conda environments, deactivate each environment until the prompt changes back to the non-coda session. For example:

     (env2) pythonuser@SERVER:~$ conda deactivate
     (env5) pythonuser@SERVER:~$ conda deactivate

Manage Multiple Conda Environments

  1. Create a new environment env2 with a specific Python version

     $ conda create --name env2 python=3.9 -y
  2. View the list of available Conda environments

     $ conda env list

    Output:

     # conda environments:
     #
     base                     /home/pythonuser/miniconda3/
     env1                     /home/pythonuser/miniconda3/envs/env1
     env2                     /home/pythonuser/miniconda3//envs/env2
  3. To delete a Conda environment, use the syntax below

     $ conda remove --name ENVIRONMENT --all

    For example, delete env1

     $ conda remove --name env1 --all
  4. To rename an environment, use the following syntax

     $ conda rename --name OLD-ENV-NAME NEW-ENV-NAME

    For example, rename env2 to env3

     $ conda rename --name env2 env3
  5. Create a new environment env4 as a clone of an existing environment env3

     $ conda create --clone env3 --name env4
  6. To create a new environment with pre-installed software packages, follow the syntax below

     $ conda create --name ENV-Name PKG_NAME -c CHANNEL_NAME

    For example, create env5 with the pytorch and CPU-only packages from the PyTorch channel

     $ conda create --name env5 pytorch cpuonly -c pytorch -y

Manage Conda Packages

  1. View the list of installed packages in all Conda environments

     $ conda list

    Output:

     # Name                    Version                   Build  Channel
     _anaconda_depends         2023.07                 py311_1  
     _libgcc_mutex             0.1                        main  
     _openmp_mutex             5.1                       1_gnu  
     abseil-cpp                20211102.0           hd4dd3e8_0  
     aiobotocore               2.5.0           py311h06a4308_0  
     aiofiles                  22.1.0          py311h06a4308_0  
     aiohttp                   3.8.5           py311h5eee18b_0  

    When you run the above command in a Conda environment, all installed packages display in your output. For example, activate the env5 Conda environment

     $ conda activate env3 

    Run the list command again

     $ conda list

    Your output should look like the one below:

     # Name                    Version                   Build  Channel
     _libgcc_mutex             0.1                        main  
     _openmp_mutex             5.1                       1_gnu  
     ca-certificates           2023.08.22           h06a4308_0  
     cpuonly                   2.0                           0    pytorch
     python                    3.11.5               h955ad1f_0  
     pytorch                   2.0.1              py3.11_cpu_0    pytorch
     pytorch-mutex             1.0                         cpu    pytorch

    View the list of installed packages in a different environment. For example env3

     $ conda list --name env3
  2. When searching packages, Conda uses the default channel, to specify a target channel with Conda packages, follow the syntax below

     $ conda search PKG_NAME -c CHANNEL_NAME

    For example, search the pytorch package in the pytorch channel

     $ conda search pytorch -c pytorch
  3. To install a specific package in your Conda environment, follow the syntax below

     $ conda install PACKAGE -c CHANNEL_NAME

    OR

     $ conda install channel_name::pkg_name

    For example, install the PyTorch package from the pytorch channel

     $ conda install pytorch::pytorch -y

    To install a specific package version, specify it next to the package. For example, install the PyTorch version 2.0.0

     $ conda install pytorch=2.0.0 -c pytorch -y

    If a newer version of the same package is available in the same environment, it's downgraded to the specified version. Also, if an older version of the package is available in the environment, it's upgraded to the set version

  4. Update all installed packages in a Conda environment

     $ conda update --all -y

    To update all installed packages in a different environment such as env6. Run the following command

     $ conda update --all --name env6 -y

    To update a specific package, specify it after the update declaration. For example, update the PyTorch package in your environment

     $ conda update pytorch -y
  5. To uninstall a package, use the syntax below

     $ conda remove ENVIRONMENT PACKAGE -y

    For example, uninstall pytorch from the env4 environment

     $ conda remove --name env4 pytorch -y

    When a target environment is not specified, the above command uninstalls the package from the active environment

Import and Export Environments

  1. To export a Conda environment to a single file, use the following listing syntax

     $ conda list --explicit --name ENVIRONMENT > EXPORT-FILENAME

    For example, to export env6 to a requirements_env6.txt file, run the following command

     $ conda list --explicit --name env6 > requirements_env6.txt

    To export the file to another format such as .yaml, specify the file extension

     $ conda list --explicit --name env6 > requirements_env6.yaml
  2. To import a file as a new Conda environment, specify the source file. For example, create env7 using the env6 export file

     $ conda  create --name env7 --file requirements_env6.txt

    When created, view the list of available Conda environments

     $ conda env list

    Output:

     env6                  *  /root/anaconda/envs/env6
     env7                     /root/anaconda/envs/env7

Use Pip with Conda

To extend the Conda package sources, install packages using the Python Pip package manager. Installed packages are only visible in the respective Conda environments, install the package manager as described below.

  1. Activate your target Conda environment. For example, env3

     $ conda activate env3
  2. Using Pip, install the scipy package

     $ pip install scipy

    When installed, Scipy is only available in the active env3 environment.

Conclusion

You have explored fundamental operations you can perform within a Conda environment. Depending on your development environment, you can extensively manage environments and packages using Conda. For more information, visit the following Conda documentation resources: