-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_conda_jupyterhub.sh
More file actions
executable file
·143 lines (114 loc) · 4.62 KB
/
install_conda_jupyterhub.sh
File metadata and controls
executable file
·143 lines (114 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env bash
set -euo pipefail
PKG_DIR="$(cd "$(dirname "$0")" && pwd)"
MINICONDA_DIR="$HOME/miniconda3"
DOWNLOAD_URL="https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh"
INSTALLER="$PKG_DIR/Miniconda3-latest-Linux-x86_64.sh"
echo "[1/5] Downloading Miniconda installer..."
if [ ! -f "$INSTALLER" ]; then
echo "Downloading from $DOWNLOAD_URL..."
curl -L "$DOWNLOAD_URL" -o "$INSTALLER"
chmod +x "$INSTALLER"
else
echo "Miniconda installer already present, skipping download"
fi
echo "[2/5] Installing Miniconda..."
if [ ! -d "$MINICONDA_DIR" ]; then
bash "$INSTALLER" -b -p "$MINICONDA_DIR"
else
echo "Miniconda already present at $MINICONDA_DIR, skipping install"
fi
# Initialize conda for this shell
source "$MINICONDA_DIR/etc/profile.d/conda.sh"
echo "[3/5] Updating conda base configuration..."
conda config --set always_yes yes --set changeps1 no
echo "[3.5/5] Accepting conda Terms of Service..."
# Accept main channel
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main || echo "Warning: Could not accept main channel ToS"
# Accept r channel
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r || echo "Warning: Could not accept r channel ToS"
# Try conda-forge channel (may fail due to network issues)
conda tos accept --override-channels --channel https://repo.conda-forge || echo "Warning: Could not accept conda-forge channel ToS, will try alternative installation method"
echo "[4/5] Installing JupyterHub..."
# Try conda-forge first, fallback to main channel if network issues
if conda install -c conda-forge jupyterhub; then
echo "JupyterHub installed from conda-forge channel"
else
echo "conda-forge failed, trying main channel..."
conda install jupyterhub
fi
echo "[5/5] Installing JupyterLab and Notebook..."
conda install jupyterlab
conda install notebook
# Clean up downloaded installer
rm -f "$INSTALLER"
echo "Done. You can run JupyterHub with: jupyterhub -f ~/jupyterhub_config.py"
echo ""
# Automatically activate conda environment for current shell
echo "Activating conda environment for current shell..."
# Set PATH and activate base environment
export PATH="$MINICONDA_DIR/bin:$PATH"
# Manually set conda environment variables
export CONDA_DEFAULT_ENV="base"
export CONDA_PREFIX="$MINICONDA_DIR"
export CONDA_SHLVL=1
# Ensure we're in the user's home directory
cd "$HOME"
# Standard conda initialization
echo "Initializing conda for bash..."
"$MINICONDA_DIR/bin/conda" init bash
# Source .bashrc to activate the environment
echo "Activating conda environment..."
source ~/.bashrc
# Verify activation
if command -v conda >/dev/null 2>&1; then
echo "✅ Conda environment activated successfully!"
echo "Current conda version: $(conda --version)"
echo "Current conda environment: $CONDA_DEFAULT_ENV"
echo "Conda prefix: $CONDA_PREFIX"
else
echo "❌ Failed to activate conda environment"
fi
echo ""
echo "[6/6] Configuring JupyterHub for system authentication..."
# Fix unix_chkpwd permissions
echo "Fixing unix_chkpwd permissions..."
sudo chmod 4755 /usr/sbin/unix_chkpwd
# Create PAM configuration for JupyterHub
echo "Creating PAM configuration..."
sudo tee /etc/pam.d/jupyterhub >/dev/null <<'PAM'
auth include common-auth
account include common-account
password include common-password
session include common-session
# 可选:记录 loginuid,不依赖 pam_lastlog
session optional pam_loginuid.so
PAM
# Generate JupyterHub config
echo "Generating JupyterHub configuration..."
jupyterhub --generate-config
# Configure JupyterHub for PAM authentication
echo "Configuring JupyterHub for PAM authentication..."
cat >> jupyterhub_config.py << 'CONFIG'
# PAM Authentication Configuration
c.JupyterHub.authenticator_class = 'jupyterhub.auth.PAMAuthenticator'
c.LocalAuthenticator.create_system_users = True
c.Authenticator.allow_all = True
c.Authenticator.admin_users = {'ubuntu'}
c.PAMAuthenticator.service = 'jupyterhub'
# Set spawner command to use conda environment
c.Spawner.cmd = ['/home/ubuntu/miniconda3/bin/jupyterhub-singleuser']
CONFIG
# Set proper permissions for JupyterHub access
echo "Setting proper permissions..."
sudo chmod o+x /home/ubuntu
sudo chmod o+rx /home/ubuntu/miniconda3
sudo chmod o+rx /home/ubuntu/miniconda3/bin
sudo chmod 755 /home/ubuntu/miniconda3/bin/jupyterhub-singleuser
echo "✅ JupyterHub configuration completed!"
echo ""
echo "IMPORTANT: You can now start JupyterHub with:"
echo "sudo -E env PATH=\"\$PATH\" jupyterhub -f ~/jupyterhub_config.py"
echo ""
echo "Note: To activate conda in new terminals, add this to your ~/.bashrc:"
echo "echo 'source ~/miniconda3/etc/profile.d/conda.sh' >> ~/.bashrc"