Python & Jupyter on Ubuntu
Recently I started an online course to learn Python to understand the global concepts of Machine learning, artificial intelligence (AI), Big Data, and Robotics which all rely heavily on Python. Even Cyber security is driven by Python. While Java is still the number one requested development language, Python is already in the top 5 overall (source; https://stackify.com). The following guide is to implement Jupyter notebook to run Python both version 2 and 3 inside a VM on Ubuntu Server.
I started with a clean Ubuntu server installation and, after the update/upgrade of the software, I ran the following command to install the basic requirement;
albert@Jupyter:~$ sudo apt-get -y install python-pip python3-pip ipython3
We need to use pip (packet management software) to get jupyter though when I ran it on the normal prompt (even even with [sudo]) I got the following error;
albert@Jupyter:~$ pip install Traceback (most recent call last): File "/usr/bin/pip", line 11, in <module> sys.exit(main()) File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 215, in main locale.setlocale(locale.LC_ALL, '') File "/usr/lib/python2.7/locale.py", line 581, in setlocale return _setlocale(category, locale) locale.Error: unsupported locale setting
I discovered the hard way that if you become root, the installation went just fine. By default python 2 was running so I also installed a extra kernel to run python 3.
albert@Jupyter:~$ sudo su - root@Jupyter:~# pip install --upgrade pip root@Jupyter:~# pip install jupyter root@Jupyter:~# python3.5 -m pip install ipykernel root@Jupyter:~# exit
Afterwards you are set to run! Though you will need to start Jupyter in a new terminal to get it running. You might run into trouble if you don’t. I use screen for this. Screen also is good for running even if you kill your connection to the ssh terminal. It’s a bit getting use to the commands involved though once you get the hang of it… (quick introduction; hit CTRL+A and than N (next) or C (Create) to jump or create between screen sessions after you have launched your first screen session). I also need to be able to access the notebook from another PC in my network, hence the extra syntax [–ip=’*’] . Below is the output of the full command after a first run;
albert@Jupyter:~$ jupyter notebook --ip='*' [I 11:09:12.035 NotebookApp] Writing notebook server cookie secret to /run/user/1000/jupyter/notebook_cookie_secret [I 11:09:12.165 NotebookApp] Serving notebooks from local directory: /home/albert [I 11:09:12.165 NotebookApp] 0 active kernels [I 11:09:12.165 NotebookApp] The Jupyter Notebook is running at: [I 11:09:12.166 NotebookApp] http://localhost:8888/?token=163fc82e0e05c45c0f335827e92eaf8e7895fbf139c635f0 [I 11:09:12.166 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [W 11:09:12.166 NotebookApp] No web browser found: could not locate runnable browser.
Now you can start a web browser that points to your server IP and use port 8888. You will need the token that is listed in the start (5th line). You might want to set a password right away so you won’t have to use the token all the time. This provided me with some problems as it was unable to save the the password and I got a 404 error on the browser. If you ran into this problem it has to do with permission. I discovered that my user (albert in my case) was not the owner of the directory that holds the configuration thus the password. Below is the [chown] command I use to change the containing directories towards my username as owner.
albert@Jupyter:~$ sudo chown albert /home/albert/.local albert@Jupyter:~$ sudo chown albert /home/albert/.local/share/jupyter/
That’s it! You should now have Jupyter running with both Python version 2 and 3 on Ubuntu Server