Skip to content

Instantly share code, notes, and snippets.

@ziyuang
Created January 13, 2017 18:57
Show Gist options
  • Save ziyuang/301cdb32594c6e60702f386b86fb0d06 to your computer and use it in GitHub Desktop.
Save ziyuang/301cdb32594c6e60702f386b86fb0d06 to your computer and use it in GitHub Desktop.
Install MXNet (with Anaconda Python 3, CUDA, cuDNN, Intel MKL, OpenCV, Zsh) for p2.xlarge on Ubuntu 16.04 (ami-6f587e1c).
#!/bin/bash
set -e
# Put
# 1. CUDA installation script (cuda_*.run, see https://developer.nvidia.com/cuda-downloads)
# 2. cuDNN archive (cudnn-*.tgz, see https://developer.nvidia.com/rdp/cudnn-download)
# 3. Intel MKL archive (l_mkl_*.tgz, see https://software.intel.com/en-us/intel-mkl)
# under $HOME
# Also set INTEL_MKL_SN to Intel MKL's serial number (XXXX-XXXXXXXX)
# Use `source` to run the script as it modifies $PATH
ANACONDA_VERSION=4.2.0
OPENCV_VERSION=3.2.0
CUDA_COMPUTE_CAPABILITY=3.7 # K80
function append_to_bashrc_and_run()
{
local cmd_str=$1
echo ${cmd_str} >> /home/ubuntu/.bashrc
eval ${cmd_str}
}
sudo apt-get update
sudo apt-get -y install aptitude
sudo aptitude -y upgrade
sudo aptitude -y install build-essential cmake git unzip
# Python
ANACONDA_INSTALLATION_SCRIPT="Anaconda3-${ANACONDA_VERSION}-Linux-x86_64.sh"
wget "https://repo.continuum.io/archive/${ANACONDA_INSTALLATION_SCRIPT}"
bash ${ANACONDA_INSTALLATION_SCRIPT} -b
append_to_bashrc_and_run 'export PATH="$HOME/anaconda3/bin:$PATH"'
conda update -y --all
# CUDA
sudo aptitude -y install linux-source linux-headers-$(uname -r) dkms
CUDA_INSTALLATION_SCRIPT=$(ls cuda_*.run)
sudo bash ${CUDA_INSTALLATION_SCRIPT} --silent --driver --toolkit --samples
append_to_bashrc_and_run 'export PATH="/usr/local/cuda/bin:$PATH"'
append_to_bashrc_and_run 'export LD_LIBRARY_PATH="/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"'
append_to_bashrc_and_run 'export CPATH="/usr/local/cuda/include${CPATH:+:${CPATH}}"'
# cuDNN
CUDNN_ARCHIVE=$(ls cudnn-*.tgz)
tar -xzf ${CUDNN_ARCHIVE}
sudo rsync -a cuda/* /usr/local/cuda/
# MKL
MKL_ARCHIVE=$(ls l_mkl_*.tgz)
tar -xzf ${MKL_ARCHIVE}
MKL_FOLDER=${MKL_ARCHIVE//\.tgz/}
cd ${MKL_FOLDER}
sed -i -r 's/^ACCEPT_EULA=.+$/ACCEPT_EULA=accept/' silent.cfg
sed -i -r "s/^#?ACTIVATION_SERIAL_NUMBER=.+$/ACTIVATION_SERIAL_NUMBER=${INTEL_MKL_SN}/" silent.cfg
sed -i -r 's/^ACTIVATION_TYPE=.+$/ACTIVATION_TYPE=serial_number/' silent.cfg
sed -i -r 's/^ARCH_SELECTED=.+$/ARCH_SELECTED=INTEL64/' silent.cfg
sudo bash install.sh --silent silent.cfg
cd ..
append_to_bashrc_and_run 'source /opt/intel/mkl/bin/mklvars.sh intel64'
append_to_bashrc_and_run 'export CPATH="/opt/intel/mkl/include:${CPATH}"'
# OpenCV
sudo aptitude -y install libgtk3.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev libjasper-dev libdc1394-22-dev libavresample-dev
wget https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip
unzip ${OPENCV_VERSION}.zip
cd opencv-${OPENCV_VERSION}
mkdir build
cd build
cmake -D CUDA_ARCH_PTX="" -DCUDA_ARCH_BIN=${CUDA_COMPUTE_CAPABILITY} -DBUILD_opencv_ts=0 ..
make -j$(nproc)
sudo make install
cd ../..
# MXNet
sudo ldconfig
sudo aptitude -y install libssl-dev libcurl4-openssl-dev
git clone https://github.com/dmlc/mxnet.git --recursive
cd mxnet
cp make/config.mk .
sed -i -r 's/^USE_CUDA_PATH.+$/USE_CUDA_PATH=\/usr\/local\/cuda/' config.mk
# USE_CUDA_PATH=/usr/local/cuda
sed -i -r 's/^USE_CUDA[^_]+$/USE_CUDA=1/' config.mk # USE_CUDA=1
sed -i -r 's/^USE_CUDNN.+$/USE_CUDNN=1/' config.mk # USE_CUDNN=1
sed -i -r 's/^USE_NVRTC.+$/USE_NVRTC=1/' config.mk # USE_NVRTC=1
sed -i -r 's/^USE_INTEL_PATH.+$/USE_INTEL_PATH=\/opt\/intel\nUSE_BLAS=mkl/' config.mk
# USE_INTEL_PATH=/opt/intel
# USE_BLAS=mkl
sed -i -r 's/^USE_S3.+$/USE_S3=1/' config.mk # USE_S3=1
MXNET_CUDA_COMPUTE_CAPABILITY=${CUDA_COMPUTE_CAPABILITY//\./}
sed -i -r 's/-gencode.+//' config.mk
sed -i -r "s/^CUDA_ARCH.+$/CUDA_ARCH:=-gencode arch=compute_${MXNET_CUDA_COMPUTE_CAPABILITY},code=sm_${MXNET_CUDA_COMPUTE_CAPABILITY}/" config.mk
# CUDA_ARCH:=-gencode arch=compute_37,code=sm_37
# Link with libopencv_highgui instead of libopencv_imgcodecs if in OpenCV 2.4
echo "ADD_LDFLAGS += -lopencv_core -lopencv_imgproc -lopencv_imgcodecs" >> config.mk
make -j$(nproc)
cd python
python setup.py install
cd ../..
# Clean
rm -f ${ANACONDA_INSTALLATION_SCRIPT}
rm -f ${CUDA_INSTALLATION_SCRIPT}
rm -f ${CUDNN_ARCHIVE}
rm -rf cuda
rm -f ${MKL_ARCHIVE}
rm -rf ${MKL_FOLDER}
rm -f ${OPENCV_VERSION}.zip
unset INTEL_MKL_SN
# ZSH
sudo aptitude -y install zsh
sudo chsh -s $(which zsh) ubuntu
sudo zsh -c "echo $(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -) | sed 's/env zsh//g'"
sudo chown ubuntu:ubuntu .z*
sudo chown -R ubuntu:ubuntu .oh-my-zsh
sed -i -r 's/^ZSH_THEME=".+"$/ZSH_THEME="gallois"/' .zshrc # personal favorite theme
tail -n 6 .bashrc >> .zshrc
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment