How to Use Anaconda

0 System Environment

Ubuntu 16.04/18.04
Anaconda 3

1 Installing Anaconda

Anaconda is a powerful virtual environment tool that supports many commonly used scientific computing libraries, making it easier to set up your environment. Its biggest advantage is that, unlike pip, it doesn’t require you to modify your system environment, so you can switch between environments safely. The image below shows some of the common computing libraries Anaconda supports:

First, download the latest available Anaconda installer from the website. I downloaded the Linux installer for Python 3.7, but you can switch to a different Python version later.

https://www.anaconda.com/download

If you’re using Linux, download the .sh file.
Upload the .sh file to the server’s data/download/ directory, then run the installer:

bash /data/download/Anaconda3-2021.11-Linux-x86_64.sh

When asked whether to run init, choose yes:

installation finished.
Do you wish the installer to initialize Anaconda3
by running conda init? [yes|no]
[no] >>> yes

Anaconda installs a set of commonly used packages for you, saving you a lot of trouble. You can accept the defaults for all options. When you see the screen below, the installation is complete:

Anaconda automatically adds the necessary environment variables to ~/.bashrc. To apply the changes in your current terminal window, run:

source ~/.bashrc

If you use zsh, copy the code at the end of .bashrc that looks like the example below and add it to ~/.zshrc:

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/media/liuxiao/9649F32BF3B0A902/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/media/liuxiao/9649F32BF3B0A902/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/media/liuxiao/9649F32BF3B0A902/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/media/liuxiao/9649F32BF3B0A902/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

For instructions on configuring Anaconda mirrors, see:Anaconda Mirrors

2 Creating and Deleting Environments

To create an environment, run:

conda create -n ENV_NAME

To delete an environment, run:

conda env remove -n ENV_NAME

3 Installing Deep Learning Frameworks

3.1 Installing PyTorch

Run the following command to install it:

conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch

3.2 Installing TensorFlow

Run the following command to install it:

conda install tensorflow_env tensorflow-gpu

To check which TensorFlow and CUDA versions are compatible, see:
https://tensorflow.google.cn/install/source?hl=en#linux

4 Common Operations

功能项命令
列出环境列表conda env list
删除某个环境conda env remove --name ENV_NAME
升级 conda 版本conda update conda
列出当前安装软件版本conda list
列出可以安装的指定 package 版本conda search PKG_NAME

Other Operations

Changing the Anaconda Environment Installation Location

Frequently Asked Questions

1. which python Points to a Different Environment

If you run which python in a conda environment and find that it points to a Python executable outside that environment:

This usually happens when nested conda environments get mixed up. To fix it, repeatedly run conda activate until you’ve exited all conda environments, then run conda activate again.
I tested it again, and it worked:

Add a Comment

Your email address will not be published. Required fields are marked *