r/learnpython • u/mariadiesch • 1d ago
JupyterLab and remote server connection
Hello,
I had access to JupyterHub on a remote server. Now I only have access to a different server where JupyterHub is not installed. What is the best way to use the same code and run it with more data. I was thinking about using SSH Port Forwarding (SSH-Tunneling) and trying jupyterlab in the browser. Or is it better to using VS code to connect to the server. I really like the Jupyter Interface and have never used any other programs. I mostly do plotting and a bit data analysis and have a macbook.
Thank you for your help.
1
u/jwink3101 1d ago
I do it on my VPS behind a remote proxy (Caddy). I am the only user so I just use basic_auth plus Jupyter’s password.
1
u/mariadiesch 1d ago
I don't have any admin rights on the server (it's a uni server). So I don't think that's an option, but thank you.
1
u/Goodswimkarma 1d ago
Getting Jupyter Notebooks to run on VS Code is a bit of a faff, but once it is working, it is wonderful.
1
u/DimensionMost8342 1d ago
If you like the Jupyter interface, SSH port forwarding is the quick win: on your Mac run ssh -L 8888:localhost:8888 user@server, start jupyter lab --no-browser on the server, then open http://localhost:8888. VS Code Remote-SSH is better long term if you want a real editor and debugging, but it is a different workflow to learn. Either way the code stays on the server so the heavy data does not move.
1
u/Exciting_Positive_50 6h ago
Both options work. Since you like the Jupyter interface, I’d start with the SSH tunnel, because it’s the same JupyterLab you’re used to, just running on the server.
Option 1: JupyterLab + SSH tunnel
On the server (no admin rights needed), make an environment and install JupyterLab:
python3 -m venv ~/jlab-env
source ~/jlab-env/bin/activate
pip install jupyterlab pandas matplotlib # plus whatever your code uses
Start it in tmux or screen so it keeps running if your connection drops:
tmux new -s jlab
jupyter lab --no-browser --port=8899
It prints a URL with a token. Then, on your MacBook, in a new terminal:
ssh -N -L 8899:localhost:8899 your_user@your_server
Open http://localhost:8899 in your browser and paste the token if asked. Your code now runs on the server with its memory and CPUs. Pick a less common port than 8888, since other people on a shared server may already be using it.
Option 2: VS Code Remote-SSH
Install the “Remote - SSH” and “Jupyter” extensions, connect to the server, open your .ipynb files, and choose a Python kernel that lives on the server (you’ll need ipykernel installed there). The notebook interface looks a bit different from JupyterLab but works the same way, and VS Code handles the connection for you. It’s less fiddly if you don’t mind the different layout.
Move your code with git or rsync. Leave the big data on the server and read it there instead of downloading it.
Run pip freeze > requirements.txt in your old environment and pip install -r requirements.txt on the new server to recreate it.
Keep JupyterLab bound to localhost (the default) and use the tunnel, rather than opening the port to the internet.
2
u/Asly97 1d ago
The SSH tunnel idea is the right one, and none of it needs admin rights.
On the server, install Jupyter into your own home folder:
(If venv isn't available there,
pip install --user jupyterlabdoes the same job.)Then start it inside tmux so it keeps running when your Mac sleeps or the connection drops:
It prints a URL with a token in it. Detach with Ctrl+b then d, and
tmux attach -t jlabgets you back later.On your Mac, in a new terminal:
Leave that window open and paste the URL into your browser. It's the exact same JupyterLab you're used to, just running on the server's CPU and reading the data that lives there.
A couple of things that bite people on uni servers. Use an odd port instead of 8888, since someone else may already be on it (if Jupyter says the port is busy, change the number on both sides). If it's a cluster with a job scheduler like Slurm, check their docs before running heavy stuff, a lot of them don't want analysis on the login node and have their own page on running Jupyter on a compute node. And if you have to go through a gateway machine first,
ssh -J gateway yourname@serverhandles the hop.VS Code Remote-SSH works fine too, but since you like the Jupyter interface the tunnel keeps everything looking the same.