Server Setup, Part II: Python Environments with Conda
This series records how I set up a remote GPU server for deep learning work. Part I covers SSH and Git; this part covers Python environments.
Conda manages isolated environments, each with its own Python version and packages. On a shared server this keeps projects from interfering with each other and avoids depending on the system Python.
Choosing a distribution
There are three common installers:
- Anaconda includes several hundred preinstalled packages and needs several gigabytes.
- Miniconda includes only conda and Python. Packages are installed as needed.
- Miniforge is similar to Miniconda but uses the community
conda-forgechannel by default.
On a server, Miniconda or Miniforge is usually the better choice. The packages in Anaconda’s default channels are covered by Anaconda’s terms of service, which require a paid license for some organizations; if this applies to your institution, Miniforge avoids the issue. The commands below are the same for all three.
Installation
Download the installer for Linux and run it. For Anaconda (check the archive for the current version):
curl -O https://repo.anaconda.com/archive/Anaconda3-2024.10-1-Linux-x86_64.sh
bash Anaconda3-2024.10-1-Linux-x86_64.sh
The installer asks you to accept the license and choose an install location; the default under your home directory is fine. When it asks whether to run conda init, answer yes. This adds a block to ~/.bashrc so that conda is available in new shells.
Open a new shell afterwards. The prompt should begin with (base), which means the default environment is active. If you prefer not to activate it automatically, run conda config --set auto_activate_base false.
Environments
Create one environment per project rather than installing into base.
| Task | Command |
|---|---|
| Create an environment | conda create -n my_project python=3.11 |
| Activate it | conda activate my_project |
| Deactivate | conda deactivate |
| List environments | conda env list |
| Remove an environment | conda remove -n my_project --all |
| Export to a file | conda env export > environment.yml |
| Recreate from a file | conda env create -f environment.yml |
Packages
Packages are installed into the active environment.
| Task | Command |
|---|---|
| Install packages | conda install numpy pandas matplotlib |
| List installed packages | conda list |
| Update a package | conda update pandas |
| Update everything | conda update --all |
| Remove a package | conda remove matplotlib |
pip also works inside a conda environment and installs into it. When mixing the two, install conda packages first and pip packages afterwards, since conda does not track what pip installed. PyTorch is a common case: recent releases are distributed through pip rather than conda, so use the command generated on pytorch.org for your CUDA version.
Mirrors
Downloads from the default servers can be slow from some regions, including mainland China. pip and conda are configured separately.
For pip, set a mirror index, for example the Tsinghua (TUNA) mirror:
pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
For conda, edit ~/.condarc. The configuration recommended by TUNA is:
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
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
Run conda clean -i afterwards to clear the cached index. The TUNA help page lists the current configuration.