Access remote code in a breeze with JupyterLab via SSH | by Chiawei Lim | Towards Data Science

During our daily development work, there’s a need for us to access a remote system to
co-develop with peers in a shared workplace
train deep learning models on a powerful machine
deploy some scripts in production
The remote server might be referring to the public cloud, the private data center of an organization, a personal workstation, or others.
There is one common attribute with these remote servers — it is located in a fixed location, often with top-notch security and a good cooling system. Fundamentally, since the server is located remotely, the user would need to access the server via SSH or other UI-based remote desktop application.
UI-based remote desktop applications such as Anydesk, Teamviewer might not be the preferred option due to a few reasons
These applications might not be free for long term use
The need to have the application always running in the background
The remote server is headless
Increase of networking latency with the inclusion of graphical interface
Hence, the majority opt for SSH as a secured method to access a remote system. With SSH work solely works in the terminal, at times it might be difficult to edit configurations or code with just vim/vi.
Here’s where forwarding the web-based IDE — JupyterLab to the local system comes into handy.
Walkthrough of the workflow
Run JupyterLab in the background
Run JupyterLab as a root user
But I’m using Jupyter Notebook
To allow the running of JupyterLab in sync with the remote system, there are two main steps
Initiate the running of JupyterLab without interface.Forward remote port to local port
Initiate the running of JupyterLab without interface.
Forward remote port to local port
Step 1: Initiate the running of JupyterLab without an interface
Ssh into the remote system in the form of
ssh <remote-user>@<remote-host>
With JupyterLab installed, run the following command
jupyter lab --no-browser --port <port-number>
To forward the port, open another terminal and run the following command
ssh -N -L localhost:<local-port>:localhost:<remote-port> <remote-user>@<remote-host>
-N: Used for port forwarding. This suppresses the execution of a remote command.
-L: Bind remote port to local port.
Go ahead and open http://localhost:<local-port>/ in the browser.
Enter the token key by copying from the terminal window from step 1.
There are three important points to take notes of:
In the current setup, the terminal running JupyterLab in the remote system has to be kept open. Alternatively, check out Run JupyterLab in the background, allowing the terminal to be closed without killing the process.
Likewise, the terminal forwarding the port has to be kept open during the whole operation.
The port forwarding terminal will not generate any output message to prompt that operation was successful (due to the flag -N being used).
There is an option to run JupyterLab in the background and allow the terminal to be closed without killing the JupyterLab process.
Simply add an ampersand & at the end of the command.
jupyter lab --no-browser --port <port-number> &
To kill the process, use the following command to identify the process
ps -ef | grep python | sort
Kill the process with
kill -9 <process-id>
Running JupyterLab as a root user is not recommended.
However, the workaround for it is by adding a flag to explicitly allow running in the root user mode.
jupyter lab --no-browser --port <port-number> --allow-root
There is still a vast community that opts for Jupyter Notebook as their go-to IDE. Fear not. All of the commands introduced in this article are compatible with Jupyter Notebook too.
Simply swap the command of JupyterLab into Jupyter Notebook.
Example:
jupyter notebook --no-browser --port <port-number>
The installation of the JupyterLab package also allows the running of the Jupyter Notebook too!
Till next time!