Miniconda 安装和使用笔记

Miniconda是Anaconda的简化版, 可以管理多个Python版本的环境. 实际使用的话, 占用的空间不会很小, 我跑一些正常的应用后, 安装目录占用空间4.3GB, 安装建议要预留10到20G的空间.

安装 Miniconda

下载安装包

在里面下载适合自己操作系统的安装包, 对于X64的Ubuntu, 下载的是 Miniconda3-latest-Linux-x86_64.sh

chmod后执行

chmod +x Miniconda3-latest-Linux-x86_64.sh
./Miniconda3-latest-Linux-x86_64.sh

或者直接用bash执行

bash Miniconda3-latest-Linux-x86_64.sh

中间会有一些提示, 按提示安装. 最后一步要启动自动设置环境, 选no

启用conda环境

启动 conda 环境时需要用下面的命令

eval "$(/install-path/bin/conda shell.YOUR_SHELL_NAME hook)"

我的环境时bash, 命令就是

eval "$(/home/milton/miniconda3/bin/conda shell.bash hook)"

启用后console的提示符前面会增加(base), 可以用conda --verion验证一下是否生效

(base) milton@somewhere:~$ conda --version
conda 23.11.0

用info查看环境信息

(base) milton@somewhere:~$ conda info

     active environment : base
    active env location : /home/milton/miniconda3
            shell level : 1
       user config file : /home/milton/.condarc
 populated config files : 
          conda version : 23.11.0
    conda-build version : not installed
         python version : 3.11.5.final.0
                 solver : libmamba (default)
       virtual packages : __archspec=1=haswell
                          __conda=23.11.0=0
                          __glibc=2.35=0
                          __linux=6.5.0=0
                          __unix=0=0
       base environment : /home/milton/miniconda3  (writable)
      conda av data dir : /home/milton/miniconda3/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/milton/miniconda3/pkgs
                          /home/milton/.conda/pkgs
       envs directories : /home/milton/miniconda3/envs
                          /home/milton/.conda/envs
               platform : linux-64
             user-agent : conda/23.11.0 requests/2.31.0 CPython/3.11.5 Linux/6.5.0-15-generic ubuntu/22.04.3 glibc/2.35 solver/libmamba conda-libmamba-solver/23.12.0 libmambapy/1.5.3
                UID:GID : 1000:1000
             netrc file : None
           offline mode : False

关于这个eval和hook的说明

https://askubuntu.com/questions/1254371/can-anyone-explain-what-this-command-actually-do-eval-users-jsmith-anaconda

eval "$(shell-command)" will run shell-command and feed its output to the shell as a command to execute.
A hook in this context is a mechanism to attach custom actions to some event processed by a software.
so $CONDA_PATH/bin/conda shell.bash hook will probably generate a bash command line which, when executed, will connect Anaconda into the running bash process, and eval "$($CONDA_PATH/bin/conda shell.bash hook)" will take care of executing that command. Try running $CONDA_PATH/bin/conda shell.bash hook directly from a shell prompt to see what it is producing, in order to better understand what is going on.

直接运行这个会打印出eval将会执行的脚本

/home/milton/miniconda3/bin/conda shell.bash hook

关于 conda 环境的启用和关闭

启用conda环境实际上涉及到两部分, 一个是将conda加入环境变量, 另一个是启用conda的虚拟python环境,
前者在执行eval后就一直有效, 直到你退出当前的命令行, 查看 $PATH 可以看到加入了两个目录 miniconda3/bin 和 miniconda3/condabin

$ echo $PATH
/home/milton/miniconda3/bin:/home/milton/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

后者可以用 activate 和 deactivate 来启用和关闭

# 关闭
(base) milton@somewhere:~$ conda deactivate
# 关闭后 (base) 消失, 但是 conda 命令还是可用的
milton@somewhere:~$ conda --version
conda 23.11.0
# 再次启用, 可以看到 (base) 又出现了
milton@somewhere:~$ conda activate
(base) milton@somewhere:~$ 

在 conda 环境下, 用的 python 版本是独立的, 和系统的 python 版本不一样

milton@somewhere:~$ python3 --version
Python 3.10.12

milton@somewhere:~$ conda activate
(base) milton@somewhere:~$ python3 --version
Python 3.11.5

conda环境管理

查看环境列表

这三个命令是一样的, 当前环境会用星号标识

conda info -e
conda info --envs
conda env list

创建环境

先deactivate关闭当前环境, 然后执行

conda create --name [env_name] python=[version]
# 或者
conda create -n [env_name] python=[version]

# 例如
conda create --name test3.6 python=3.6

会计算依赖然后提示需要下载按照的包列表, 安装结束后会提示启用命令

conda activate test3.6
# To deactivate an active environment, use
conda deactivate

启用后查看对应的python版本就变成了3.6

conda activate test3.6
(test3.6) milton@somewhere:~$ python --version
Python 3.6.13 :: Anaconda, Inc.

删除环境

conda env remove -n test3.6
conda remove -n test3.6 --all

conda 管理

升级

conda update --all

posted on 2024-02-20 20:05  Milton  阅读(1733)  评论(1编辑  收藏  举报

导航