one-click-installation-script/jupyter.sh

154 lines
5.6 KiB
Bash
Raw Normal View History

2022-12-18 20:00:15 +08:00
#!/usr/bin/env bash
#by spiritlhl
#from https://github.com/spiritLHLS/one-click-installation-script
#version: 2022.12.18
2022-12-18 22:14:19 +08:00
source ~/.bashrc
2022-12-18 20:00:15 +08:00
red(){ echo -e "\033[31m\033[01m$1$2\033[0m"; }
green(){ echo -e "\033[32m\033[01m$1$2\033[0m"; }
yellow(){ echo -e "\033[33m\033[01m$1$2\033[0m"; }
reading(){ read -rp "$(green "$1")" "$2"; }
ver="2022.12.18"
changeLog="一键安装jupyter环境"
clear
echo "#######################################################################"
2022-12-18 20:15:50 +08:00
echo "# ${YELLOW}一键安装jupyter环境${PLAIN} #"
2022-12-18 20:00:15 +08:00
echo "# 版本:$ver #"
2022-12-18 20:15:50 +08:00
echo "# 更新日志:$changeLog #"
2022-12-18 20:00:15 +08:00
echo "# ${GREEN}作者${PLAIN}: spiritlhl #"
echo "# ${GREEN}作仓库${PLAIN}: https://github.com/spiritLHLS/one-click-installation-script #"
echo "#######################################################################"
echo "支持系统Ubuntu 18+Debian 8+centos 7+FedoraAlmalinux 8.5+"
2022-12-18 23:23:19 +08:00
echo "执行脚本,之前有安装过则打印设置的登陆信息,没安装过则进行安装再打印信息"
2022-12-18 21:44:57 +08:00
echo "如果是初次安装无脑输入yes或y回车即可"
2022-12-18 20:00:15 +08:00
install_jupyter() {
2022-12-18 21:33:28 +08:00
rm -rf Miniconda3-latest-Linux-x86_64.sh*
# Check if conda is already installed
if ! command -v conda &> /dev/null; then
2022-12-18 22:14:19 +08:00
# Install conda
2022-12-18 21:33:28 +08:00
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
2022-12-18 22:14:19 +08:00
bash Miniconda3-latest-Linux-x86_64.sh -b -u
# added by Miniconda3 installer
2022-12-18 21:33:28 +08:00
echo 'export PATH="$PATH:$HOME/miniconda3/bin:$HOME/miniconda3/condabin"' >> ~/.bashrc
echo 'export PATH="$PATH:$HOME/.local/share/jupyter"' >> ~/.bashrc
source ~/.bashrc
2022-12-18 22:14:19 +08:00
sleep 1
echo 'export PATH="/home/user/miniconda3/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
sleep 1
# Add the necessary paths to your search path
export PATH="/home/user/miniconda3/bin:$PATH"
2022-12-18 22:29:36 +08:00
green "请关闭本窗口开一个新窗口再执行本脚本,否则无法加载一些预设的环境变量" && exit 0
2022-12-18 20:00:15 +08:00
fi
2022-12-18 22:28:38 +08:00
green "加载预设的conda环境变量成功准备安装jupyter无脑输入y和回车即可"
2022-12-18 21:33:28 +08:00
# Create a new conda environment and install jupyter
conda create -n jupyter-env python=3
source activate jupyter-env
conda install jupyter jupyterlab
2022-12-18 22:28:38 +08:00
2022-12-18 21:44:57 +08:00
# Add the following line to /etc/profile
echo 'export PATH="$PATH:~/.local/share/jupyter"' >> /etc/profile
# Execute the configuration
source /etc/profile
2022-12-18 22:28:38 +08:00
# Set username and password for Jupyter Server
2022-12-18 22:40:54 +08:00
# jupyter notebook --generate-config
# cp ~/.jupyter/jupyter_notebook_config.py ~/.jupyter/jupyter_server_config.py
jupyter server --generate-config
# echo "c.ServerApp.password = 'spiritlhl'" >> ~/.jupyter/jupyter_server_config.py
# echo "c.ServerApp.username = 'spiritlhl'" >> ~/.jupyter/jupyter_server_config.py
2022-12-18 20:00:15 +08:00
# Open port 13692 in firewall
if command -v ufw &> /dev/null; then
sudo ufw allow 13692/tcp
elif command -v firewall-cmd &> /dev/null; then
sudo firewall-cmd --add-port=13692/tcp --permanent
sudo firewall-cmd --reload
fi
2022-12-18 22:28:38 +08:00
# Start Jupyter Server with port 13692 and host 0.0.0.0
nohup jupyter lab --port 13692 --no-browser --ip=0.0.0.0 --allow-root & echo $!
2022-12-18 22:40:54 +08:00
sleep 5
cat nohup.out
2022-12-18 23:23:19 +08:00
# Add the specified paths to the PATH variable
paths="./miniconda3/envs/jupyter-env/etc/jupyter:./miniconda3/envs/jupyter-env/bin/jupyter:./miniconda3/envs/jupyter-env/share/jupyter"
export PATH="$paths:$PATH"
2022-12-18 20:00:15 +08:00
2022-12-18 23:23:19 +08:00
# Remove duplicate paths from the PATH variable
new_path=$(echo "$PATH" | tr ':' '\n' | awk '!x[$0]++' | tr '\n' ':')
export PATH="$new_path"
2022-12-18 20:00:15 +08:00
2022-12-18 23:23:19 +08:00
# Refresh the current shell
source ~/.bashrc
green "已安装jupyter lab的web端到外网端口13692上请打开你的 外网IP:13692"
green "同时已保存日志输出到当前目录的nohup.out中且已打印5秒日志如上"
green "如果需要进一步查询,请关闭本窗口开一个新窗口再执行本脚本,否则无法加载一些预设的环境变量" && exit 0
2022-12-18 20:00:15 +08:00
}
2022-12-18 20:15:50 +08:00
query_jupyter_info() {
2022-12-18 23:23:19 +08:00
source activate jupyter-env
2022-12-18 20:15:50 +08:00
# Check if jupyter is installed
2022-12-18 23:23:19 +08:00
if ! jupyter --version &> /dev/null; then
2022-12-18 20:15:50 +08:00
echo "Error: Jupyter is not installed on this system."
return 1
fi
2022-12-18 23:23:19 +08:00
# Find jupyter config directory
config_dir=$(jupyter --config-dir)
config_path="$config_dir/jupyter_server_config.py"
# Check if jupyter_server_config.py exists
if [ ! -f "$config_path" ]; then
echo "Error: jupyter_server_config.py not found."
2022-12-18 20:15:50 +08:00
return 1
fi
2022-12-18 23:23:19 +08:00
# Read jupyter_server_config.py
config=$(cat "$config_path")
2022-12-18 20:15:50 +08:00
2022-12-18 23:23:19 +08:00
# Extract token
# token=$(echo "$config" | grep "c.ServerApp.token" | awk -F "=" '{print $2}' | tr -d ' ')
token=$(echo "$config" | grep "c.IdentityProvider.token" | awk -F "=" '{print $2}' | tr -d ' ')
if [ -z "$token" ]; then
echo "Error: Token not found in jupyter_server_config.py."
return 1
2022-12-18 20:15:50 +08:00
fi
2022-12-18 23:23:19 +08:00
echo "Token: $token"
2022-12-18 20:15:50 +08:00
# Extract port
2022-12-18 23:23:19 +08:00
port=$(echo "$config" | grep "c.ServerApp.port" | awk -F "=" '{print $2}' | tr -d ' ')
2022-12-18 20:15:50 +08:00
echo "Port: $port"
2022-12-18 21:33:28 +08:00
}
2022-12-18 20:00:15 +08:00
main() {
2022-12-18 23:23:19 +08:00
source activate jupyter-env
2022-12-18 20:00:15 +08:00
# Check if jupyter is installed
2022-12-18 20:15:50 +08:00
if jupyter --version &> /dev/null; then
2022-12-18 23:23:19 +08:00
green "Jupyter is already installed on this system."
2022-12-18 20:00:15 +08:00
else
2022-12-18 20:18:37 +08:00
reading "Jupyter is not installed on this system. Do you want to install it? (y/n) " confirminstall
2022-12-18 20:00:15 +08:00
echo ""
# Check user's input and exit if they do not want to proceed
2022-12-18 20:18:37 +08:00
if [ "$confirminstall" != "y" ]; then
2022-12-18 20:00:15 +08:00
exit 0
fi
2022-12-18 20:18:37 +08:00
install_jupyter
2022-12-18 20:00:15 +08:00
fi
2022-12-18 20:18:37 +08:00
2022-12-18 20:15:50 +08:00
# Print the current info for Jupyter
echo "The current info for Jupyter:"
query_jupyter_info
2022-12-18 20:00:15 +08:00
}
main