Linux 下配置 conda 环境

loskyertt Unknown

一、安装

下载最新版miniconda3

1
2
# 下载适用于 Linux 的 Miniconda 安装包(根据你系统的架构选择 64-bit 或 32-bit)
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

下载完成后,通过以下命令启动安装:

1
2
3
4
5
# 赋予安装脚本执行权限
chmod +x Miniconda3-latest-Linux-x86_64.sh

# 执行安装脚本
./Miniconda3-latest-Linux-x86_64.sh

对于miniforge,下载:

1
2
3
4
5
# 如果是 x86_64 架构(大多数 PC)
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-x86_64.sh

# 如果是 ARM64 架构(比如 Raspberry Pi)
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-aarch64.sh

下载完成后,运行以下命令安装:

1
2
3
4
5
# 赋予安装脚本执行权限
chmod +x Miniforge3-Linux-*.sh

# 执行安装脚本
./Miniforge3-Linux-*.sh

二、miniconda3 配置

注: 如果用的是miniforge,操作方式与此类似。

2.1 把conda集成到zsh终端中

2.1.1 详细步骤

  1. 添加conda环境变量:

把这行代码加入到.zshrc中:

1
export PATH=/opt/miniconda3/bin:$PATH

终端输入这行可以防止conda自动激活环境(建议加上):

1
conda config --set auto_activate_base false
  1. 重新加载 ~/.zshrc 文件:

在终端中运行以下命令重新加载配置文件:

1
source ~/.zshrc
  1. 运行 conda init

初始化 conda 环境:

1
2
# 这是conda默认安装的位置
/opt/miniconda3/bin/conda init zsh
  1. 再次重新加载 ~/.zshrc 文件:

再次运行以下命令重新加载配置文件:

1
source ~/.zshrc

2.1.2 验证

验证 conda 是否配置好:

1
conda --version

2.1.3 一步到位操作

可以不用管以上操作,直接把这段复制到.zshrc中,注意安装miniconda的路径:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/miniconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/opt/miniconda3/etc/profile.d/conda.sh" ]; then
. "/opt/miniconda3/etc/profile.d/conda.sh"
else
export PATH="/opt/miniconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
export CRYPTOGRAPHY_OPENSSL_NO_LEGACY=1

2.2 问题汇总

2.2.1 OpenSSL 问题

当运行conda activate base时,可能出现下面问题:

1
2
3
4
 $ conda activate base
Error while loading conda entry point: conda-content-trust (OpenSSL 3.0's legacy provider failed to load. This is a fatal error by default, but cryptography supports running without legacy algorithms by setting the environment variable CRYPTOGRAPHY_OPENSSL_NO_LEGACY. If you did not expect this error, you have likely made a mistake with your OpenSSL configuration.)

CondaError: Run 'conda init' before 'conda activate'

根据该的错误信息,问题可能与 OpenSSL 版本有关。OpenSSL 3.0 引入了一些变化,可能导致与某些软件包的兼容性问题。在此情况下,设置环境变量CRYPTOGRAPHY_OPENSSL_NO_LEGACY可能会解决问题。

  • 解决方法:

编辑 ~/.zshrc 文件,添加以下行到 ~/.zshrc 文件:

1
export CRYPTOGRAPHY_OPENSSL_NO_LEGACY=1

保存并退出编辑器,然后执行source ~/.zshrc

  • Title: Linux 下配置 conda 环境
  • Author: loskyertt
  • Created at : 2024-08-19 16:21:15
  • Updated at : 2025-02-17 04:36:55
  • Link: https://redefine.ohevan.com/2024/08/19/Linux-conda/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments