安装 miniconda

1、下载安装包 Miniconda3-py37_22.11.1-1-Linux-x86_64.sh,或者自行选择版本

2、把安装包上传到服务器上,这里放在 /home/software

3、安装

bash Miniconda3-py37_22.11.1-1-Linux-x86_64.sh 

4、按回车

Welcome to Miniconda3 py37_22.11.1-1

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>> 

5、按空格跳到最下面,输入yes

Do you accept the license terms? [yes|no]
[no] >>>  yes

6、选择安装位置,这里选择默认,直接回车,有需要可以自己输入改掉

Miniconda3 will now be installed into this location:
/root/miniconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/root/miniconda3] >>> 
PREFIX=/root/miniconda3

7、初始化 miniconda,输入 yes

installation finished.
Do you wish the installer to initialize Miniconda3
by running conda init? [yes|no]
[no] >>> yes

8、现在 conda 命令是找不到的,需要激活

source ~/.bashrc

激活后可以看到启动了 base 环境,conda 命令也可以用了,下一节会介绍常用 conda 命令。

9、可以设置启动时,不自动激活 base 环境

conda config --set auto_activate_base false

10、设置 conda 镜像源

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

11、设置 pip 镜像

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set install.trusted-host mirrors.aliyun.com

12、查看 python 环境

(base) [root@xxx01 software]# python
Python 3.7.15 (default, Nov 24 2022, 21:12:53) 
[GCC 11.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello')
hello
>>> exit()

可以看到默认的 python 环境是 3.7,也就是我们安装的 miniconda 的 python 版本。

并且 python2 还是存在的。

(base) [root@xxx01 software]# python2
Python 2.7.5 (default, Oct 14 2020, 14:45:30) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello')
hello

使用 miniconda 管理 python 多环境

这里简单介绍一些常用的 conda 命令。

1、查看所有环境

conda env list

2、激活某个环境, 为环境名

conda activate <name>

3、退出当前环境

conda deactivate

4、创建虚拟环境,指定名字和 python 版本

conda create --name <name> python=3.x

5、克隆一个环境

conda create --name <new_name> --clone <old_name>

6、删除某个环境

conda remove --name <name> --all

7、conda 清空缓存

conda clean -y -all

8、conda 镜像源

# 查看镜像源
conda config --show-sources
# 添加镜像源
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 --set show_channel_urls yes
# 删除镜像源
conda config --remove channels https://XXX
# 删除配置的镜像源,使用默认镜像源
conda config --remove-key channels

9、pip 镜像源

# 查看配置
pip config list
# 添加镜像源
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set install.trusted-host mirrors.aliyun.com
# 清除缓存
rm -rf ~/.cache/pip
# windows 下 pip 配置文件位置
C:\Users\Administrator\AppData\Roaming\pip

或者直接配置 pip 镜像源,pip.ini

[global]
cache-dir = D:\Environment\Miniconda3\pip\cache
index-url = https://mirrors.aliyun.com/pypi/simple/
extra-index-url=
        https://pypi.tuna.tsinghua.edu.cn/simple/
        http://pypi.douban.com/simple/
        http://pypi.mirrors.ustc.edu.cn/simple/

[install]
trusted-host=
        pypi.tuna.tsinghua.edu.cn
        mirrors.aliyun.com
        pypi.douban.com
        pypi.mirrors.ustc.edu.cn

10、导出与导入当前环境配置

conda env export > environment.yaml
conda env create -f environment.yaml

卸载 miniconda

1、删除 miniconda3 文件夹

rm -rf ~/miniconda3/

2、删除 .conda 文件夹和 condarc 文件

rm -rf ~/.conda
rm -rf ~/.condarc

3、删除配置中 conda 相关

vim ~/.bashrc

删除或注释下面这段

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/root/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/root/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/root/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/root/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

4、刷新环境

source ~/.bashrc