one-click-installation-script/install_scripts/jupyter.sh

329 lines
14 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
2023-09-26 13:52:13 +08:00
#version: 2023.09.26
2023-04-27 20:48:25 +08:00
2023-04-27 20:47:24 +08:00
utf8_locale=$(locale -a 2>/dev/null | grep -i -m 1 -E "UTF-8|utf8")
if [[ -z "$utf8_locale" ]]; then
2023-09-26 13:52:13 +08:00
echo "No UTF-8 locale found"
2023-04-27 20:47:24 +08:00
else
2023-09-26 13:52:13 +08:00
export LC_ALL="$utf8_locale"
export LANG="$utf8_locale"
export LANGUAGE="$utf8_locale"
echo "Locale set to $utf8_locale"
2023-04-27 20:47:24 +08:00
fi
2023-05-14 09:02:34 +08:00
cd /root >/dev/null 2>&1
2023-07-26 09:29:09 +08:00
ver="2023.07.26"
2022-12-19 11:45:38 +08:00
changeLog="一键安装jupyter环境"
2022-12-18 22:14:19 +08:00
source ~/.bashrc
2023-09-26 13:52:13 +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"; }
2023-07-26 16:59:16 +08:00
temp_file_apt_fix="apt_fix.txt"
REGEX=("debian|astra" "ubuntu" "centos|red hat|kernel|oracle linux|alma|rocky" "'amazon linux'" "fedora" "arch" "freebsd")
RELEASE=("Debian" "Ubuntu" "CentOS" "CentOS" "Fedora" "Arch" "FreeBSD")
PACKAGE_UPDATE=("! apt-get update && apt-get --fix-broken install -y && apt-get update" "apt-get update" "yum -y update" "yum -y update" "yum -y update" "pacman -Sy" "pkg update")
PACKAGE_INSTALL=("apt-get -y install" "apt-get -y install" "yum -y install" "yum -y install" "yum -y install" "pacman -Sy --noconfirm --needed" "pkg install -y")
PACKAGE_REMOVE=("apt-get -y remove" "apt-get -y remove" "yum -y remove" "yum -y remove" "yum -y remove" "pacman -Rsc --noconfirm" "pkg delete")
PACKAGE_UNINSTALL=("apt-get -y autoremove" "apt-get -y autoremove" "yum -y autoremove" "yum -y autoremove" "yum -y autoremove" "" "pkg autoremove")
2023-09-26 13:52:13 +08:00
CMD=("$(grep -i pretty_name /etc/os-release 2>/dev/null | cut -d \" -f2)" "$(hostnamectl 2>/dev/null | grep -i system | cut -d : -f2)" "$(lsb_release -sd 2>/dev/null)" "$(grep -i description /etc/lsb-release 2>/dev/null | cut -d \" -f2)" "$(grep . /etc/redhat-release 2>/dev/null)" "$(grep . /etc/issue 2>/dev/null | cut -d \\ -f1 | sed '/^[ ]*$/d')" "$(grep -i pretty_name /etc/os-release 2>/dev/null | cut -d \" -f2)" "$(uname -s)")
2023-07-26 16:59:16 +08:00
SYS="${CMD[0]}"
[[ -n $SYS ]] || exit 1
for ((int = 0; int < ${#REGEX[@]}; int++)); do
if [[ $(echo "$SYS" | tr '[:upper:]' '[:lower:]') =~ ${REGEX[int]} ]]; then
SYSTEM="${RELEASE[int]}"
[[ -n $SYSTEM ]] && break
fi
done
2022-12-18 20:00:15 +08:00
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 #"
2023-04-01 18:32:46 +08:00
echo "# ${GREEN}仓库${PLAIN}: https://github.com/spiritLHLS/one-click-installation-script #"
2022-12-18 20:00:15 +08:00
echo "#######################################################################"
2022-12-19 11:44:16 +08:00
echo "验证已支持的系统:"
2023-09-26 13:52:13 +08:00
echo "Ubuntu 系 - 推荐,脚本自动挂起到后台"
echo "Debian 系 - 部分可能需要手动挂起到后台,详看脚本运行安装完毕的后续提示"
2022-12-19 10:30:18 +08:00
echo "可能支持的系统centos 7+FedoraAlmalinux 8.5+"
2022-12-19 11:36:25 +08:00
red "本脚本尝试使用Miniconda3安装虚拟环境jupyter-env再进行jupyter和jupyterlab的安装如若安装机器不纯净勿要轻易使用本脚本"
2022-12-21 22:00:36 +08:00
yellow "执行脚本,之前有用本脚本安装过则直接打印设置的登陆信息,没安装过则进行安装再打印信息,如果已安装但未启动则自动启动后再打印信息"
2023-08-07 18:06:02 +08:00
yellow "如果是初次安装无脑y无脑回车即可按照提示进行操作即可安装完毕将在后台常驻运行自动添加常用的安装包通道源"
2022-12-18 21:44:57 +08:00
2023-09-26 13:52:13 +08:00
check_china() {
2023-07-26 09:29:09 +08:00
yellow "IP area being detected ......"
if [[ -z "${CN}" ]]; then
if [[ $(curl -m 6 -s https://ipapi.co/json | grep 'China') != "" ]]; then
yellow "根据ipapi.co提供的信息当前IP可能在中国"
read -e -r -p "是否选用中国镜像完成相关组件安装? ([y]/n) " input
case $input in
2023-09-26 13:52:13 +08:00
[yY][eE][sS] | [yY])
echo "使用中国镜像"
CN=true
;;
[nN][oO] | [nN])
echo "不使用中国镜像"
;;
*)
echo "使用中国镜像"
CN=true
;;
2023-07-26 09:29:09 +08:00
esac
else
if [[ $? -ne 0 ]]; then
if [[ $(curl -m 6 -s cip.cc) =~ "中国" ]]; then
yellow "根据cip.cc提供的信息当前IP可能在中国"
read -e -r -p "是否选用中国镜像完成相关组件安装? [Y/n] " input
case $input in
2023-09-26 13:52:13 +08:00
[yY][eE][sS] | [yY])
echo "使用中国镜像"
CN=true
;;
[nN][oO] | [nN])
echo "不使用中国镜像"
;;
*)
echo "不使用中国镜像"
;;
2023-07-26 09:29:09 +08:00
esac
fi
fi
fi
fi
}
2023-09-26 13:52:13 +08:00
check_update() {
2023-07-26 16:59:16 +08:00
yellow "Updating package management sources"
2023-09-26 13:52:13 +08:00
if command -v apt-get >/dev/null 2>&1; then
2023-07-26 16:59:16 +08:00
apt_update_output=$(apt-get update 2>&1)
2023-09-26 13:52:13 +08:00
echo "$apt_update_output" >"$temp_file_apt_fix"
2023-07-26 16:59:16 +08:00
if grep -q 'NO_PUBKEY' "$temp_file_apt_fix"; then
public_keys=$(grep -oE 'NO_PUBKEY [0-9A-F]+' "$temp_file_apt_fix" | awk '{ print $2 }')
joined_keys=$(echo "$public_keys" | paste -sd " ")
yellow "No Public Keys: ${joined_keys}"
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ${joined_keys}
apt-get update
if [ $? -eq 0 ]; then
green "Fixed"
fi
fi
rm "$temp_file_apt_fix"
else
${PACKAGE_UPDATE[int]}
fi
rm -rf $temp_file_apt_fix
}
check_sudo() {
yellow "checking sudo"
2023-09-26 13:52:13 +08:00
if ! command -v sudo >/dev/null 2>&1; then
2023-07-26 16:59:16 +08:00
yellow "Installing sudo"
2023-09-26 13:52:13 +08:00
${PACKAGE_INSTALL[int]} sudo >/dev/null 2>&1
2023-07-26 16:59:16 +08:00
fi
}
check_wget() {
2023-09-26 13:52:13 +08:00
if ! which wget >/dev/null; then
2023-07-26 16:59:16 +08:00
yellow "Installing wget"
${PACKAGE_INSTALL[int]} wget
2023-09-26 13:52:13 +08:00
fi
2023-07-26 16:59:16 +08:00
}
check_curl() {
2023-09-26 13:52:13 +08:00
if ! which curl >/dev/null; then
2023-07-26 16:59:16 +08:00
yellow "Installing curl"
${PACKAGE_INSTALL[int]} curl
2023-09-26 13:52:13 +08:00
fi
2023-07-26 16:59:16 +08:00
if [ $? -ne 0 ]; then
2023-09-26 13:52:13 +08:00
apt-get -f install >/dev/null 2>&1
2023-07-26 16:59:16 +08:00
${PACKAGE_INSTALL[int]} curl
fi
}
check_ufw() {
2023-09-26 13:52:13 +08:00
if ! which ufw >/dev/null; then
2023-07-26 16:59:16 +08:00
yellow "Installing ufw"
${PACKAGE_INSTALL[int]} ufw
2023-09-26 13:52:13 +08:00
fi
2023-07-26 16:59:16 +08:00
}
2022-12-18 20:00:15 +08:00
2023-07-26 17:19:12 +08:00
is_private_ipv4() {
local ip_address=$1
local ip_parts
if [[ -z $ip_address ]]; then
return 0 # 输入为空
fi
2023-09-26 13:52:13 +08:00
IFS='.' read -r -a ip_parts <<<"$ip_address"
2023-07-26 17:19:12 +08:00
# 检查IP地址是否符合内网IP地址的范围
# 去除 回环REC 1918多播 地址
if [[ ${ip_parts[0]} -eq 10 ]] ||
2023-09-26 13:52:13 +08:00
[[ ${ip_parts[0]} -eq 172 && ${ip_parts[1]} -ge 16 && ${ip_parts[1]} -le 31 ]] ||
[[ ${ip_parts[0]} -eq 192 && ${ip_parts[1]} -eq 168 ]] ||
[[ ${ip_parts[0]} -eq 127 ]] ||
[[ ${ip_parts[0]} -eq 0 ]] ||
[[ ${ip_parts[0]} -ge 224 ]]; then
return 0 # 是内网IP地址
2023-07-26 17:19:12 +08:00
else
2023-09-26 13:52:13 +08:00
return 1 # 不是内网IP地址
2023-07-26 17:19:12 +08:00
fi
}
2023-09-26 13:52:13 +08:00
check_ipv4() {
2023-07-26 17:19:12 +08:00
IPV4=$(ip -4 addr show | grep global | awk '{print $2}' | cut -d '/' -f1 | head -n 1)
if is_private_ipv4 "$IPV4"; then # 由于是内网IPV4地址需要通过API获取外网地址
IPV4=""
local API_NET=("ipv4.ip.sb" "ipget.net" "ip.ping0.cc" "https://ip4.seeip.org" "https://api.my-ip.io/ip" "https://ipv4.icanhazip.com" "api.ipify.org")
for p in "${API_NET[@]}"; do
response=$(curl -s4m8 "$p")
sleep 1
if [ $? -eq 0 ] && ! echo "$response" | grep -q "error"; then
IP_API="$p"
IPV4="$response"
break
fi
done
fi
export IPV4
}
2022-12-18 20:00:15 +08:00
install_jupyter() {
2023-09-26 13:52:13 +08:00
rm -rf Miniconda3-latest-Linux-x86_64.sh*
check_update
check_sudo
check_wget
check_curl
check_ufw
if ! command -v conda &>/dev/null; then
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b -u
echo 'export PATH="$PATH:$HOME/miniconda3/bin:$HOME/miniconda3/condabin"' >>~/.bashrc
echo 'export PATH="$PATH:$HOME/.local/share/jupyter"' >>~/.bashrc
source ~/.bashrc
sleep 1
echo 'export PATH="/home/user/miniconda3/bin:$PATH"' >>~/.bashrc
source ~/.bashrc
sleep 1
export PATH="/home/user/miniconda3/bin:$PATH"
green "请关闭本窗口开一个新窗口再执行本脚本,否则无法加载一些预设的环境变量(或断开SSH连接后重新连接)" && exit 0
fi
green "加载预设的conda环境变量成功准备安装jupyter无脑输入y和回车即可"
conda create -n jupyter-env python=3
sleep 5
source activate jupyter-env
2022-12-18 22:14:19 +08:00
sleep 1
2023-09-26 13:52:13 +08:00
conda install jupyter jupyterlab
check_china
if [[ -n "${CN}" && "${CN}" == true ]]; then
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
fi
echo 'export PATH="$PATH:~/.local/share/jupyter"' >>/etc/profile
source /etc/profile
# 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
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
ubuntu_version=$(lsb_release -rs)
channels_to_add=(
"dglteam"
"pytorch"
"conda-forge"
"anaconda"
)
2022-12-19 11:18:16 +08:00
source activate jupyter-env
sleep 1
2022-12-21 22:00:36 +08:00
rm -rf nohup.out
2022-12-19 11:18:16 +08:00
green "后台执行的pid的进程ID和输出日志文件名字如下"
2023-09-26 13:52:13 +08:00
nohup jupyter lab --port 13692 --no-browser --ip=0.0.0.0 --allow-root &
green $!
2022-12-19 11:18:16 +08:00
sleep 5
2023-09-26 13:52:13 +08:00
if grep -q 'token=' nohup.out >/dev/null 2>&1; then
cat nohup.out
else
echo "你可能在不支持的系统是执行,运行的最后几行可能有如下提示"
yellow "nohup: failed to run command 'jupyter': No such file or directory"
echo "你需要手动执行下面的命令"
yellow "source activate jupyter-env"
yellow "nohup jupyter lab --port 13692 --no-browser --ip=0.0.0.0 --allow-root"
green "等待5秒后关闭本窗口开新窗口执行下面的命令查看登陆信息"
yellow "cat nohup.out"
2023-09-27 00:03:21 +08:00
green "如若无成功输出,可尝试重新运行本脚本"
2023-09-26 13:52:13 +08:00
fi
current_channels=$(conda config --get channels)
for channel in "${channels_to_add[@]}"; do
if echo "$current_channels" | grep -q "$channel" >/dev/null 2>&1; then
:
else
conda config --add channels "$channel" >/dev/null 2>&1
fi
done
paths="./miniconda3/envs/jupyter-env/etc/jupyter:./miniconda3/envs/jupyter-env/bin/jupyter:./miniconda3/envs/jupyter-env/share/jupyter"
export PATH="$paths:$PATH"
new_path=$(echo "$PATH" | tr ':' '\n' | awk '!x[$0]++' | tr '\n' ':')
export PATH="$new_path"
source ~/.bashrc
check_ipv4
jpyurl="http://${IPV4}:13692/"
green "已安装jupyter lab的web端到外网端口13692上请打开你的 外网IP:13692"
green "如果你是在云服务上运行,那么请打开 ${jpyurl} 如果是在本地安装的,请打开 http://127.0.0.1:13692/"
green "初次安装会要求输入token设置密码token详见上方打印信息或当前目录的nohup.out日志"
green "同时已保存日志输出到当前目录的nohup.out中且已打印5秒日志如上"
green "如果需要进一步查询,请关闭本窗口开一个新窗口再执行本脚本,否则无法加载一些预设的环境变量"
green "如果想要手动查询,输入 source activate jupyter-env && jupyter server list && conda deactivate 即可查询"
exit 0
2022-12-18 20:00:15 +08:00
}
2022-12-18 20:15:50 +08:00
query_jupyter_info() {
2023-09-26 13:52:13 +08:00
source activate jupyter-env >/dev/null 2>&1
if ! jupyter --version &>/dev/null; then
echo "Error: Jupyter is not installed on this system."
return 1
fi
check_ipv4
jpyurl="http://${IPV4}:13692/"
source activate jupyter-env && jupyter server list && conda deactivate
cat nohup.out
green "已查询登陆信息如上"
green "如果你是在云服务上运行,那么请打开 ${jpyurl} 如果是在本地安装的,请打开 http://127.0.0.1:13692/"
green "token详见上方打印信息或当前目录的nohup.out日志"
green "如果想要手动查询,输入 source activate jupyter-env && jupyter server list && conda deactivate 即可查询"
2022-12-18 21:33:28 +08:00
}
2022-12-18 20:00:15 +08:00
main() {
2023-09-26 13:52:13 +08:00
source activate jupyter-env >/dev/null 2>&1
if jupyter --version &>/dev/null; then
green "Jupyter is already installed on this system."
if ! (nc -z localhost 13692) >/dev/null 2>&1; then
source activate jupyter-env
rm -rf nohup.out
green "后台未启动jupyter正在启动"
nohup jupyter lab --port 13692 --no-browser --ip=0.0.0.0 --allow-root &
green $!
sleep 1
jupyter lab
fi
else
reading "Jupyter is not installed on this system. Do you want to install it? (y/n) " confirminstall
echo ""
if [ "$confirminstall" != "y" ]; then
exit 0
fi
install_jupyter
2022-12-18 20:00:15 +08:00
fi
2023-09-26 13:52:13 +08:00
green "The current info for Jupyter:"
query_jupyter_info
2022-12-18 20:00:15 +08:00
}
main