remote_jupyter
Differences
This shows you the differences between two versions of the page.
| remote_jupyter [2022/11/07 10:01] – Initial draft renklc | remote_jupyter [2022/11/07 10:08] (current) – removed renklc | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Running Jupyter Notebooks and JupyterLab Remotely ====== | ||
| - | |||
| - | This is a summary of the setup to run [[https:// | ||
| - | |||
| - | ===== 1. SSH Configuration ===== | ||
| - | |||
| - | Generally, if you want to log into a remote server (e.g., [[https:// | ||
| - | <code bash> | ||
| - | ssh < | ||
| - | </ | ||
| - | where ''< | ||
| - | |||
| - | Typing in your user name and the full domain of your server can be cumbersome, but you can set up a configuration file that allows you to log in using an alias. | ||
| - | |||
| - | On your //local// computer, open a terminal, and update the file '' | ||
| - | <code bash> | ||
| - | touch ~/ | ||
| - | </ | ||
| - | If the file did not yet exist, this will create an empty file for you. | ||
| - | |||
| - | Open this file with a text editor of your choice (note that the folder '' | ||
| - | < | ||
| - | Host tiko | ||
| - | | ||
| - | | ||
| - | </ | ||
| - | Here, '' | ||
| - | |||
| - | Save the file with the name '' | ||
| - | <code bash> | ||
| - | ssh tiko | ||
| - | </ | ||
| - | |||
| - | You will be prompted to enter your password on the the remote machine. | ||
| - | |||
| - | To log out of the remote server use the command '' | ||
| - | |||
| - | |||
| - | ===== 2. Passwordless SSH ===== | ||
| - | |||
| - | If you do not want to enter your password every time, you can set up an ssh-key that gives you passwordless access to the remote server. | ||
| - | |||
| - | Open an terminal on your *local* machine and check if you already have an existing key pair by listing the contents of the '' | ||
| - | <code bash> | ||
| - | ls -l ~/.ssh | ||
| - | </ | ||
| - | You are looking for the two files '' | ||
| - | |||
| - | If these files do not exist, you can generate them with the following command in your terminal: | ||
| - | <code bash> | ||
| - | ssh-keygen -b 4096 | ||
| - | </ | ||
| - | The '' | ||
| - | |||
| - | You will be prompted to enter a passphrase, but you can leave it empty to skip this extra level of protection. If the key generation was successful, you will be shown a randomart image in your terminal. | ||
| - | |||
| - | Copy the public key to the remote server: | ||
| - | <code bash> | ||
| - | ssh-copy-id tiko | ||
| - | </ | ||
| - | Note that this assumes that you have configured your ssh connection following the instructions above. If you did not set up an alias you will have to replace '' | ||
| - | |||
| - | Test the passwordless connection by logging in to the remote machine: | ||
| - | <code bash> | ||
| - | ssh tiko | ||
| - | </ | ||
| - | If you chose a passphrase for your key, you will be prompted to enter it the first time. Afterwards, you should be able to connect to the remote server without it. | ||
| - | |||
| - | To log out of the remote server use the command '' | ||
| - | |||
| - | |||
| - | ===== 3. Conda Environment ===== | ||
| - | |||
| - | To keep your code portable and reproducible, | ||
| - | |||
| - | The best way to create and curate your conda environment is using an '' | ||
| - | |||
| - | In the following, we assume that you are logged in and working on the remote machine, but the same principles apply to the work on your local computer. Let us also assume that you have a folder '' | ||
| - | < | ||
| - | my_project/ | ||
| - | ├── data/ | ||
| - | │ ├── external/ | ||
| - | │ ├── processed/ | ||
| - | │ └── raw/ | ||
| - | ├── notebooks/ | ||
| - | ├── README.md | ||
| - | ├── references/ | ||
| - | ├── reports/ | ||
| - | │ └── figures/ | ||
| - | └── src/ | ||
| - | </ | ||
| - | (You can [[https:// | ||
| - | |||
| - | Open a new file with a text editor of your choice and add the following | ||
| - | <code yaml> | ||
| - | name: my_project | ||
| - | channels: | ||
| - | - conda-forge | ||
| - | - defaults | ||
| - | dependencies: | ||
| - | - python=3.9 | ||
| - | - cartopy | ||
| - | - cmocean | ||
| - | - jupyter | ||
| - | - jupyterlab | ||
| - | - matplotlib | ||
| - | - numpy | ||
| - | - pandas | ||
| - | - xarray | ||
| - | </ | ||
| - | (Add or remove packages depending on your needs.) | ||
| - | |||
| - | Save the file as '' | ||
| - | |||
| - | To create or [[https:// | ||
| - | <code bash> | ||
| - | cd ~/ | ||
| - | conda env update --file environment.yml --prune | ||
| - | </ | ||
| - | You only have to do this once or every time you want to install a new package. | ||
| - | |||
| - | Once your conda environment is created, you need to activate it: | ||
| - | <code bash> | ||
| - | conda activate my_project | ||
| - | </ | ||
| - | You will have to run this command every time you open a new terminal or log in to the remote server to run jupyter. | ||
| - | |||
| - | |||
| - | ===== 4. Jupyter Configuration Files ===== | ||
| - | |||
| - | By default, the [[https:// | ||
| - | |||
| - | Alternatively, | ||
| - | |||
| - | Check if you already have a configuration file. Assuming that you are still logged in to the remote server and have a conda environment activated that includes the Jupyter packages, list the contents of the '' | ||
| - | <code bash> | ||
| - | ls -l ~/.jupyter | ||
| - | </ | ||
| - | You are looking for the files '' | ||
| - | |||
| - | If these files do not exist, you can create them using the commands | ||
| - | <code bash> | ||
| - | jupyter notebook --generate-config | ||
| - | jupyter lab --generate-config | ||
| - | </ | ||
| - | |||
| - | To set the password for Jupyter notebooks, run the command | ||
| - | <code bash> | ||
| - | jupyter notebook password | ||
| - | </ | ||
| - | This will ask you to enter and verify a password of your choice. | ||
| - | |||
| - | Similarly, to set the password for Jupyter labs, run the command | ||
| - | <code bash> | ||
| - | jupyter lab password | ||
| - | </ | ||
| - | This will also ask you to enter and verify a password of your choice and can be the same as for Jupyter notebooks. | ||
| - | |||
| - | You can also use these commands to reset or change your password. | ||
| - | |||
| - | There are a variety of command line arguments that can be used to to run a notebook server. Please refer to the [[https:// | ||
| - | |||
| - | |||
| - | ===== 5. Open Remote Jupyter Notebook or Jupyter Lab ===== | ||
| - | |||
| - | On your local computer, open a terminal and log in to the remote server: | ||
| - | <code bash> | ||
| - | ssh tiko | ||
| - | </ | ||
| - | |||
| - | After successful login, change into the project directory and activate the conda environment: | ||
| - | <code bash> | ||
| - | cd ~/ | ||
| - | conda activate my_project | ||
| - | </ | ||
| - | If you have issues with '' | ||
| - | |||
| - | Open Jupyter lab with the following options | ||
| - | <code bash> | ||
| - | jupyter lab --no-browser --port=XXXX | ||
| - | </ | ||
| - | Change '' | ||
| - | |||
| - | This will start a Jupyter lab on the remote server and since you specified the '' | ||
| - | |||
| - | Open a second terminal on your local computer and log in to the remote server, but this time using the port forwarding option '' | ||
| - | <code bash> | ||
| - | ssh -N -f -L localhost: | ||
| - | </ | ||
| - | |||
| - | Finally, open the URL [[http:// | ||
| - | |||
| - | Once you are finished with your work, close the browser and stop the remote Jupyter notebook by using the '' | ||
| - | |||
| - | In the second terminal, stop the port forwarding with | ||
| - | <code bash> | ||
| - | lsof -ti:8888 | xargs kill -9 | ||
| - | <code bash> | ||
| - | |||
| - | ==== 5.1. Port Forwarding in Configuration File ==== | ||
| - | |||
| - | You can make your life easier by creating and alias in the [[[#1. SSH Configuration|SSH configuration file]]. Open the file '' | ||
| - | < | ||
| - | Host tiko-jupyter | ||
| - | | ||
| - | | ||
| - | | ||
| - | </ | ||
| - | Replace ''< | ||
| - | |||
| - | Instead of typing the lengthy command in the second shell, you can now use | ||
| - | <code bash> | ||
| - | ssh -N -f tiko-jupyter | ||
| - | </ | ||
remote_jupyter.1667829693.txt.gz · Last modified: 2022/11/07 10:01 by renklc
