(1) Linux system installation Anaconda and environment configuration

1. Download Anaconda

Download the required version from the Anaconda website https://www.anaconda.com/, for example: Anaconda3-2023.09-0-Linux-x86_64.sh. (You can also use the URL path to download directly under Linux, which will not be introduced here.)

2. Install Anaconda

1. Place the downloaded installation package in the Linux system path and adjust it to this path.

2.Linux command:

bash Anaconda3-2023.09-0-Linux-x86_64.sh

3. Keep pressing Enter, or press q + yes to skip reading

(1) It is installed in the user directory by default and can be installed by pressing Enter;

(2) You can also customize the installation directory, enter the installation directory directly, and press Enter to install;

(3) Until “Do you wish the installer to initialize Anaconda3 by running conda init?” appears, enter no and press Enter.

3. Configure environment variables (otherwise conda will not be recognized)

(1) Enter echo $PATH to check whether it has been added to the environment variable. If it does not exist or is not recognized by conda, proceed to step (2) or (3).

(base) fzx@fzx-System-Product-Name:~$ echo $PATH
/home/fzx/anaconda3/bin:/home/fzx/anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr /games:/usr/local/games:/snap/bin

(2) Effective permanently

If the installation path is not added to the system environment variables during the installation of Anaconda, you need to add it manually after installation:

a. Enter $sudo gedit /etc/profile in the terminal to open the profile file.

sudo gedit /etc/profile

b. Add a line at the end of the file: export PATH=/home/username/anaconda3/bin:$PATH, where replace “/home/username/anaconda3/bin” with your actual installation path. save.

export PATH=/home/username/anaconda3/bin:$PATH

c. Restart Linux.

d. Open the terminal, enter conda, press Enter and the help content will be displayed, indicating that the configuration is successful.

ps: You can also enter echo $PATH in the terminal to check the existing environment variables and confirm whether there is an Anaconda path in the output.

(3) Temporarily effective

Configure anaconda into environment variables.

export PATH=~/anaconda3/bin:$PATH

Update bashrc and activate environment variables;

source ~/.bashrc

(4) NoWritableEnvsDirError: No writeable envs directories configured occurs when conda creates the environment: The reason is that the anaconda file after installation does not have read and write permissions. The solution:

sudo chmod 777 -R xxx/anaconda3/(this is the anaconda file path)

4. Modify Anaconda’s default virtual environment installation location in Linux environment

1. One method is to modify the .condarc file in the user directory, add or modify the envs_dirs option, and specify the storage path of the virtual environment. This method allows you to set multiple virtual environment paths according to your own preferences, and search and create virtual environments in sequence. The relevant commands are as follows:

(1) Use the conda info command to view the storage address of the virtual environment and the storage address of the installation package in it. Note: The first one listed is the default address.

package cache: /home/sxw/anaconda3/pkgs
                /home/sxw/.conda/pkgs
envs directories: /home/sxw/anaconda3/envs
                   /home/sxw/.conda/envs

(2) In the configuration file .condarc, modify the default address of the virtual environment

vim ~/.condarc

(3) Add or modify the following content: /xx/xxx/new_path/new_path/new_path/envs, is the new default address set (put first)

package cache: /home/sxw/anaconda3/pkgs
                /home/sxw/.conda/pkgs
envs directories: /home/sxw/anaconda3/envs
                   /home/sxw/.conda/envs

(4) Tip: When modifying files, do not use the tab key, otherwise an error will be reported as follows.

Ignoring configuration file (/home/sxw/.condarc) due to error:
Unable to load configuration file.
  path: /home/sxw/.condarc
  reason: invalid yaml at line 1, column 8

Use the space bar instead of tab.
Common vim commands: i: enter editing mode; esc + shift +; or esc: return to normal mode;
The following commands are all entered in normal mode:
:q: Exit the file; :q!: Abandon modifications and exit; :wq: Save modifications and exit; :e!: Abandon modifications and return to the file open state; u:: Undo the previous operation.

2. Another method is to specify the location of the virtual environment by using the –prefix parameter when creating the virtual environment. This method provides the flexibility to specify the location individually for each virtual environment, but requires that the –prefix parameter is also used when activating and deleting virtual environments. The relevant commands are as follows:

# Create a virtual environment named myenv and specify its location as /home/user/myenv
conda create --prefix /home/user/myenv
# Activate the virtual environment
conda activate /home/user/myenv
# Delete the virtual environment
conda remove --prefix /home/user/myenv --all

5. Change the image download source

(Replace it with Tsinghuayuan here, or you can replace it with Alibaba Cloud and the University of Science and Technology of China)

Each system can use the TUNA image source by modifying the .condarc file in the user directory.

Windows users cannot directly create a file named .condarc. They can first execute conda config --set show_channel_urls yes to generate the file and then modify it.

1.vim enter the .condarc file

vim ~/.condarc

2. Paste the following configuration and check that the preceding spaces are spaces and not tabs.

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

If it is tab, the content of .condarc file will have one more line.

show_channel_urls: true

This will cause an error to be reported in subsequent executions of conda clean -i

# >>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<

    Traceback (most recent call last):
      File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/exception_handler.py", line 17, in __call__
        return func(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^
      File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/cli/main.py", line 47, in main_subshell
        context.__init__(argparse_args=pre_args)
      File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/base/context.py", line 456, in __init__
        self._set_search_path(
      File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/common/configuration.py", line 1428, in _set_search_path
        self._set_raw_data(dict(self._load_search_path(self._search_path)))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/common/configuration.py", line 1417, in _load_search_path
        yield path, YamlRawParameter.make_raw_parameters_from_file(path)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^
      File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/common/configuration.py", line 416, in make_raw_parameters_from_file
        return cls.make_raw_parameters(filepath, yaml_obj) or EMPTY_MAP
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/common/configuration.py", line 389, in make_raw_parameters
        return {
               ^
      File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/common/configuration.py", line 391, in <dictcomp>
        source, key, from_map[key], cls._get_yaml_key_comment(from_map, key)
                     ~~~~~~~~^^^^^
    TypeError: string indices must be integers, not 'str'

`$ /home/sxw/anaconda3/bin/conda config --remove-key channels`

  environment variables:
                 CIO_TEST=<not set>
               CONDA_ROOT=/home/sxw/anaconda3
           CURL_CA_BUNDLE=<not set>
               LD_PRELOAD=<not set>
                     PATH=/home/sxw/anaconda3/bin/:~/MyApp/anaconda3/bin:~/MyApp/anaconda3/bin:~
                          /MyApp/anaconda3/bin:~/MyApp/anaconda3/bin:~/MyApp/anaconda3/bin:~/MyA
                          pp/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sb
                          in:/bin:/usr/games:/usr/local/games:/snap/bin:/home/anaconda3/bin
       REQUESTS_CA_BUNDLE=<not set>
            SSL_CERT_FILE=<not set>

     active environment: None
       user config file: /home/sxw/.condarc
 populated config files:
          conda version: 23.7.4
    conda-build version: 3.26.1
         python version: 3.11.5.final.0
       virtual packages: __archspec=1=x86_64
                          __glibc=2.35=0
                          __linux=5.15.0=0
                          __unix=0=0
       base environment: /home/sxw/anaconda3 (writable)
      conda av data dir: /home/sxw/anaconda3/etc/conda
  conda av metadata url : None
           channel URLs: https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache: /home/sxw/anaconda3/pkgs
                          /home/sxw/.conda/pkgs
       envs directories: /home/sxw/anaconda3/envs
                          /home/sxw/.conda/envs
               platform: linux-64
             user-agent: conda/23.7.4 requests/2.31.0 CPython/3.11.5 Linux/5.15.0-86-generic ubuntu/22.04.3 glibc/2.35
                UID:GID: 1000:1000
             netrc file: None
           offline mode: False


An unexpected error has occurred. Conda has prepared the above report.
If you suspect this error is being caused by a malfunctioning plugin,
consider using the --no-plugins option to turn off plugins.

Example: conda --no-plugins install <package>

Alternatively, you can set the CONDA_NO_PLUGINS environment variable on
the command line to run the command without plugins enabled.

Example: CONDA_NO_PLUGINS=true conda install <package>

If submitted, this report will be used by core maintainers to improve
future releases of conda.
Would you like conda to send this report to the core maintainers? [y/N]:
Timeout reached. No report sent.

PlanB: You can also use commands to directly add sources (direct pasting may also report the above error, please check whether there are errors after pasting)

#Add data source: For example, add Tsinghua anaconda mirror:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/


For other mirror sources, it is recommended to use the source of University of Science and Technology of China.

# University of Science and Technology of China Mirror Source
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/
?
#Alibaba mirror source
conda config --add channels https://mirrors.aliyun.com/anaconda/

# Douban’s python source
conda config --add channels http://pypi.douban.com/simple/

2.# Expansion: Regarding the data source of conda, there are also the following operations to choose from.

#Show the current data sources of conda

conda config --show channels

#Delete data source

conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

#Reply to default source

conda config --remove-key channels

6. Create a new environment

Generally, a new environment is created under the ~/Anaconda3/envs path, which facilitates management and differentiation. You can also create your own directory according to your own preferences.

1. Enter conda first to enter the conda environment

sxw@sxw-server:~/anaconda3$ conda
usage: conda [-h] [--no-plugins] [-V] COMMAND ...

conda is a tool for managing and deploying applications, environments and packages.

options:
  -h, --help Show this help message and exit.
  --no-plugins Disable all plugins that are not built into conda.
  -V, --version Show the conda version number and exit.

commands:
  The following built-in and plugins subcommands are available.

  COMMAND
    build Build conda packages from a conda recipe.
    clean Remove unused packages and caches.
    compare Compare packages between conda environments.
    config Modify configuration values in .condarc.
    content-trust Signing and verification tools for Conda
    convert Convert pure Python packages to other platforms
                      (a.k.a., subdirs).
    create Create a new conda environment from a list of
                      specified packages.
    debug Debug the build or test phases of conda recipes.
    develop Install a Python package in 'development mode'.
                      Similar to `pip install --editable`.
    doctor Display a health report for your environment.
    env See `conda env --help`.
    index Update package index metadata files. Pending
                      deprecation, use https://github.com/conda/conda-index
                      instead.
    info Display information about current conda install.
    init Initialize conda for shell interaction.
    inspect Tools for inspecting conda packages.
    install Install a list of packages into a specified conda
                      environment.
    list List installed packages in a conda environment.
    metapackage Specialty tool for generating conda metapackage.
    notices Retrieve latest channel notifications.
    pack See `conda pack --help`.
    package Create low-level conda packages. (EXPERIMENTAL)
    remove(uninstall)
                      Remove a list of packages from a specified conda
                      environment.
    rename Rename an existing environment.
    render Expand a conda recipe into a platform-specific recipe.
    repo See `conda repo --help`.
    run Run an executable in a conda environment.
    search Search for packages and display associated information
                      using the MatchSpec format.
    server See `conda server --help`.
    skeleton Generate boilerplate conda recipes.
    token See `conda token --help`.
    update (upgrade) Update conda packages to the latest compatible
                      version.
    verify See `conda verify --help`.

2. If it prompts not command, it is because conda was not written to the environment variable for the first time. Enter the following command

export PATH=~/anaconda3/bin:$PATH
source ~/.bashrc

3. Use the command conda create –name environment name python==3.9 to create the environment you want to create (name the environment name yourself).

4. Enter y

5. Enter conda env list to display the virtual environment list. You can see the environment and path you just created.

sxw@sxw-server:~/anaconda3$ conda env list
# conda environments:
#
base /home/sxw/anaconda3
ComPython39 /home/sxw/anaconda3/envs/ComPython39

6.source activate pytorch [Switch your environment name, such as source activate base], and then install the package in the current environment.

sxw@sxw-server:~/anaconda3$ source activate ComPython39
(ComPython39) sxw@sxw-server:~/anaconda3$

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treePreparatory knowledgeInstalling Python382146 people are learning the system