在 Ubuntu/CentOS 上安装和配置 Miniconda

在 Ubuntu/CentOS上安装 Miniconda 是一个相对简单的过程。Miniconda 是 Anaconda 的轻量级版本,只提供了最基础的包管理功能,非常适合那些只想要安装特定包而不需要整个 Anaconda 发行版的用户。下面是安装 Miniconda 的步骤:

1. 下载 Miniconda 安装脚本

首先,你需要下载适合 Linux 系统的 Miniconda 安装脚本。可以使用 wget 或 curl 来下载。

使用 wget 下载:

bash 复制代码
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

使用 curl 下载:

bash 复制代码
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

2. 验证下载的脚本(可选)

你可以对下载的脚本进行校验(可选),以确保下载完整且未被篡改。官方提供了 SHA256 校验和,你可以与下载文件进行比较。

首先,使用以下命令生成文件的 SHA256 校验和:

bash 复制代码
sha256sum Miniconda3-latest-Linux-x86_64.sh

然后,访问 Miniconda 下载页面,找到该文件的 SHA256 校验和,并将其与你生成的校验和进行比较。

3. 运行安装脚本

在终端中,使用以下命令为 Miniconda 安装脚本添加执行权限:

bash 复制代码
chmod +x Miniconda3-latest-Linux-x86_64.sh

然后,运行安装脚本:

bash 复制代码
./Miniconda3-latest-Linux-x86_64.sh

4. 安装过程

安装脚本运行后,会提示你进行一些配置:

  • 许可协议 :你需要阅读并接受许可协议,输入 yes 来接受。
  • 安装位置 :你可以选择默认的安装路径(通常是 $HOME/miniconda3),或者输入自定义路径来改变安装目录。默认的路径是用户主目录下的 miniconda3 文件夹。

5. 初始化 Miniconda

安装完成后,你会看到类似以下的提示:

arduino 复制代码
Miniconda3 has been successfully installed.
Please close and reopen your terminal to begin using Miniconda.

这时,你需要初始化 Miniconda,这会修改你的 shell 配置文件(比如 .bashrc、.zshrc 等,如果看不明白选bash就对了),以便在启动终端时自动激活 conda 环境。

执行以下命令来初始化 Miniconda:

bash 复制代码
~/miniconda3/bin/conda init

如果你使用的是 Bash,系统会自动修改 .bashrc 文件。如果你使用的是 Zsh 等其他 shell,你可能需要手动修改对应的 shell 配置文件。

6. 重新启动终端

初始化完成后,关闭当前终端窗口并重新打开一个新的终端,或者运行以下命令来重新加载 shell 配置:

bash 复制代码
source ~/.bashrc  # 对于 Bash 用户
# 或者
source ~/.zshrc   # 对于 Zsh 用户

7. 验证安装

通过运行以下命令来验证 Miniconda 是否安装成功:

bash 复制代码
conda --version

你应该会看到类似如下的输出:

复制代码
conda 23.1.0

8.conda和pip换源

在使用 pip 和 conda 时,是否需要换源取决于你的网络环境。对于国内用户,通常建议更换镜像源,因为默认的官方源(如 PyPI 或 Anaconda 源)可能因网络延迟导致下载速度缓慢或不稳定。

对于conda而言,源仓库选择清华仓库:官方教程

方法一 通过命令修改

bash 复制代码
# 下载时显示仓库来源
conda config --set show_channel_urls yes
# 添加官方仓库镜像
conda config --add default_channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --add default_channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
conda config --add default_channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
# (可选)添加conda-forge仓库镜像(社区管理的conda仓库)和PyTorch仓库镜像( PyTorch 项目的官方 conda 仓库)
conda config --add custom_channels.pytorch https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
conda config --add custom_channels.conda-forge https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
# 清除索引缓存
conda clean -i

方法二 除了用通过命令也可以手动修改文件实现

bash 复制代码
vim ~/.condarc 或者 nano ~/.condarc 或者 其他编辑器
/*文件不存在就新建,然后将文件内容修改为以下
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
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
*/
# 清除索引缓存
conda clean -i

对于pip而言,源仓库选择清华仓库:官方教程

方法一 临时使用第三方源(因为conda镜像仓库比pip的稳定,所以conda一般都不使用临时源)

bash 复制代码
# 以下载notebook为例

#清华源
pip install notebook -i https://pypi.tuna.tsinghua.edu.cn/simple
# 阿里源
pip install notebook -i https://mirrors.aliyun.com/pypi/simple/
# 腾讯源
pip install notebook -i http://mirrors.cloud.tencent.com/pypi/simple
# 豆瓣源
pip install notebook -i http://pypi.douban.com/simple/

方法二 通过命令修改

bash 复制代码
# 升级pip
python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip

# 设置默认源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# 添加额外源,pip只支持一个默认源,其他的源只能通过额外源的方式添加
pip config set global.extra-index-url https://mirrors.aliyun.com/pypi/simple/
pip config set global.extra-index-url https://mirrors.cloud.tencent.com/pypi/simple
pip config set global.extra-index-url https://pypi.douban.com/simple/

方法三 通过文件修改

bash 复制代码
vim ~/.config/pip/pip.conf 或者 其他编辑器
/*
[global]
# 默认源为清华源
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
extra-index-url =
    https://mirrors.aliyun.com/pypi/simple/
    https://mirrors.cloud.tencent.com/pypi/simple
    https://pypi.douban.com/simple/
*/

top:经测试清华源成分太复杂,如果挂了代理可能访问不了,其他源好像没这问题

9. 更新 Conda(可选)

虽然 Miniconda 会安装最新版本的 conda,但你可以运行以下命令来确保 conda 是最新版本:

bash 复制代码
conda update conda

10. 安装其他软件包(可选)

现在,你可以使用 conda 来安装所需的其他软件包。例如,安装 numpy:

bash 复制代码
conda install numpy

总结

  1. 下载 Miniconda 安装脚本。
  2. 赋予执行权限并运行安装脚本。
  3. 初始化 Miniconda 配置。
  4. 重新加载 shell 配置文件。
  5. 换源使用。
  6. 验证安装是否成功,并可以开始使用 conda 安装更多的包。
相关推荐
ragnwang6 小时前
Notes: Python pybind11 import C++ module
开发语言·c++·python
王中阳Go6 小时前
后端转 Agent 岗面试怎么答:8 个高频问题与应答口径
人工智能·后端·python·大语言模型
weixin_307779137 小时前
RAG 面试陷阱——混合检索并非无条件优越:一个条件反转实验
人工智能·python·面试·职场和发展
云和数据.ChenGuang8 小时前
深入解析 Python 的 dir():从用法到 CPython 源码实现
开发语言·人工智能·python·langchain
荣码11 小时前
长上下文与大文档处理:200页PDF怎么喂给AI,我踩了4个坑
java·python
databook11 小时前
Pandas 与 Polars:语法、性能和资源占用方面的对比
python·数据分析·pandas
威哥爱编程12 小时前
Python 智能体开发实战 10:本地跑得好好的,一并发就崩?把它做成一个能上线的服务
python·agent
威哥爱编程12 小时前
Python 智能体开发实战 09:改了提示词,到底是变好还是变坏?搭一套能打分的离线评测
python·agent
meiying883812 小时前
多快递公司快递单号批量查询接口怎么对接?Python 聚合查询实现
java·大数据·python
Hello_Pyhx12 小时前
Deep-Live-Cam 源码配置思路:环境、模型与执行后端分开排查
python