背景

之前一直用anaconda,偶然发现实在是太占用空间了,我只有2个环境,足足占用了7G多的空间,趁着今天手误一不小心升级了一下python版本,导致环境直接崩溃,彻底的把 anaconda 更换为 miniconda,默认的环境1000多个库也没用过,要他有何用。

更换步骤

1、删除anaconda

首先彻底删除之前安装的anaconda文件和配置信息,先删除主目录下的 ~/anaconda3 文件夹,可以手动删除,也可以使用以下代码在终端删除,亲测终端删除速度稍微快一点

# 删除anaconda文件
rm -rf ~/anaconda3

删除系统和用户配置信息中关于conda的代码,手动打开主目录下的 ~/.bashrc 文件删除conda的配置信息,如果没有就按 Ctrl+H 显示隐藏文件和文件夹,也可以在终端中使用vim删除。

# vim打开 ~/.bashrc
sudo vim ~/.bashrc
# 删除conda相关的代码,类似下面这样的
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/lc/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/lc/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/lc/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/lc/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<
# vim打开 /etc/profile
sudo vim /etc/profile
# 删除conda相关的代码

2、下载miniconda

可以手动从以下镜像下载,也可以使用 wget 在终端中下载,我这里下载的是最新的 Miniconda-latest-Linux-x86_64.sh

  • 官方镜像https://docs.conda.io/en/latest/miniconda.html
  • Miniconda3-latest-Linux-x86_64.shhttps://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
  • 清华镜像https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/
  • Miniconda3-latest-Linux-x86_64.shhttps://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh

3、安装miniconda

打开深度终端,切换到刚才的下载目录,执行以下代码

# 安装
bash Miniconda-latest-Linux-x86_64.sh
# 开始疯狂的enter/backspace/yes
# 输入yes接受条约
Do you accept the license terms? [yes|no]
# enter,安装到默认的主目录下
[/home/lc/miniconda3] >>> 
# 输入yes,自动配置环境变量
installation finished.
Do you wish the installer to initialize Miniconda2
in your /home/you/.bashrc ? [yes|no]
# 出现这个代表安装成功
Thank you for installing Miniconda2!
# 更新系统配置信息
source ~/.bashrc
# 输入以下代码,出现conda的帮助文档信息,即表明conda安装成功
conda --help
conda子命令的帮助文档
conda create --help

4、更改镜像源

终端依次执行以下代码,将下载的镜像源改为清华的镜像

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
conda config --set show_channel_urls yes

开始使用

和anaconda命令一样,为了加快pip安装速度,将其镜像源改为清华,请参考《使用pypi国内镜像资源站解决Python工具包安装失败》http://www.dlc618.com/post-881.html

# 创建虚拟环境
conda create -n test python=3.6
# 进入虚拟环境
conda activate test
# pip镜像改为清华
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# pip安装库
pip install opencv-python==3.4.3.18