• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 conda 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 conda and set up environment for mindspore
23#   - install mindspore-cpu by conda
24#
25# Augments:
26#   - PYTHON_VERSION: python version to set up. [3.7(default), 3.8, 3.9]
27#   - MINDSPORE_VERSION: mindspore version to install, >=1.6.0
28#
29# Usage:
30#   Run script like `bash ./ubuntu-cpu-conda.sh`.
31#   To set augments, run it as `PYTHON_VERSION=3.9 MINDSPORE_VERSION=1.6.0 bash ./ubuntu-cpu-conda.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 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
58install_conda() {
59    echo "installing Miniconda3"
60    conda_file_name="Miniconda3-py3${PYTHON_VERSION##*.}_4.10.3-Linux-$(arch).sh"
61    cd /tmp
62    wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/$conda_file_name
63    bash $conda_file_name -b
64    cd -
65    . ~/miniconda3/etc/profile.d/conda.sh
66    conda init bash
67    # setting up conda mirror with tsinghua source
68    cat >~/.condarc <<END
69channels:
70  - defaults
71show_channel_urls: true
72default_channels:
73  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
74  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
75  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
76custom_channels:
77  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
78  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
79  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
80  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
81  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
82END
83}
84
85# use huaweicloud mirror in China
86sudo sed -i "s@http://.*archive.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list
87sudo sed -i "s@http://.*security.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list
88sudo apt-get update
89
90sudo apt-get install gcc-7 libgmp-dev -y
91
92# install conda
93set +e && type conda &>/dev/null
94if [[ $? -eq 0 ]]; then
95    echo "conda has been installed, skip."
96    source "$(conda info --base)"/etc/profile.d/conda.sh
97else
98    install_conda
99fi
100set -e
101
102# set up conda env and install mindspore-cpu
103env_name=mindspore_py3${PYTHON_VERSION##*.}
104conda create -n $env_name python=${PYTHON_VERSION} -c conda-forge -y
105conda activate $env_name
106install_name="mindspore"
107if [[ $MINDSPORE_VERSION != "EMPTY" ]]; then
108    install_name="${install_name}=${MINDSPORE_VERSION}"
109fi
110conda install ${install_name} -c mindspore -c conda-forge -y
111
112# check mindspore installation
113python -c "import mindspore;mindspore.run_check()"
114