Skip to content

Running Jupyter Notebook on Remote Server

Problem Statement

Running Jupyter notebooks on a local machine with limited memory can be challenging, especially when working with large datasets or multiple notebooks simultaneously. Many developers and researchers need to leverage remote servers with greater computational resources while maintaining the convenience of Jupyter's interactive interface from their local machine.

The core challenge involves establishing a secure connection between your local browser and the Jupyter notebook instance running on a remote server, ensuring you can work seamlessly without transferring large datasets to your local machine.

The most reliable and secure method involves using SSH tunneling to forward the remote Jupyter notebook to your local machine.

Step-by-Step Implementation

  1. Connect to the remote server via SSH:
bash
ssh your_username@remote_server_ip
  1. Start Jupyter notebook on the remote server:
bash
jupyter notebook --no-browser --port=8888
  1. Set up SSH tunnel from your local machine:

Open a new terminal window on your local machine and run:

bash
ssh -N -L 8888:localhost:8888 your_username@remote_server_ip
  1. Access the notebook in your browser:

Navigate to http://localhost:8888 in your local web browser.

TIP

You may need to copy and paste the token from the remote server's terminal output to authenticate.

Advanced SSH Tunnel Options

For more control over the SSH tunnel, use these additional flags:

bash
ssh -N -f -L localhost:8888:localhost:8888 your_username@remote_server_ip
  • -N: Don't execute a remote command
  • -f: Run in background
  • -L: Set up local port forwarding

Alternative Methods

Direct IP Binding Method

If you prefer not to use SSH tunneling, you can configure Jupyter to accept external connections:

  1. On the remote server:
bash
jupyter notebook --no-browser --ip=0.0.0.0 --port=8888
  1. Access directly via browser:

Navigate to http://remote_server_ip:8888 and use the provided token for authentication.

Security Consideration

This method exposes your notebook to the network. Only use this approach in trusted environments and consider additional security measures like passwords or firewalls.

Environment Variables for Permission Issues

If you encounter permission problems, set these environment variables before starting Jupyter:

bash
export JUPYTER_CONFIG_DIR=~/.jupyter
export JUPYTER_DATA_DIR=~/.local/share/jupyter
export JUPYTER_RUNTIME_DIR=~/.local/share/jupyter/runtime
jupyter notebook --no-browser --port=8888

Multiple Notebooks Management

To run multiple notebooks simultaneously, use different ports for each instance:

bash
# First notebook
jupyter notebook --no-browser --port=8888

# Second notebook (in a different terminal session)
jupyter notebook --no-browser --port=8889

Set up corresponding SSH tunnels for each:

bash
# First tunnel
ssh -N -L 8888:localhost:8888 username@server_ip

# Second tunnel
ssh -N -L 8889:localhost:8889 username@server_ip

SSHuttle for Multiple Connections

For extensive work with multiple notebooks, consider using sshuttle to create a VPN-like connection:

bash
# Install sshuttle
pip install sshuttle

# Create SSH VPN
sshuttle --dns -NHr username@server_ip 0/0

This approach eliminates the need for multiple individual tunnels.

Terminating Connections

Properly close your connections when finished:

  1. Stop the Jupyter notebook: Press Ctrl+C in the remote terminal and confirm with y
  2. Close SSH tunnels: Find and kill the process using the port:
bash
# Linux
sudo netstat -lpn | grep :8888

# macOS
sudo netstat -anv | grep 8888

# Then kill the process
kill PROCESS_ID

Best Practices

  1. Use virtual environments on the remote server to manage dependencies
  2. Regularly save your work to avoid losing progress
  3. Monitor resource usage on the remote server
  4. Use strong passwords or token authentication
  5. Consider using screen or tmux on the remote server to keep notebooks running after disconnecting

This approach allows you to leverage powerful remote server resources while maintaining the intuitive Jupyter interface on your local machine, significantly improving performance for memory-intensive tasks.