自学内容网 自学内容网

Anaconda在Ubuntu下的安装与简单使用

一、参考资料

ubuntu16.04下安装&配置anaconda+tensorflow新手教程

二、安装Anaconda

  1. 下载 Miniconda镜像1 or Miniconda镜像2

    # 下载
    wget Miniconda3-py39_4.10.3-Linux-x86_64.sh
    
    # 安装
    bash Miniconda3-py39_4.10.3-Linux-x86_64.sh
    

    一路yes

  2. 安装过程中的选项

    Do you accept the license terms? [yes|no]
    >>> yes
    
    Anaconda3 will now be installed into this location:
    /home/yichao/anaconda3
    >>> 回车
    
    If you'd prefer that conda's base environment not be activated on startup, 
       set the auto_activate_base parameter to false: 
    
    conda config --set auto_activate_base false
    
  3. 取消base为默认虚拟环境

    conda config --set auto_activate_base false
    
  4. 设置环境变量
    如果自动设置环境变量,则需要手动设置环境变量。

    vim ~/.bashrc
    
    # 添加下面两行
    export PATH="/home/username/miniconda3/bin:$PATH"
    export PATH="/home/username/miniconda3/condabin:$PATH"
    
    # 重新加载当前用户配置文件
    source ~/.bashrc
    
  5. Anaconda换源

    Anaconda换源
    anaconda | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror!

    sudo gedit ~/.condarc
    
    # 清空缓存
    conda clean -i
    
    # 安装测试
    conda create -n myenv numpy
    
    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
    
  6. 切换Python版本

    # 打开配置文件
    vim ~/.bashrc
    
    # 添加配置
    alias python="/usr/bin/python"
    alias python3="/usr/local/bin/python3"
    alias pyana="/home/yoyo/anaconda3/bin/python3"
    
    # 更新配置
    source ~/.bashrc
    
  7. 卸载Anaconda

    # 删除anaconda相关文件夹
    rm -rf ~/miniconda3
    rm -rf ~/anaconda3
    rm -rf ~/.conda
    rm -rf ~/.condarc
    rm -rf ~/.anaconda
    
    
    # 删除环境变量
    # 删除关于conda部分的环境变量
    vi ~/.bashrc
    
    # 更新环境变量
    source ~/.bashrc
    

三、Anaconda的常用指令

# 1. 创建虚拟环境
conda create -n your_env_name python=X.X(2.7、3.6等)

# 1.1 在指定的位置创建虚拟环境
conda create -p /PATH/TO/path

# 查看所有的conda虚拟环境
conda env list   

# 2. 激活虚拟环境
source activate your_env_name(虚拟环境名称)

# 3. 退出虚拟环境
source deactivate your_env_name(虚拟环境名称)

# 4. 删除虚拟环境
conda remove -n your_env_name(虚拟环境名称) --all

# 5. 安装包
conda install package_name(包名)
conda install scrapy==1.3 # 安装指定版本的包
conda install -n 环境名 包名 # 在conda指定的某个环境中安装包

# 6. 跳过安装失败的包,继续安装
conda方式
while read requirement; do conda install --yes $requirement; done < requirements.txt

pip方式
while read requirement; do conda install --yes $requirement || pip install $requirement; done < requirements.txt
  1. 查看

    # 1. 查看安装了哪些包
    conda list
    
    # 2. 查看conda配置
    conda config --show
    
    # 3. 查看当前存在哪些虚拟环境
    conda env list 
    或 conda info -e
    或 conda info --envs
    
  2. 更新

    # 1. 检查更新当前conda
    conda update conda
    
    # 2. 更新anaconda
    conda update anaconda
    
    # 3. 更新所有库
    conda update --all
     
    # 4. 更新python
    conda update python
    
  3. 清理conda缓存(conda报错segment fault的时候就是需要清理缓存哦)

    conda clean -p      //删除没有用的包
    conda clean -t      //删除tar包
    conda clean -y --all //删除所有的安装包及cache
    

四、Anaconda的使用技巧

conda环境的复制(生成.yaml文件)和pip环境的复制(生成requirements.txt)

  1. 克隆环境

    # 克隆一个BBB,环境和Tensorflow一样
    conda create -n BBB --clone Tensorflow 
    
  2. 克隆环境(跨计算机)

    # 跨计算机克隆
    # 目标计算机的环境目录 /PATH/TO/home/yoyo/miniconda3/envs/monodepth2-gpu 
    conda create -n BBB --clone /PATH/TO/home/yoyo/miniconda3/envs/monodepth2-gpu 
    
  3. 导入/导出环境

    # 导出环境到yaml文件
    conda env export > env.yaml
    
    # 根据yaml文件复现环境
    conda env create -f env.yaml
    

注意.yaml 文件移植过来的环境只安装了原来环境里用 conda install 等命令直接安装的包,使用pip安装的软件包没有移植过来,需要重新安装。

# 导出安装包
pip freeze > requirements.txt

# 安装
pip install -r requirements.txt 
或者
conda install --yes --file requirements.txt
  1. 打包/解包(跨计算机)

    # 将环境打包
    tar cvf monodepth2-gpu.tar envirement
    
    # monodepth2-gpu.tar文件,通过http、ssh等方式拷贝到目标计算机
    rsync -rzP */envirement.tar /home/yoyo/anaconda3/envs
    
    # 解包
    tar xvf monodepth2-gpu.tar
    
    # 修改conda的环境配置文件 ~/.conda/envirement.txt,在尾部添加拷贝的环境目录
    /home/yoyo/miniconda3/envs/monodepth2-gpu
    
  2. 搜索包

    # 在anaconda官方仓库中搜索包(可能失效)
    anaconda search tensorflow-gpu -t conda  #搜包[tensorflow-gpu]
    
    # 在清华源镜像中搜索包
    conda search tensorflow-gpu
    
    # 模糊查询
    conda search *scikit*
    或者
    conda search '*scikit*'
    
    # 显示包详细信息(可能失效)
    anaconda show aaronszs/tensorflow-gpu  #查询第二条[aaronszs/tensorflow-gpu]
    
    # 启动Anaconda Navigator 图形化界面
    anaconda-navigator
    
    # 导入anaconda所有库
    conda install anaconda
    

五、FAQ

anaconda 环境新建/删除/拷贝 jupyter notebook上使用python虚拟环境 TensorFlow

Anaconda 换国内源、删源最全集锦

在anaconda中安装不存在的python包并安装到指定环境中

【转】conda install和创建虚拟环境下载慢 pip下载慢 有用

TensorFlow+Faster-RCNN+Ubuntu 环境配置&代码运行过程

Q:根据yaml文件复现环境错误

Collecting package metadata (repodata.json): done
Solving environment: failed

ResolvePackageNotFound: 
  - wincertstore==0.2=py36h7fe50ca_0
  - jbig==2.1=h8d14728_2003
  - libdeflate==1.7=h8ffe710_5
  - zstd==1.5.0=h6255e5f_0
  - lz4-c==1.9.3=h8ffe710_1
  - lerc==2.2.1=h0e60522_0
  - libtiff==4.3.0=h0c97f57_1
  - jpeg==9d=h8ffe710_0
  - mkl==2021.3.0=hb70f87d_564
  - setuptools==52.0.0=py36haa95532_0
  - tbb==2021.3.0=h2d74725_0
  - certifi==2021.5.30=py36haa95532_0
  - vc==14.2=h21ff451_1
  - python==3.6.6=hea74fb7_0
  - zlib==1.2.11=h62dcd97_1010
  - numpy==1.19.5=py36h4b40d73_2
  - pip==21.2.2=py36haa95532_0
  - libpng==1.6.37=h1d00b33_2
  - opencv==3.3.1=py36h20b85fd_1
  - vs2015_runtime==14.27.29016=h5e58377_2
  - intel-openmp==2021.3.0=h57928b3_3372
  - xz==5.2.5=h62dcd97_1

Q:根据yaml文件复现环境错误

Collecting package metadata (repodata.json): done
Solving environment: failed

ResolvePackageNotFound: 
  - wincertstore=0.2
  - vc=14.2
  - vs2015_runtime=14.27.29016
# 错误原因
找不到相关的包

# 解决办法
注释对应的行

Q:NotWritableError: The current user does not have write permissions to a required path.

conda创建环境时报错:NotWritableError: The current user does not have write permissions to a required path.

tx2@tx2:~$ conda create -n efficientdet python=3.7
Solving environment: failed

NotWritableError: The current user does not have write permissions to a required path.
  path: /home/tx2/archiconda3/pkgs/urls.txt
  uid: 1000
  gid: 1000

If you feel that permissions on this path are set incorrectly, you can manually
change them by executing

  $ sudo chown 1000:1000 /home/tx2/archiconda3/pkgs/urls.txt

In general, it's not advisable to use 'sudo conda'.‵
# 错误原因
用户没有对anaconda3文件夹的读写权限,造成其原因可能是由于在安装anaconda时使用了管理员权限。

# 解决办法
cd /home/tx2
sudo chown tx2:tx2 -R archiconda3

Q:subprocess.CalledProcessError: Command 'lsb_release -a' returned non-zero exit status 1.

subprocess.CalledProcessError: Command ‘(‘lsb_release’, ‘-a’)’ returned non-zero exit status 1.

(mslite) liulinjun@LAPTOP-4DTD5D42:~/MyDocuments/mindspore$ conda info --envs

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

    Traceback (most recent call last):
      File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/exceptions.py", line 1079, in __call__
        return func(*args, **kwargs)
      File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/cli/main.py", line 84, in _main
        exit_code = do_call(args, p)
      File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/cli/conda_argparse.py", line 83, in do_call
        return getattr(module, func_name)(args, parser)
      File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/cli/main_info.py", line 316, in execute
        info_dict = get_info_dict(args.system)
      File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/cli/main_info.py", line 135, in get_info_dict
        _supplement_index_with_system(virtual_pkg_index)
      File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/core/index.py", line 163, in _supplement_index_with_system
        dist_name, dist_version = context.os_distribution_name_version
      File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/_vendor/auxlib/decorators.py", line 268, in new_fget
        cache[inner_attname] = func(self)
      File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/base/context.py", line 786, in os_distribution_name_version
        from .._vendor.distro import id, version
      File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/_vendor/distro.py", line 1084, in <module>
        _distro = LinuxDistribution()
      File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/_vendor/distro.py", line 599, in __init__
        self._lsb_release_info = self._get_lsb_release_info() \
      File "/home/liulinjun/miniconda3/lib/python3.9/site-packages/conda/_vendor/distro.py", line 943, in _get_lsb_release_info
        raise subprocess.CalledProcessError(code, cmd, stdout, stderr)
    subprocess.CalledProcessError: Command 'lsb_release -a' returned non-zero exit status 1.

`$ /home/liulinjun/miniconda3/bin/conda info --envs`


An unexpected error has occurred. Conda has prepared the above report.

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]: N

No report sent. To permanently opt-out, use

    $ conda config --set report_errors false
File "/root/miniconda3/lib/python3.9/site-packages/conda/_vendor/distro.py", line 943, in _get_lsb_release_info
        raise subprocess.CalledProcessError(code, cmd, stdout, stderr)
    subprocess.CalledProcessError: Command 'lsb_release -a' returned non-zero exit status 1.

`$ /root/miniconda3/bin/conda info --envs`


An unexpected error has occurred. Conda has prepared the above report.

If submitted, this report will be used by core maintainers to improve
future releases of conda.
# 解决方法一
find / -name lsb_release

mv /usr/bin/lsb_release /usr/bin/lsb_release.bak
或者
rm -rf /usr/bin/lsb_release

# 解决方法二
如果方法一无法解决,可尝试方法二。
conda config --set report_errors false

原文地址:https://blog.csdn.net/m0_37605642/article/details/137737120

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!