This page looks best with JavaScript enabled

Conda Installation and Use

 ·  ☕ 2 min read

1. Installing conda

1
2
3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
rm -rf Miniconda3-latest-Linux-x86_64.sh

However, Miniconda cannot be used for free at large-scale commercial deployments, so you can use Miniforge as a drop-in alternative.

1
2
wget "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash Miniforge3-$(uname)-$(uname -m).sh

2. Changing the default configuration

2.1 Initializing the Shell

Without initialization, activating an environment reports CondaError: Run 'conda init' before 'conda activate'.

1
2
3
4
5
6
7
8
9
conda init --help

usage: conda init [-h] [--all] [--user] [--no-user] [--system] [--reverse] [--json] [-v] [-q] [-d] [SHELLS ...]

Initialize conda for shell interaction.

positional arguments:
  SHELLS         One or more shells to be initialized. If not given, the default value is 'bash' on unix and 'cmd.exe' & 'powershell' on Windows. Use the '--all' flag to initialize all shells. Available shells: ['bash', 'fish', 'powershell',
                 'tcsh', 'xonsh', 'zsh']

Different shell terminals require different initialization commands.

  • zsh
1
conda init zsh
  • bash
1
conda init bash

The purpose of initialization is to make the shell automatically run Conda’s initialization script and configure environment variables when it starts a session.

2.2 Changing where Conda stores packages

  • Edit the .condarc file and add the configuration
1
vim ~/.condarc
1
2
3
4
pkgs_dirs:
  - /Volumes/Data2/Conda/packages
envs_dirs:
  - /Volumes/Data2/Conda/environments

Here the default paths are changed to the /Volumes/Data2/Conda directory to avoid consuming space on the system disk.

  • View the configuration
1
conda config --show

2.3 Using a domestic mirror source

  • Edit .condarc and add domestic mirror sources
1
vim ~/.condarc
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  deepmodeling: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/

3. Environment management

  • Create an environment
1
conda create -n newenv python=3.9
  • List environments
1
2
3
4
5
6
conda env list

# conda environments:
#
newenv                   /Volumes/Data2/Conda/environments/newenv
base                     /usr/local/Caskroom/miniconda/base
  • Activate an environment
1
conda activate newenv
  • Deactivate an environment
1
conda deactivate
  • Delete an environment
1
conda remove -n newenv --all

4. Package management

  • Search for a package
1
conda search pytorch
  • Install a package
1
conda install pytorch==2.2.0
  • Install a package from a specified channel
1
conda install pytorch==2.2.0 torchvision==0.17.0 torchaudio==2.2.0 pytorch-cuda=12.1 -c pytorch -c nvidia

For packages that cannot be found on domestic sources, you can install them by specifying a channel with -c.

  • List packages
1
conda list
  • Update a package
1
conda update pytorch
  • Remove a package
1
conda remove pytorch
  • Install packages from requirements.txt
1
conda install --file requirements.txt

微信公众号
WRITTEN BY
微信公众号