1#!/bin/bash 2# Copyright 2022 Huawei Technologies Co., Ltd 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# ============================================================================ 16 17# Prepare and Install mindspore cpu by pip on Ubuntu 18.04. 18# 19# This file will: 20# - change deb source to huaweicloud mirror 21# - install mindspore dependencies via apt like gcc, libgmp 22# - install python3 & pip3 via apt and set it to default 23# - install mindspore-cpu within new installed python by pip 24# 25# Augments: 26# - PYTHON_VERSION: python version to install. [3.7(default), 3.8, 3.9] 27# - MINDSPORE_VERSION: mindspore version to install, >=1.6.0, required 28# 29# Usage: 30# Run script like `MINDSPORE_VERSION=1.7.0 bash ./ubuntu-cpu-pip.sh`. 31# To set augments, run it as `PYTHON_VERSION=3.9 MINDSPORE_VERSION=1.6.0 bash ./ubuntu-cpu-pip.sh`. 32 33set -e 34 35PYTHON_VERSION=${PYTHON_VERSION:-3.7} 36MINDSPORE_VERSION=${MINDSPORE_VERSION:-EMPTY} 37 38version_less() { 39 test "$(echo "$@" | tr ' ' '\n' | sort -rV | head -n 1)" != "$1"; 40} 41 42if [ $MINDSPORE_VERSION == "EMPTY" ] || version_less "${MINDSPORE_VERSION}" "1.6.0"; then 43 echo "MINDSPORE_VERSION should be >=1.6.0, please check available versions at https://www.mindspore.cn/versions." 44 exit 1 45fi 46 47available_py_version=(3.7 3.8 3.9) 48if [[ " ${available_py_version[*]} " != *" $PYTHON_VERSION "* ]]; then 49 echo "PYTHON_VERSION is '$PYTHON_VERSION', but available versions are [${available_py_version[*]}]." 50 exit 1 51fi 52 53if [[ "$PYTHON_VERSION" == "3.8" && ${MINDSPORE_VERSION:0:3} == "1.6" ]]; then 54 echo "PYTHON_VERSION==3.8 is not compatible with MINDSPORE_VERSION==1.6.x, please use PYTHON_VERSION==3.7 or 3.9 for MINDSPORE_VERSION==1.6.x." 55 exit 1 56fi 57 58declare -A version_map=() 59version_map["3.7"]="${MINDSPORE_VERSION/-/}-cp37-cp37m" 60version_map["3.8"]="${MINDSPORE_VERSION/-/}-cp38-cp38" 61version_map["3.9"]="${MINDSPORE_VERSION/-/}-cp39-cp39" 62 63# use huaweicloud mirror in China 64sudo sed -i "s@http://.*archive.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list 65sudo sed -i "s@http://.*security.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list 66sudo apt-get update 67sudo apt-get install gcc-7 libgmp-dev -y 68 69# python 70sudo add-apt-repository -y ppa:deadsnakes/ppa 71sudo apt-get install python$PYTHON_VERSION python$PYTHON_VERSION-distutils python3-pip -y 72sudo update-alternatives --install /usr/bin/python python /usr/bin/python$PYTHON_VERSION 100 73# pip 74python -m pip install -U pip -i https://pypi.tuna.tsinghua.edu.cn/simple 75echo -e "alias pip='python -m pip'" >> ~/.bashrc 76python -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple 77 78# install mindspore whl 79arch=`uname -m` 80python -m pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/${MINDSPORE_VERSION}/MindSpore/unified/${arch}/mindspore-${version_map["$PYTHON_VERSION"]}-linux_${arch}.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple 81 82# check mindspore installation 83python -c "import mindspore;mindspore.run_check()" 84