如何通过macOS终端借助大学远程服务器运行Jupyter Notebook以解决本地内存不足问题?
Absolutely, offloading your Jupyter Notebooks to your university's remote server is exactly the fix you need for your local memory crunch. I’ve set this up dozens of times for similar scenarios—here’s a straightforward, step-by-step walkthrough using your macOS terminal:
Step 1: SSH into Your Remote Server
First, connect to the university server like you normally do. Open your terminal and run:
ssh your_university_username@remote_server_hostname_or_ip
If your university uses SSH keys for authentication (which is common), use this instead (replace the path with your private key’s location):
ssh -i ~/.ssh/your_university_key.pem your_username@remote_server_address
Enter your password or passphrase when prompted, and you’ll be logged into the remote server.
Step 2: Ensure Jupyter is Installed on the Server
Most university servers have Jupyter pre-installed, but if not, install it using pip (or conda if you use a conda environment):
# Using pip (installs to your user directory) pip install jupyter --user # Or using conda (activate your environment first) conda activate your_environment_name conda install jupyter
Step 3: Start Jupyter on the Server (Headless Mode)
Since the remote server doesn’t have a graphical interface, start Jupyter without launching a browser, and specify a port (8888 is standard, but use any unused port like 8889 if needed):
jupyter notebook --no-browser --port=8888
When it starts, you’ll see output with a URL containing a token (e.g., http://localhost:8888/?token=abc123...). Copy that token—you’ll need it to log in from your local browser later.
Step 4: Set Up Local Port Forwarding
Open a new terminal window on your Mac (don’t close the SSH-connected one) and run this command to forward traffic from your local machine to the remote server’s Jupyter port:
ssh -N -L 8888:localhost:8888 your_username@remote_server_address
-N: Keeps SSH focused on maintaining the tunnel (no remote commands executed)-L: Maps your local port 8888 to the remote server’s port 8888
Leave this terminal running—closing it will break the connection to your remote Jupyter session.
Step 5: Access Jupyter from Your Local Browser
Open any browser (Safari, Chrome, etc.) and navigate to:
http://localhost:8888
Paste the token you copied earlier into the login prompt, and you’ll see your full Jupyter interface—all running on the remote server, using its memory instead of your local machine’s!
Pro Tips for Better Workflow
- Keep sessions alive after disconnecting: Use
screenortmuxon the remote server to preserve your Jupyter session even if you close the SSH terminal. Example:# Start a named screen session screen -S jupyter_session # Launch Jupyter as usual jupyter notebook --no-browser --port=8888 # Press Ctrl+A, then D to detach from the session # Reconnect later with: screen -r jupyter_session - Monitor server resources: Use
htoportopon the remote server to check memory/CPU usage, so you know how many Notebooks you can run comfortably. - Switch ports if needed: If 8888 is occupied, just pick a different port (e.g., 8889) and update both the Jupyter startup command and port forwarding command to match.
内容的提问来源于stack exchange,提问作者Mahesh

