Skip to content

Instantly share code, notes, and snippets.

@shr00mie
Last active July 20, 2019 10:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shr00mie/0f45256d99d9144086ab6ae5c2ca93f2 to your computer and use it in GitHub Desktop.
Save shr00mie/0f45256d99d9144086ab6ae5c2ca93f2 to your computer and use it in GitHub Desktop.
Jupyterlab Install + Service on Ubuntu Server 16.04LTS
#!/bin/bash
# variables
# listening address for jupyter. set to localhost if only accessing locally.
read -p $'\n\e[32mServer static IP address\e[m: ' ipAddress
# Set this either as parent or direct working directory of the project you want to work on.
read -p $'\n\e[32mProject Parent Directory\e[m: ' WorkingDirectory
echo -e "\n...\e[32mGenerating random token\e[m...\n"
token=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 35 | head -n 1)
echo -e "\n...\e[32mUpdating system\e[m...\n"
sudo apt update && sudo apt upgrade -y
echo -e "\n...\e[32mAdding Nodejs 9.x repository\e[m...\n"
wget https://gist.githubusercontent.com/shr00mie/8473a7f885316dba7f4ac39ca5b13874/raw/fed1b63626ebee6dbc66bacbe0525e5fec4d1aea/nodejs-9x-install.sh
chmod u+x nodejs-9x-install.sh
sudo ./nodejs-9x-install.sh
echo -e "\n...\e[32mInstalling python3, python3-dev, python3-pip, and nodejs\e[m...\n"
sudo apt install python3 python3-dev python3-pip nodejs -y
echo -e "\n...\e[32mInstalling NVM\e[m...\n"
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
echo -e "\n...\e[32mInstall Cleanup\e[m...\n"
sudo apt autoclean && sudo apt autoremove -y
rm nodejs-9x-install.sh
echo -e "\n...\e[32mUpgrading pip\e[m...\n"
pip3 install -U pip
echo -e "\n...\e[32mInstalling dependencies\e[m...\n"
pip3 install --user autopep8 pylama beautysh websocket-client pymongo mongoengine bintrees requests
echo -e "\n...\e[32mInstalling Jupyter Lab & enabling jupyterlab server extension\e[m...\n"
pip3 install --user jupyterlab
jupyter serverextension enable --py jupyterlab
echo -e "\n...\e[32mAdding update alias to ~/.bashrc\e[m...\n"
cat << EOF | sudo tee -a /home/$USER/.bashrc
# ALIASES
alias update='sudo apt update && sudo apt upgrade -y'
# PATH
export PATH=/home/$USER/.local/bin:$PATH
EOF
echo -e "\n...\e[32mGenerating jupyter config file\e[m...\n"
jupyter-lab --generate-config
echo -e "\n...\e[32mTweaking config file\e[m...\n"
# allow connections from other devices
# listen on static IP
# don't open browser on start
# set port
# set token
# Configuration
# Incoming connection whitelist. tried with IP & CIDR. not sure about ranges. should be comma separated if more than one.
sed -i.back "s/#c.NotebookApp.allow_origin = ''/c.NotebookApp.allow_origin = '10.1.0.0\/24'/" ~/.jupyter/jupyter_notebook_config.py
# Jupyter listening IP. Set to localhost if only planning on using locally.
sed -i "s/#c.NotebookApp.ip = 'localhost'/c.NotebookApp.ip = '$ipAddress'/" ~/.jupyter/jupyter_notebook_config.py
# Whether or not to open browser on jupyter launch. If headless, or server, set to False.
sed -i "s/#c.NotebookApp.open_browser = True/c.NotebookApp.open_browser = False/" ~/.jupyter/jupyter_notebook_config.py
# Listening port. Change as necessary
sed -i "s/#c.NotebookApp.port = 8888/c.NotebookApp.port = 8888/" ~/.jupyter/jupyter_notebook_config.py
# Randomly generated token for access without user/pass
sed -i "s/^#c.NotebookApp.token .*/c.NotebookApp.token = '$token'/" ~/.jupyter/jupyter_notebook_config.py
# Trash Cleanup
sed -i "s/#c.NotebookApp.cookie_secret = b''/#c.NotebookApp.cookie_secret = ''/" ~/.jupyter/jupyter_notebook_config.py
sed -i "s/#c.Session.key = b''/#c.Session.key = ''/" ~/.jupyter/jupyter_notebook_config.py
sed -i "s/#c.NotebookNotary.secret = b''/#c.NotebookNotary.secret = ''/" ~/.jupyter/jupyter_notebook_config.py
echo -e "\n...\e[32mCreating Jupyter Lab service\e[m...\n"
cat << EOF | sudo tee /etc/systemd/system/jupyter-lab.service
[Unit]
Description=Jupyter Lab
[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/home/$USER/.local/bin/jupyter-lab --config=/home/$USER/.jupyter/jupyter_notebook_config.py
WorkingDirectory=$WorkingDirectory
User=$USER
Group=$USER
Restart=always
RestartSec=10
#KillMode=mixed
[Install]
WantedBy=multi-user.target
EOF
echo -e "\n...\e[32mReloading systemctl daemon after creating service entry\e[m...\n"
sudo systemctl daemon-reload
echo -e "\n...\e[32mEnabling Jupyter Lab service\e[m...\n"
sudo systemctl enable jupyter-lab.service
echo -e "\n...\e[32mStarting Jupyter Lab service\e[m...\n"
sudo systemctl start jupyter-lab.service
echo -e "\n...\e[32mChecking status of Jupyter Lab service\e[m...\n"
sudo systemctl status jupyter-lab.service
echo -e "\n...\e[32mAtom Hydrogen gateway configuration\e[m...\n"
echo "[{\"name\":\"Jupiter Lab - Ubuntu Server 16.04\",\"options\":{\"baseUrl\":\"http://$ipAddress:8888\",\"token\":\"$token\"}}]"
echo -e "\n...\e[32mYou should be able to test the installation by opening a browser and opening the below URL\e[m...\n"
echo "http://$ipAddress:8888/?token=$token/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment