---
# System prepended metadata

title: Install Jupyter Notebook on Ubuntu 18.04
tags: [virtualenv, jupyter notebook, jupyter notebook server, pip3, 2022/06]

---

# Install Jupyter Notebook on Ubuntu 18.04

###### tags: `2022/06` `jupyter notebook` `pip3` `virtualenv` `jupyter notebook server`

::: info
<ins>(2022/6/17)</ins> After installation of GPU Driver, CUDA and cuDNN, it will be easier to user Jupyter Notebook for (Python based) **ML (Machine Learning)/DL (Deep Learning)** application programming . So naturally next step is to install **Jupyter Notebook**. 
> 
> ==(For people not familiar with ML/DL applications, I would suggest to install Anaconda which includes Jupyter Notebook and package managers. It is more user friendly for ML newbies.)==
>  
>  I refer to these articles, [How to install Jupyter Notebook on Ubuntu 20.04](https://www.rosehosting.com/blog/how-to-install-jupyter-notebook-on-ubuntu-20-04/), and [Run Jupyter Notebook Through Remote Rerver on local machine](https://stackoverflow.com/questions/69244218/how-to-run-a-jupyter-notebook-through-a-remote-server-on-local-machine) for this note.
 :::

---
## Table of Contents

[TOC]
 

---
#### ++Preparation-1:++ Install pip3++
Ubuntu 18.04 already includes Python3 as the default Python, and excludes Python2. Still need to install pip3 separately.

```
$ sudo apt-get install -y python3-dev python3-pip
# Check the pip3 version installed
$ pip3 --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
```

:::warning
Please don't use `sudo` for Python3 and pip3 related instructions, as it is user applications. This reminder is from my past experiences when I encountered some Python3 or pip3 errors. I was thinking to solve it by adding `sudo`, which is a bad idea. Instead, find the root cause before trying `sudo`, unless you would like to install those commands for all users. Below one is one example using `sudo` as alternative.
:::

#### ++Preparation-2:++ Install virtualenv

It is a good habit to use `virtualenv` for different Python applications with different environment settings when there are so many applications and versions dependency and, sometimes, conflicting. Install virtualenv and related set up will hep to avoid such conflicts.

```
# Only if you will use virtualenv by current user
$ pip3 install virtualenv
# We will create a new user later, so we install virtualenv to be applied to all users. So below command set is recommended
$ sudo -H pip3 install virtualenv
```

#### ++Preparation-3:++ Set up virtualenv and execution

Add new user `jupyter` (without `sudo` priviledge).

```
$ sudo adduser jupyter
[sudo] password for marconi: 
Adding user `jupyter' ...
Adding new group `jupyter' (1002) ...
Adding new user `jupyter' (1002) with group `jupyter' ...
Creating home directory `/home/jupyter' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for jupyter
Enter the new value, or press ENTER for the default
	Full Name []: jupyter
	Room Number []: 
	Work Phone []: 
	Home Phone []: 
	Other []: 
Is the information correct? [Y/n] 
```

Set up `virtualenv` for user `jupyter`

```
$ su - jupyter
$ mkdir -p jupyter_setup
```


---
## Jupyter Notebook Execution
- ++Execute Jupyter-1:++ Jupyter Notebook uses browser
- ++Execute Jupyter-2:++ Exit from Jupyter Notebook
- ++Execute Jupyter-3:++ User Jupyter Notebook server from remote




#### ++Execute Jupyter-1:++ Jupyter Notebook uses server browser

Everything is all set, and ready to launch with following command line instructions. 

```
$ cd jupyter_setup/
$ virtualenv notebook
$ source notebook/bin/activate
(notebook) $ pip instal jupyter
(notebook) $ jupyter notebook

To access the notebook, open this file in a browser:
        file:///home/jupyter/.local/share/jupyter/runtime/nbserver-24611-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=41d4dc457e5a9c0ad7b28d6d1162d3f0e792ddf75ebf97a1
     or http://127.0.0.1:8888/?token=41d4dc457e5a9c0ad7b28d6d1162d3f0e792ddf75ebf97a1
No protocol specified
Unable to init server: Could not connect: Connection refused
Error: cannot open display: :10.0
No protocol specified
Unable to init server: Could not connect: Connection refused
Error: cannot open display: :10.0
Warning: program returned non-zero exit code #1
Opening "/home/jupyter/.local/share/jupyter/runtime/nbserver-24611-open.html" with Firefox Web Browser  (text/html)
No protocol specified
Unable to init server: Could not connect: Connection refused
Error: cannot open display: :10.0
No protocol specified
Unable to init server: Could not connect: Connection refused
Error: cannot open display: :10.0
No protocol specified
Unable to init server: Could not connect: Connection refused
Error: cannot open display: :10.0
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: iceweasel: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: seamonkey: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: mozilla: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: epiphany: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: konqueror: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: chromium: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: chromium-browser: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: google-chrome: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: www-browser: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: links2: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: elinks: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: links: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: lynx: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: w3m: not found
xdg-open: no method available for opening 'file:///home/jupyter/.local/share/jupyter/runtime/nbserver-24611-open.html'
[I 14:42:15.647 NotebookApp] 302 GET /?token=41d4dc457e5a9c0ad7b28d6d1162d3f0e792ddf75ebf97a1 (127.0.0.1) 1.920000ms
```


It will still work even when there are some error, will figure it out later what errors are. Can continue by opening web browser and copy the following URL starting with `http://localhost:8888/?.....`, the strings after `?` will change everytime when `virtualenv notebook` is launched.

```
To access the notebook, open this file in a browser:
        file:///home/jupyter/.local/share/jupyter/runtime/nbserver-24611-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=41d4dc457e5a9c0ad7b28d6d1162d3f0e792ddf75ebf97a1
```

The browser will look like below screenshot. Can click the Python3 to start coding.

![Jupyter Notebook Browser Screenshot](https://imgur.com/a/MNdIDaU)


#### ++Execute Jupyter-2:++ Exit from Jupyter Notebook


When you want to exit Jupyter Notebook, go back to `terminal`, and press `CTRL-c` to stop Jupyter Notebook.

Then use `deactivate` to exit the virtual environment.
```
(notebook) $ deactivate
$ 
```

#### ++Execute Jupyter-3:++ User Jupyter Notebook server from remote

Jupyter Notebook support server-client mode with remote access. It will be faster with local browser connected to Jupyter Notebook server, without logging to Server GUI which consumers more bandwith. 


#### ++Execute Jupyter-3.1:++ Launch Jupyter Notebook on server 
First, still at server `terminal` to launch Jupyter Notebook command. The setting of `--no-browser` is to support remote client browser by skipping browser supoprt on server.

```
$ jupyter notebook --no-browser --port=8080
    To access the notebook, open this file in a browser:
        file:///home/jupyter/.local/share/jupyter/runtime/nbserver-25611-open.html
    Or copy and paste one of these URLs:
        http://localhost:8080/?token=2da026cdce475492adec22538323452084d57ae7ef098727
     or http://127.0.0.1:8080/?token=2da026cdce475492adec22538323452084d57ae7ef098727
```

Copy the string start with `http://localhost:8080/....` for later use.

#### ++Execute Jupyter-3.2:++ Install a terminal program on client and use ssh to connect to server
2nd, go the the client PC. I have Windows 10 installed on my notebook PC, and use MobaXterm to connect to the server.

```
ssh -L 8080:localhost:8080 jupyter@10.241.69.252
```

The first port 8080 is the port defined in Jupyter Noteoobk, and the 2nd port 8080 will be used in client browser.

We use `jupyter` user to log in as we already add user earlier on server.

You might need to change the IP address of your server, which is the same as you log on to server for installation.


#### ++Execute Jupyter-3.3:++ Launch Jupyter Notebook on client PC browser

Launch browser (I user Microsoft Edge more often than Chrome nowadays), and copy the previous string `http://localhost:8080/....` to the browser. Then it is ready to go.

[:arrow_left:Previous article - Install NVIDIA GPU](https://hackmd.io/@MarconiJiang/nvidia_v100_ubuntu1804) 
[:arrow_right:Next article - Install Darknet](https://hackmd.io/@MarconiJiang/install_darknet)
[:arrow_up:back to marconi's blog](https://marconi1964.github.io/) 