Ubuntu 20.04 安装 Anaconda 超详细教程
一、准备工作
1.1 系统要求
- Ubuntu 20.04 64位系统
- 至少 3 GB 可用磁盘空间
- 稳定网络连接(下载约500MB安装包)
1.2 更新系统包
# 更新包列表
sudo apt update
# 升级已安装的包
sudo apt upgrade -y
# 安装必要的依赖
sudo apt install wget curl -y
二、下载 Anaconda 安装包
2.1 访问 Anaconda 官网
打开浏览器访问:https://www.anaconda.com/products/distribution
或者使用命令行下载(推荐使用清华大学镜像源加速):
2.2 使用命令行下载(方法一:官方源)
# 进入临时目录
cd /tmp
# 查看最新版本(访问官网获取最新链接)
# 下载 Anaconda(以 Python 3.9 版本为例)
wget https://repo.anaconda.com/archive/Anaconda3-2023.03-1-Linux-x86_64.sh
2.3 使用命令行下载(方法二:国内镜像源,推荐)
# 清华大学镜像源(更快)
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2023.03-1-Linux-x86_64.sh
# 或者中科大镜像源
# wget https://mirrors.ustc.edu.cn/anaconda/archive/Anaconda3-2023.03-1-Linux-x86_64.sh
三、安装 Anaconda
3.1 验证安装包完整性
# 查看下载的文件
ls -lh Anaconda3*.sh
# 计算SHA256校验和(可选)
sha256sum Anaconda3-2023.03-1-Linux-x86_64.sh
# 应该输出:e7e5c4eabd8bc0eb4f65c1d4ae0ae541e8c7c5c4e10e62592e9d5aae3a9e6c4d
3.2 运行安装脚本
# 添加执行权限
chmod +x Anaconda3-2023.03-1-Linux-x86_64.sh
# 运行安装脚本
bash Anaconda3-2023.03-1-Linux-x86_64.sh
3.3 安装过程详解
步骤1:阅读许可协议
Welcome to Anaconda3 2023.03
In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>>
- 按
Enter 键逐页查看协议
- 查看完后输入
yes 接受协议
步骤2:选择安装位置
Anaconda3 will now be installed into this location:
/home/yourusername/anaconda3
- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below
[/home/yourusername/anaconda3] >>>
- 按
Enter 使用默认位置(推荐)
- 或输入自定义路径
步骤3:初始化 Anaconda
Do you wish the installer to initialize Anaconda3
by running conda init? [yes|no]
[no] >>> yes
- 重要:输入
yes(这样会在shell配置文件中添加conda初始化)
四、配置环境变量
4.1 激活 conda 环境
# 重新加载 shell 配置(如果选择conda init)
source ~/.bashrc
# 或者手动添加环境变量(如果conda init失败)
# echo 'export PATH="/home/yourusername/anaconda3/bin:$PATH"' >> ~/.bashrc
# source ~/.bashrc
4.2 验证安装
# 检查 conda 版本
conda --version
# 检查安装的包
conda list
# 检查 Python 版本
python --version
五、配置 Anaconda
5.1 设置国内镜像源(加速下载)
# 添加清华镜像源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
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/
# 显示下载源URL
conda config --set show_channel_urls yes
# 查看当前配置
conda config --show channels
# 移除默认源(可选)
conda config --remove channels defaults
5.2 创建和管理环境
# 创建新环境
conda create -n myenv python=3.9
# 激活环境
conda activate myenv
# 列出所有环境
conda env list
# 退出当前环境
conda deactivate
# 删除环境
conda remove -n myenv --all
5.3 常用 conda 命令
# 安装包
conda install numpy pandas matplotlib
# 更新包
conda update numpy
# 更新 conda 自身
conda update conda
conda update anaconda
# 搜索包
conda search tensorflow
# 查看环境信息
conda info
六、配置 IDE 支持
6.1 配置 VS Code
# 安装 VS Code(如果未安装)
sudo snap install code --classic
# 在 VS Code 中安装 Python 扩展
# 1. 打开 VS Code
# 2. 按 Ctrl+Shift+X 打开扩展商店
# 3. 搜索并安装 "Python" 扩展
# 4. 按 Ctrl+Shift+P,输入 "Python: Select Interpreter"
# 5. 选择 Anaconda 的 Python 解释器
6.2 配置 Jupyter Notebook
# 生成 Jupyter 配置文件
jupyter notebook --generate-config
# 设置密码
jupyter notebook password
# 启动 Jupyter Notebook
jupyter notebook
# 或在后台运行
jupyter notebook --no-browser &
七、常见问题解决
7.1 命令未找到
# 如果 conda 命令不可用
source ~/anaconda3/etc/profile.d/conda.sh
conda activate base
7.2 恢复 shell 提示符
# 如果提示符显示 (base),想要禁用自动激活
conda config --set auto_activate_base false
source ~/.bashrc
# 重新启用
conda config --set auto_activate_base true
7.3 卸载 Anaconda
# 删除整个 Anaconda 目录
rm -rf ~/anaconda3
# 从 .bashrc 中移除相关行
# 编辑 ~/.bashrc,删除 conda 相关的部分
nano ~/.bashrc
# 删除类似以下内容
# >>> conda initialize >>>
# ... conda 配置 ...
# <<< conda initialize <<<
八、实用技巧
8.1 创建常用环境模板
# 数据科学环境
conda create -n datascience python=3.9 numpy pandas matplotlib scikit-learn jupyter
# 机器学习环境
conda create -n ml python=3.8 tensorflow-gpu keras pytorch torchvision
# Web 开发环境
conda create -n webdev python=3.9 django flask fastapi
8.2 导出和导入环境配置
# 导出当前环境配置
conda env export > environment.yml
# 从配置文件创建环境
conda env create -f environment.yml
# 导出精确版本(包括 pip 安装的包)
conda list --explicit > spec-file.txt
conda create --name newenv --file spec-file.txt
九、性能优化建议
9.1 清理缓存
# 清理未使用的包和缓存
conda clean --all
# 只清理 tarballs
conda clean --tarballs
9.2 更新所有包
# 更新所有包(谨慎操作)
conda update --all
十、验证完整安装
运行以下脚本验证安装:
# 创建测试脚本 test_anaconda.py
echo '
import sys
print(f"Python版本: {sys.version}")
print(f"Python路径: {sys.executable}")
try:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
print("✓ NumPy 版本:", np.__version__)
print("✓ Pandas 版本:", pd.__version__)
# 创建简单的图表
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title("Anaconda 安装测试")
plt.savefig("test_plot.png")
print("✓ Matplotlib 测试通过 - 图表已保存")
except ImportError as e:
print(f"✗ 导入错误: {e}")
print("\nAnaconda 安装验证完成!")
' > test_anaconda.py
# 运行测试
python test_anaconda.py
至此,Anaconda 已在 Ubuntu 20.04 上成功安装并配置完成!