Setting Up Jupyter Lab on Google Colab
This guide will walk you through the steps to set up Jupyter Lab with necessary extensions, GPU support, and expose it using Cloudflare on Google Colab.
Requirements
Google Colab environment
Steps
Step 1: Install Jupyter Lab and Required Packages
First, install Jupyter Lab and other required packages:
!pip install jupyterlab jupyterlab-code-formatter jupyterlab-lsp python-lsp-server[all]
Step 2: Install Cloudflare to Expose the Jupyter Lab Server
!wget -P ~ https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
!dpkg -i ~/cloudflared-linux-amd64.deb
Step 3: Install Jupyter Lab Extensions
Install the language server and code formatter extensions:
!jupyter labextension install @krassowski/jupyterlab-lsp !jupyter labextension install @ryantam626/jupyterlab_code_formatter
Step 4: Enable Jupyter Lab Extensions
Enable the code formatter server extension:
!jupyter serverextension enable --py jupyterlab_code_formatter
Step 5: Install GPU Packages (TensorFlow, PyTorch)
!pip install tensorflow-gpu !pip install torch torchvision torchaudio
Step 6: Generate Jupyter Lab Configuration
Generate the default Jupyter Lab configuration file:
!jupyter lab --generate-config
Step 7: Configure Jupyter Lab
Append the following configuration to your jupyter_lab_config.py:
`config_path = "/root/.jupyter/jupyter_lab_config.py" with open(config_path, "a") as config_file: config_file.write("""
c.ServerApp.port = 8888 c.ServerApp.open_browser = False c.ServerApp.ip = '0.0.0.0' c.ServerApp.allow_remote_access = True c.ServerApp.token = '' # No token authentication c.ServerApp.password = '' # No password authentication c.LspInstallSettings.install_optional_dependencies = True
c.CodeFormatter.formatters = { 'python': ['black'], }
c.NotebookApp.kernel_spec_manager_class = 'nb_conda_kernels.CondaKernelSpecManager'
c.NotebookApp.shutdown_no_activity_timeout = 3600 c.NotebookApp.terminals_enabled = True """)`
Step 8: Start Cloudflare
Set up a tunnel for the Jupyter Lab server:
`import subprocess import threading import time import socket
def iframe_thread(port): while True: time.sleep(0.5) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) result = sock.connect_ex(('127.0.0.1', port)) if result == 0: break sock.close()
print("\nJupyter Lab finished loading, trying to launch cloudflared...\n")
# Start Cloudflare tunnel
p = subprocess.Popen(["cloudflared", "tunnel", "--url", f"http://127.0.0.1:{port}"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for line in p.stderr:
l = line.decode()
if "trycloudflare.com" in l:
print("This is the URL to access Jupyter Lab:", l[l.find("http"):], end='')
threading.Thread(target=iframe_thread, daemon=True, args=(8888,)).start()`
Step 9: Start Jupyter Lab Server
Finally, start the Jupyter Lab server:
!jupyter lab --ip=0.0.0.0 --port=8888 --no-browser --allow-root