Skip to content

Instantly share code, notes, and snippets.

@xiamaz
Last active October 2, 2018 22:03
Show Gist options
  • Save xiamaz/b148b5f1ecc68c85b5d34ea15868d73b to your computer and use it in GitHub Desktop.
Save xiamaz/b148b5f1ecc68c85b5d34ea15868d73b to your computer and use it in GitHub Desktop.
Automatic setup of nvidia docker and some convenience features on Ubuntu 16.04
#!/bin/sh
APPLICATIONS="$HOME/Apps"
mkdir -p $APPLICATIONS
LOCALBIN="$HOME/.local/bin"
mkdir -p $LOCALBIN
install_fun() {
prog=$1
if ! command -v $1 > /dev/null; then
echo "$1 is not available. Trying to install it."
sudo apt-get install $1
fi
}
install_appimage() {
url=$1
name=$2
wget $url -O $APPLICATIONS/$name.appimage
chmod +x $APPLICATIONS/$name.appimage
ln -s $APPLICATIONS/$name.appimage $LOCALBIN/$name
}
# install fish from latest ppa
sudo apt-add-repository -y ppa:fish-shell/release-2
sudo add-apt-repository -y ppa:aacebedo/fasd
sudo apt-get update && sudo apt-get install -y fish fasd
# install aws tools
sudo apt-get install -y awscli
# install latest neovim as appimage
sudo apt-get install -y python3-pip
sudo pip3 install neovim
install_appimage https://github.com/neovim/neovim/releases/download/v0.3.1/nvim.appimage nvim
# Install Configuration files
install_fun git
install_fun stow
if ! [ -d "Configurations" ]; then
git clone https://github.com/xiamaz/Configurations.git
fi
# Create configs
cd ~/Configurations
make fish
make tmux
make neovim
make git
#!/bin/sh
NVIDIA_REPO=/etc/apt/sources.list.d/nvidia-ml.list
install_nvidia_ml_repo () {
if [ -e $NVIDIA_REPO ]; then
echo "Repo already added."
return
fi
sudo sh -c "echo \"deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64 /\" > $NVIDIA_REPO"
}
remove_nvidia_ml_repo () {
rm -r $NVIDIA_REPO
}
CUDA_VERSION=9.0
CUDNN_VERSION="7.3.1.20-1+cuda$CUDA_VERSION"
NCCL_VERSION="2.3.5-2+cuda$CUDA_VERSION"
install_nccl() {
nccl_file=nccl_${NCCL_VERSION}_x86_64.txz
if ! [ -e $nccl_file ]; then
echo "Please download $nccl_file"
return
fi
sudo cp $nccl_file /usr/local
sudo tar xvf /usr/local/$nccl_file -C /usr/local/
sudo mv /usr/local/$(basename $nccl_file .txz) /usr/local/nccl-2.3
sudo rm /usr/local/$nccl_file
}
# Install cuda, cudnn and nccl, eg everything we need to make tensorflow run
install_cuda() {
if [ -d /usr/local/cuda-$CUDA_VERSION ]; then
echo "Cuda $CUDA_VERSION already installed"
else
sudo apt-get update && sudo apt-get install cuda-$CUDA_VERSION
fi
sudo apt-get update
sudo apt-get install libcudnn7=$CUDNN_VERSION libcudnn7-dev=$CUDNN_VERSION
# libnccl2=$NCCL_VERSION libnccl-dev=$NCCL_VERSION
install_nccl
}
install_nvidia_ml_repo
install_cuda
#!/bin/sh
set -e # exit script when one command fails
set -u # unset variables will cause failure
install_dockerce(){
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable"
sudo apt-get update && sudo apt-get install -y docker-ce
}
install_nvidia(){
install_dockerce
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | \
sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update
sudo apt-get install -y nvidia-docker2
sudo pkill -SIGHUP dockerd
}
nvtest() {
docker run --runtime=nvidia --rm nvidia/cuda:9.0-base nvidia-smi
}
# Test nvidia-smi with the latest official CUDA image
case $1 in
install)
install_nvidia
;;
test)
nvtest
;;
*)
echo "Unknown argument $1. Valid options are: install, test."
;;
esac
#!/bin/sh
TENSORFLOW_ENV=tensorflow_p36
CONDADIR=$HOME/miniconda
# CONDA Installation functions
# install conda on the current user
install_conda() {
if [ -d $CONDADIR ]; then
echo "Conda already installed in $CONDADIR"
return
fi
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
bash ~/miniconda.sh -b -p $CONDADIR
# create tf environment
$CONDADIR/conda create -y -n $TENSORFLOW_ENV python=3.6
}
CONDA_BASHCONF=$HOME/.bashrc
conda_bash() {
if grep "$CONDADIR" $CONDA_BASHCONF; then
echo "Conda already in bashrc."
return
fi
echo "export PATH=\"$CONDADIR/bin:\$PATH\"" >> $CONDA_BASHCONF
}
# configure fish shell for conda use
CONDA_FISHCONF=$HOME/.config/fish/conf.d/conda.fish
conda_fish() {
if [ -e $CONDA_FISHCONF ]; then
echo "Conda fish config already exists in $CONDA_FISHCONF"
return
fi
echo "source $CONDADIR/etc/fish/conf.d/conda.fish\nconda activate $TENSORFLOW_ENV" > $CONDA_FISHCONF
}
# Tensorflow Installation functions
BAZELVER=0.17.2
install_bazel() {
if command -v bazel > /dev/null; then
echo "Bazel already installed."
return
fi
if ! [ -e bazel-installer.sh ]; then
wget https://github.com/bazelbuild/bazel/releases/download/$BAZELVER/bazel-$BAZELVER-installer-linux-x86_64.sh -O bazel-installer.sh
fi
# install bazel to user
bash bazel-installer.sh --user
}
TENSORFLOW_VERSION="r1.11"
build_tensorflow() {
pip install -U pip six numpy wheel mock
pip install -U keras_applications==1.0.5 --no-deps
pip install -U keras_preprocessing==1.0.3 --no-deps
if ! [ -d tensorflow ]; then
git clone https://github.com/tensorflow/tensorflow.git
fi
cd tensorflow
git checkout $TENSORFLOW_VERSION && git pull
# Configure if not configured yet
if ! [ -e .tf_configure.bazelrc ]; then
./configure
fi
# bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
pip install /tmp/tensorflow_pkg/*.whl
}
# install_conda
# conda_bash
# conda_fish
# install_bazel
build_tensorflow
@xiamaz
Copy link
Author

xiamaz commented Oct 2, 2018

Updated to include more scripts containg almost all steps required to build a native tensorflow package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment