本文档介绍如何在CPU环境的Linux系统上,使用pip方式快速安装MindSpore。下面以Ubuntu 18.04为例说明MindSpore安装步骤。
如果您想在一个全新的Ubuntu 18.04上通过pip安装MindSpore,可以使用自动安装脚本进行一键式安装,参见自动安装小节。自动安装脚本会安装MindSpore及其所需的依赖。
如果您的系统已经安装了部分依赖,如Python,GCC等,则推荐参照手动安装小节的安装步骤手动安装。
自动安装脚本需要更改软件源配置以及通过APT安装依赖,所以需要申请root权限。使用以下命令获取自动安装脚本并执行。自动安装脚本仅支持安装MindSpore>=1.6.0。
wget https://gitee.com/mindspore/mindspore/raw/master/scripts/install/ubuntu-cpu-pip.sh
# 安装MindSpore 2.2.0和Python 3.7
MINDSPORE_VERSION=2.2.0 bash ./ubuntu-cpu-pip.sh
# 如需指定Python和MindSpore版本,以Python 3.9和MindSpore 1.6.0为例,使用以下方式
# PYTHON_VERSION=3.9 MINDSPORE_VERSION=1.6.0 bash ./ubuntu-cpu-pip.sh
该脚本会执行以下操作:
自动安装脚本执行完成后,需要重新打开终端窗口以使环境变量生效。
更多的用法请参看脚本头部的说明。
下表列出了安装MindSpore所需的系统环境和第三方依赖。
软件名称 | 版本 | 作用 |
---|---|---|
Ubuntu | 18.04 | 运行MindSpore的操作系统 |
Python | 3.7-3.9 | MindSpore的使用依赖Python环境 |
GCC | 7.3.0到9.4.0之间 | 用于编译MindSpore的C++编译器 |
下面给出第三方依赖的安装方法。
Python可通过多种方式进行安装。
通过Conda安装Python
安装Miniconda:
cd /tmp
curl -O https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-py37_4.10.3-Linux-$(arch).sh
bash Miniconda3-py37_4.10.3-Linux-$(arch).sh -b
cd -
. ~/miniconda3/etc/profile.d/conda.sh
conda init bash
安装完成后,可以为Conda设置清华源加速下载,参考此处。
创建虚拟环境,以Python 3.7.5为例:
conda create -n mindspore_py37 python=3.7.5 -y
conda activate mindspore_py37
通过APT安装Python,命令如下。
sudo apt-get update
sudo apt-get install software-properties-common -y
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt-get install python3.7 python3.7-dev python3.7-distutils python3-pip -y
# 将新安装的Python设为默认
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 100
# 安装pip
python -m pip install pip -i https://repo.huaweicloud.com/repository/pypi/simple
sudo update-alternatives --install /usr/bin/pip pip ~/.local/bin/pip3.7 100
pip config set global.index-url https://repo.huaweicloud.com/repository/pypi/simple
若要安装其他Python版本,只需更改命令中的3.7
。
可以通过以下命令查看Python版本。
python --version
可以通过以下命令安装GCC。
sudo apt-get install gcc-7 -y
如果要安装更高版本的GCC,使用以下命令安装GCC 8。
sudo apt-get install gcc-8 -y
或者安装GCC 9。
sudo apt-get install software-properties-common -y
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-9 -y
首先参考版本列表选择想要安装的MindSpore版本,并进行SHA-256完整性校验。以2.2.0版本为例,执行以下命令。
export MS_VERSION=2.2.0
然后根据系统架构及Python版本执行如下命令安装MindSpore。
# x86_64 + Python3.7
pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/${MS_VERSION}/MindSpore/unified/x86_64/mindspore-${MS_VERSION/-/}-cp37-cp37m-linux_x86_64.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple
# x86_64 + Python3.8
pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/${MS_VERSION}/MindSpore/unified/x86_64/mindspore-${MS_VERSION/-/}-cp38-cp38-linux_x86_64.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple
# x86_64 + Python3.9
pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/${MS_VERSION}/MindSpore/unified/x86_64/mindspore-${MS_VERSION/-/}-cp39-cp39-linux_x86_64.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple
# aarch64 + Python3.7
pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/${MS_VERSION}/MindSpore/unified/aarch64/mindspore-${MS_VERSION/-/}-cp37-cp37m-linux_aarch64.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple
# aarch64 + Python3.8
pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/${MS_VERSION}/MindSpore/unified/aarch64/mindspore-${MS_VERSION/-/}-cp38-cp38-linux_aarch64.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple
# aarch64 + Python3.9
pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/${MS_VERSION}/MindSpore/unified/aarch64/mindspore-${MS_VERSION/-/}-cp39-cp39-linux_aarch64.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple
在联网状态下,安装whl包时会自动下载mindspore安装包的依赖项(依赖项详情参见setup.py中的required_package),其余情况需自行安装。运行模型时,需要根据ModelZoo中不同模型指定的requirements.txt安装额外依赖,常见依赖可以参考requirements.txt。
python -c "import mindspore;mindspore.set_context(device_target='CPU');mindspore.run_check()"
如果输出:
MindSpore version: 版本号
The result of multiplication calculation is correct, MindSpore has been installed on platform [CPU] successfully!
说明MindSpore安装成功了。
当需要升级MindSpore版本时,可执行如下命令:
pip install --upgrade mindspore=={version}
其中:
{version}
为rc版本号,例如1.6.0rc1;如果升级到正式版本,=={version}
字段可以缺省。此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。