(一)Linux 系统安装Anaconda及环境设置

打印 上一主题 下一主题

主题 552|帖子 552|积分 1656

一、下载Anaconda

Anaconda网站https://www.anaconda.com/下载所需的版本,例如:Anaconda3-2023.09-0-Linux-x86_64.sh。(也可以利用网址路径直接在linux下下载,这里暂不介绍)
二、安装Anaconda

1.将下载的安装包放在Linux系统路径下,并调到此路径下。
2.Linux命令:
  1. bash Anaconda3-2023.09-0-Linux-x86_64.sh
复制代码
3.一直按回车,或按q+yes跳过阅读
(1)默认安装在用户目录下,回车即可安装;
(2)也可自界说安装目录,直接输入安装目录,回车即可安装;
(3)直到出现“Do you wish the installer to initialize Anaconda3 by running conda init ? ”,输入no,回车。
三、设置环境变量(不然会出现conda不识别情况)

(1)输入 echo $PATH检察是否已添加到环境变量中。假如不存在大概conda不识别,则举行(2)或(3)步调。
  1. (base) fzx@fzx-System-Product-Name:~$ echo $PATH
  2. /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)永久生效
假如在安装Anaconda的过程中没有将安装路径添加到系统环境变量中,需要在安装后手工添加:
a.在终端输入$sudo gedit /etc/profile,打开profile文件。
  1. sudo gedit /etc/profile
复制代码
b.在文件末尾添加一行:export PATH=/home/username/anaconda3/binPATH,其中,将“/home/username/anaconda3/bin”替换为你现实的安装路径。保存。
  1. export PATH=/home/username/anaconda3/bin:$PATH
复制代码
 c.重启Linux。
d.打开终端,输入conda,回车后显示帮助内容即设置成功
ps:也可以在终端中输入echo $PATH检察已有的环境变量,确认输出里是否已有Anaconda路径了。
(3)暂时性生效
将anaconda设置进环境变量。
  1. export PATH=~/anaconda3/bin:$PATH
复制代码
更新bashrc,激活环境变量;   
  1. source ~/.bashrc
复制代码
(4)conda创建环境时出现NoWritableEnvsDirError: No writeable envs directories configured:原因是安装后的anaconda文件没有读写权限,办理方法:
  1. sudo chmod 777 -R xxx/anaconda3/(此处为anaconda文件路径)
复制代码
四、在Linux环境下修改Anaconda的默认假造环境安装位置

1. 一种方法是通过修改用户目录下的.condarc文件,添加或修改envs_dirs选项,指定假造环境的存储路径。这种方法可以按照自己的喜好设置多个假造环境路径,并且按照次序搜刮和创建假造环境。相关命令如下:
(1) 利用命令conda info检察假造环境存放地点和其中的安装包的存放地点。Note:排在第一位的就是默认的地点。
  1. package cache : /home/sxw/anaconda3/pkgs
  2.                 /home/sxw/.conda/pkgs
  3. envs directories : /home/sxw/anaconda3/envs
  4.                    /home/sxw/.conda/envs
复制代码
(2) 在设置文件.condarc中,修改假造环境的默认地点
  1. vim ~/.condarc
复制代码
(3) 添加或修改以下内容:其中/xx/xxx/new_path/new_path/new_path/envs,就是设置的新的默认地点(放在第一位)
  1. package cache : /home/sxw/anaconda3/pkgs
  2.                 /home/sxw/.conda/pkgs
  3. envs directories : /home/sxw/anaconda3/envs
  4.                    /home/sxw/.conda/envs
复制代码
(4)提示: 修改文件时,不要用tab键,不然会报错如下。
  1. Ignoring configuration file (/home/sxw/.condarc) due to error:
  2. Unable to load configuration file.
  3.   path: /home/sxw/.condarc
  4.   reason: invalid yaml at line 1, column 8
复制代码
利用空格键取代tab。
常用vim命令:i:进入编辑模式;esc+shift+;大概esc:返回平凡模式;
以下命令都在平凡模式下输入:
:q:退出文件;:q!:放弃修改退出;:wq:保存修改退出;:e!:放弃修改,重新回到文件打开的状态;u::打消上一步的利用。
2. 另一种方法是通过在创建假造环境时利用–prefix参数,指定假造环境的位置⁴。这种方法可以机动地为每个假造环境单独指定位置,但是需要在激活和删除假造环境时也利用–prefix参数。相关命令如下: 
  1. # 创建一个名为myenv的虚拟环境,并指定其位置为/home/user/myenv
  2. conda create --prefix /home/user/myenv
  3. # 激活该虚拟环境
  4. conda activate /home/user/myenv
  5. # 删除该虚拟环境
  6. conda remove --prefix /home/user/myenv --all
复制代码
五、更换镜像下载源

(此处更换为清华源,也可换为阿里云和中科大)
各系统都可以通过修改用户目录下的.condarc文件来利用 TUNA 镜像源。
Windows 用户无法直接创建名为.condarc的文件,可先实行conda config --set show_channel_urls yes天生该文件之后再修改。
1.vim进入.condarc文件
  1. vim ~/.condarc
复制代码
2. 将下面设置黏贴进去,并查抄前面空格为空格而不是tab。
  1. channels:
  2.   - defaults
  3. show_channel_urls: true
  4. default_channels:
  5.   - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  6.   - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  7.   - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
  8. custom_channels:
  9.   conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  10.   msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  11.   bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  12.   menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  13.   pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  14.   pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  15.   simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
复制代码
假如是tab的话,.condarc文件内容将多出一行
  1. show_channel_urls: true
复制代码
这将导致后续实行conda clean -i报错
  1. # >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<
  2.     Traceback (most recent call last):
  3.       File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/exception_handler.py", line 17, in __call__
  4.         return func(*args, **kwargs)
  5.                ^^^^^^^^^^^^^^^^^^^^^
  6.       File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/cli/main.py", line 47, in main_subshell
  7.         context.__init__(argparse_args=pre_args)
  8.       File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/base/context.py", line 456, in __init__
  9.         self._set_search_path(
  10.       File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/common/configuration.py", line 1428, in _set_search_path
  11.         self._set_raw_data(dict(self._load_search_path(self._search_path)))
  12.                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  13.       File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/common/configuration.py", line 1417, in _load_search_path
  14.         yield path, YamlRawParameter.make_raw_parameters_from_file(path)
  15.                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  16.       File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/common/configuration.py", line 416, in make_raw_parameters_from_file
  17.         return cls.make_raw_parameters(filepath, yaml_obj) or EMPTY_MAP
  18.                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  19.       File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/common/configuration.py", line 389, in make_raw_parameters
  20.         return {
  21.                ^
  22.       File "/home/sxw/anaconda3/lib/python3.11/site-packages/conda/common/configuration.py", line 391, in <dictcomp>
  23.         source, key, from_map[key], cls._get_yaml_key_comment(from_map, key)
  24.                      ~~~~~~~~^^^^^
  25.     TypeError: string indices must be integers, not 'str'
  26. `$ /home/sxw/anaconda3/bin/conda config --remove-key channels`
  27.   environment variables:
  28.                  CIO_TEST=<not set>
  29.                CONDA_ROOT=/home/sxw/anaconda3
  30.            CURL_CA_BUNDLE=<not set>
  31.                LD_PRELOAD=<not set>
  32.                      PATH=/home/sxw/anaconda3/bin/:~/MyApp/anaconda3/bin:~/MyApp/anaconda3/bin:~
  33.                           /MyApp/anaconda3/bin:~/MyApp/anaconda3/bin:~/MyApp/anaconda3/bin:~/MyA
  34.                           pp/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sb
  35.                           in:/bin:/usr/games:/usr/local/games:/snap/bin:/home/anaconda3/bin
  36.        REQUESTS_CA_BUNDLE=<not set>
  37.             SSL_CERT_FILE=<not set>
  38.      active environment : None
  39.        user config file : /home/sxw/.condarc
  40. populated config files :
  41.           conda version : 23.7.4
  42.     conda-build version : 3.26.1
  43.          python version : 3.11.5.final.0
  44.        virtual packages : __archspec=1=x86_64
  45.                           __glibc=2.35=0
  46.                           __linux=5.15.0=0
  47.                           __unix=0=0
  48.        base environment : /home/sxw/anaconda3  (writable)
  49.       conda av data dir : /home/sxw/anaconda3/etc/conda
  50.   conda av metadata url : None
  51.            channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
  52.                           https://repo.anaconda.com/pkgs/main/noarch
  53.                           https://repo.anaconda.com/pkgs/r/linux-64
  54.                           https://repo.anaconda.com/pkgs/r/noarch
  55.           package cache : /home/sxw/anaconda3/pkgs
  56.                           /home/sxw/.conda/pkgs
  57.        envs directories : /home/sxw/anaconda3/envs
  58.                           /home/sxw/.conda/envs
  59.                platform : linux-64
  60.              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
  61.                 UID:GID : 1000:1000
  62.              netrc file : None
  63.            offline mode : False
  64. An unexpected error has occurred. Conda has prepared the above report.
  65. If you suspect this error is being caused by a malfunctioning plugin,
  66. consider using the --no-plugins option to turn off plugins.
  67. Example: conda --no-plugins install <package>
  68. Alternatively, you can set the CONDA_NO_PLUGINS environment variable on
  69. the command line to run the command without plugins enabled.
  70. Example: CONDA_NO_PLUGINS=true conda install <package>
  71. If submitted, this report will be used by core maintainers to improve
  72. future releases of conda.
  73. Would you like conda to send this report to the core maintainers? [y/N]:
  74. Timeout reached. No report sent.
复制代码
PlanB :也可以利用命令直接增加源 (直接粘贴可能同样报上述错误,请粘贴后查抄是否有误)
  1. #添加数据源:例如, 添加清华anaconda镜像:
  2. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  3. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  4. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  5. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  6. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  7. 其他镜像源,推荐使用中科大源
  8. # 中科大镜像源
  9. conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
  10. conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
  11. conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
  12. conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
  13. conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
  14. conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
  15. conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/
  16. # 阿里镜像源
  17. conda config --add channels https://mirrors.aliyun.com/anaconda/
  18. # 豆瓣的python的源
  19. conda config --add channels http://pypi.douban.com/simple/
复制代码
2.# 拓展:关于conda的数据源,另外有下述利用可做选择
#显示现在conda的数据源有哪些
  1. conda config --show channels
复制代码
#删除数据源
  1. conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
复制代码
#回复默认源
  1. conda config --remove-key channels
复制代码
六、创建新环境

一般在~/Anaconda3/envs 路径下新建环境,如许方便管理与区分,也可以跟自己自我喜好自建目录。
1. 先输入conda,进入conda环境
  1. sxw@sxw-server:~/anaconda3$ conda
  2. usage: conda [-h] [--no-plugins] [-V] COMMAND ...
  3. conda is a tool for managing and deploying applications, environments and packages.
  4. options:
  5.   -h, --help          Show this help message and exit.
  6.   --no-plugins        Disable all plugins that are not built into conda.
  7.   -V, --version       Show the conda version number and exit.
  8. commands:
  9.   The following built-in and plugins subcommands are available.
  10.   COMMAND
  11.     build             Build conda packages from a conda recipe.
  12.     clean             Remove unused packages and caches.
  13.     compare           Compare packages between conda environments.
  14.     config            Modify configuration values in .condarc.
  15.     content-trust     Signing and verification tools for Conda
  16.     convert           Convert pure Python packages to other platforms
  17.                       (a.k.a., subdirs).
  18.     create            Create a new conda environment from a list of
  19.                       specified packages.
  20.     debug             Debug the build or test phases of conda recipes.
  21.     develop           Install a Python package in 'development mode'.
  22.                       Similar to `pip install --editable`.
  23.     doctor            Display a health report for your environment.
  24.     env               See `conda env --help`.
  25.     index             Update package index metadata files. Pending
  26.                       deprecation, use https://github.com/conda/conda-index
  27.                       instead.
  28.     info              Display information about current conda install.
  29.     init              Initialize conda for shell interaction.
  30.     inspect           Tools for inspecting conda packages.
  31.     install           Install a list of packages into a specified conda
  32.                       environment.
  33.     list              List installed packages in a conda environment.
  34.     metapackage       Specialty tool for generating conda metapackage.
  35.     notices           Retrieve latest channel notifications.
  36.     pack              See `conda pack --help`.
  37.     package           Create low-level conda packages. (EXPERIMENTAL)
  38.     remove (uninstall)
  39.                       Remove a list of packages from a specified conda
  40.                       environment.
  41.     rename            Rename an existing environment.
  42.     render            Expand a conda recipe into a platform-specific recipe.
  43.     repo              See `conda repo --help`.
  44.     run               Run an executable in a conda environment.
  45.     search            Search for packages and display associated information
  46.                       using the MatchSpec format.
  47.     server            See `conda server --help`.
  48.     skeleton          Generate boilerplate conda recipes.
  49.     token             See `conda token --help`.
  50.     update (upgrade)  Update conda packages to the latest compatible
  51.                       version.
  52.     verify            See `conda verify --help`.
复制代码
2. 假如提示 not command,则因为第一次没有把conda写到环境变量中。输入下面命令
  1. export PATH=~/anaconda3/bin:$PATH
  2. source ~/.bashrc
复制代码
3. 利用指令conda create --name 环境名称 python==3.9创建自己想要创建的环境(环境名称自己定名)。
4. 输入y

5.输入conda env list 显示假造环境列表,可以看到自己刚才创建好的环境以及路径。
  1. sxw@sxw-server:~/anaconda3$ conda env list
  2. # conda environments:
  3. #
  4. base                     /home/sxw/anaconda3
  5. ComPython39              /home/sxw/anaconda3/envs/ComPython39
复制代码
6.source activate pytorch 【切换你的环境名,好比source activate base】,接下来可在当前环境下安装包。
  1. sxw@sxw-server:~/anaconda3$ source activate ComPython39
  2. (ComPython39) sxw@sxw-server:~/anaconda3$
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

尚未崩坏

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表