当前位置:   article > 正文

(一)Linux 系统安装Anaconda及环境配置_linux anaconda

linux anaconda

一、下载Anaconda

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

二、安装Anaconda

1.将下载的安装包放在Linux系统路径下,并调到此路径下。

2.Linux命令

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文件。

sudo gedit /etc/profile

b.在文件末尾添加一行:export PATH=/home/username/anaconda3/bin:$PATH,其中,将“/home/username/anaconda3/bin”替换为你实际的安装路径。保存。

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

 c.重启Linux。

d.打开终端,输入conda,回车后显示帮助内容即配置成功

ps:也可以在终端中输入echo $PATH查看已有的环境变量,确认输出里是否已有Anaconda路径了。

(3)暂时性生效

将anaconda配置进环境变量。

export PATH=~/anaconda3/bin:$PATH

更新bashrc,激活环境变量;   

source ~/.bashrc

(4)conda创建环境时出现NoWritableEnvsDirError: No writeable envs directories configured:原因是安装后的anaconda文件没有读写权限,解决方法:

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中,修改虚拟环境的默认地址

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文件

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文件内容将多出一行

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的数据源有哪些

conda config --show channels

#删除数据源

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

#回复默认源

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$
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/90789
推荐阅读
相关标签
  

闽ICP备14008679号