How to run a Jupyter notebook on a remote server — Complete Guide [2022]

Sihan A
4 min readMay 5, 2022

Setting up an environment is hard and time-consuming, especially on a remote server. We will learn how to solve this problem in this article.

Photo by Gabriel Heinzer on Unsplash

1. Two parts

  1. 🛠 Setting up: We will set up and understand some important tools before we start working on the server.
  2. 🌼 Workflow: This step is important. We will go through a complete workflow so that you can understand how to do it by yourself.

2. Setting up

  1. Preparation
  2. SSH: Connect the PC and the remote server
  3. Screen: A terminal multiplexer
  4. Conda: virtual environment
  5. Jupyter notebook

2.1 🍱 Preparation

  • Make sure that you have an account on the remote server. If you do not have one, please kindly ask the server manager to create an account for you.
  • SSH (Secure Shell Protocol) is installed.

2.2 🌯 SSH

Let’s log into the server with the below command.

ssh user_name@ip_address
  • user_name: the account name on the server
  • ip_address: IP address of the server

2.3 🍔 screen

GNU Screen is a terminal multiplexer, and we can check the detail here. The main reason we use Screen is that we still want the notebook to run after we close the terminal window. It is quite easy to use. I use the below commands daily.

Screen Common Commands

2.4 🍲 Conda

2.4.1 Installation

  • Download

Official installation guide: [open the link]→[1. Download the installer]→[Miniconda installer for Linux]→[Choose a version and copy the link address]

wget <paste-the-link>
  • Run the installation
bash Miniconda3-latest-Linux-x86_64.sh

2.4.2 🥵 conda command not found

If there is an error conda command not found, we need to add conda to the environment path. Run the below code.

export PATH="$HOME/miniconda3/bin:$PATH"

2.4.3 conda-forge

We can have a brief understanding of conda-forge here. If you need it, please run the below codes line by line.

conda --version
conda update conda
conda config --add channels conda-forge
conda config --set channel_priority strict

2.5 🥪 Jupyter

2.5.1 Installation of Jupyter Lab and Jupyter Notebook

pip install jupyterlab
pip install notebook

2.5.2 Set a password

To safely use the Jupyter notebook, instead of using a token, a password would be more convenient. Let’s set it.

jupyter notebook --generate-config
jupyter notebook password

2.5.3 Jupyter Notebook Extensions

This is optional, although it will help increase our work efficiency. If you’re interested, check this link.

3. Workflow: A complete and hands-on practice

  1. Connect to the lab VPN.
  2. Open a terminal on your PC and log in to the remote server with SSH.

3. Create a screen session

Here, we created a new session called note-8022. It has two pieces of information. It will be ① used for the Jupyter notebook environment and ② use the 8022 port of the server.

4. Run a Jupyter notebook

Guide link: Running Jupyter Notebook on a remote server

jupyter notebook --no-browser --port=<PORT_1>
Run a Jupyter notebook with port 8022 without opening the browser.

Then, it is ok to close down the terminal window.
If we want to go back to the session:

screen -r note-8022

5. Open a new terminal and type:

ssh -L <PORT_1>:localhost:<PORT_2> <USER>@<REMOTE_HOST>

Press Enter and type the password.
✲ Remember to keep this terminal open while we are using the Jupyter notebook. If we close down this terminal window, the notebook will be inaccessible.

6. Open a browser and navigate to the below link.

http://localhost:<PORT_1>

7. Input the password

Bingo! Let’s start happy coding. Next time we want a notebook environment, we just need to follow steps 5–7.

In this tutorial, we set up all the requirements to run a remote Jupyter notebook and go through all steps in a real-world scenario.

If this article is helpful, please click clap and follow me. 😉

--

--