Copied from : https://www.digitalocean.com/community/tutorials/how-to-set-up-a-jupyter-notebook-to-run-ipython-on-ubuntu-16-04
IntroductionIPython is an interactive command-line interface to Python. Jupyter Notebook offers an interactive web interface to many languages, including IPython.
This article will walk you through setting up a server to run Jupyter Notebook as well as teach you how to connect to and use the notebook. Jupyter notebooks (or simply notebooks) are documents produced by the Jupyter Notebook app which contain both computer code (e.g. Python) and rich text elements (paragraph, equations, figures, links, etc.) which aid in presenting reproducible research.
By the end of this guide, you will be able to run Python 2.7 code using Ipython and Jupyter Notebook running on a remote server. For the purposes of this tutorial, Python 2 (2.7.x) is used since many of the data science, scientific computing, and high-performance computing libraries support 2.7 and not 3.0+.
PrerequisitesTo follow this tutorial, you will need the following:
- Ubuntu 16.04 Droplet
- Non-root user with sudo privileges (Initial Server Setup with Ubuntu 16.04 explains how to set this up.)
sudo. Initial Server Setup with Ubuntu 16.04 explains how to add users and give them sudo access.
Step 1 — Installing Python 2.7 and PipIn this section we will install Python 2.7 and Pip.
First, update the system's package index. This will ensure that old or outdated packages do not interfere with the installation.
Next, install Python 2.7, Python Pip, and Python Development:
- sudo apt-get update
- sudo apt-get -y install python2.7 python-pip python-dev
python2.7will update to the latest version of Python 2.7, and
python-pipwill install Pip which allows us to manage Python packages we would like to use. Some of Jupyter’s dependencies may require compilation, in which case you would need the ability to compile Python C-extensions, so we are installing
To verify that you have python installed:
This will output:
- python --version
Depending on the latest version of Python 2.7, the output might be different.
You can also check if pip is installed using the following command:
You should something similar to the following:
- pip --version
Similarly depending on your version of pip, the output might be slightly different.
Outputpip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
Step 2 — Installing Ipython and Jupyter NotebookIn this section we will install Ipython and Jupyter Notebook.
First, install Ipython:
Now we can move on to installing Jupyter Notebook:
- sudo apt-get -y install ipython ipython-notebook
Depending on what version of pip is in the Ubuntu apt-get repository, you might get the following error when trying to install Jupyter:
- sudo -H pip install jupyter
If so, you can use pip to upgrade pip to the latest version:
OutputYou are using pip version 8.1.1, however version 8.1.2 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
Upgrade pip, and then try installing Jupyter again:
- sudo -H pip install --upgrade pip
- sudo -H pip install jupyter
Step 3 — Running Jupyter NotebookYou now have everything you need to run Jupyter Notebook! To run it, execute the following command:
- jupyter notebook
To ignore the error, you can press
Qand then press
A log of the activities of the Jupyter Notebook will be printed to the terminal. When you run Jupyter Notebook, it runs on a specific port number. The first notebook you are running will usually run on port
8888. To check the specific port number Jupyter Notebook is running on, refer to the output of the command used to start it:
If you are running Jupyter Notebook on a local Linux computer (not on a Droplet), you can simply navigate to
Output[I NotebookApp] Serving notebooks from local directory: /home/sammy [I NotebookApp] 0 active kernels [I NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/ [I NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
localhost:8888to connect to Jupyter Notebook. If you are running Jupyter Notebook on a Droplet, you will need to connect to the server using SSH tunneling as outlined in the next section.
At this point, you can keep the SSH connection open and keep Jupyter Notebook running or can exit the app and re-run it once you set up SSH tunneling. Let's keep it simple and stop the Jupyter Notebook process. We will run it again once we have SSH tunneling working. To stop the Jupyter Notebook process, press
Y, and hit
ENTERto confirm. The following will be displayed:
Output[C 12:32:23.792 NotebookApp] Shutdown confirmed [I 12:32:23.794 NotebookApp] Shutting down kernels
Step 4 — Connecting to the Server Using SSH TunnelingIn this section we will learn how to connect to the Jupyter Notebook web interface using SSH tunneling. Since Jupyter Notebook is running on a specific port on the Droplet (such as
:8889etc.), SSH tunneling enables you to connect to the Droplet's port securely.
The next two subsections describe how to create an SSH tunnel from 1) a Mac or Linux and 2) Windows. Please refer to the subsection for your local computer.
SSH Tunneling with a Mac or LinuxIf you are using a Mac or Linux, the steps for creating an SSH tunnel are similar to the How To Use SSH Keys with DigitalOcean Droplets using Linux or Mac guide except there are additional parameters added in the
sshcommand. This subsection will outline the additional parameters needed in the
sshcommand to tunnel successfully.
SSH tunneling can be done by running the following SSH command:
- ssh -L 8000:localhost:8888 your_server_username@your_server_ip
sshcommand opens an SSH connection, but
-Lspecifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side (Droplet). This means that whatever is running on the second port number (i.e.
8888) on the Droplet will appear on the first port number (i.e.
8000) on your local computer. You should change
8888to the port which Jupyter Notebook is running on. Optionally change port
8000to one of your choosing (for example, if
8000is used by another process). Use a port greater or equal to
8002, etc.) to avoid using a port already in use by another process.
server_usernameis your username (i.e. sammy) on the Droplet which you created and
your_server_ipis the IP address of your Droplet. For example, for the username
sammyand the server address
188.8.131.52, the command would be:
If no error shows up after running the
- ssh -L 8000:localhost:8888 firstname.lastname@example.org
ssh -Lcommand, you can run Jupyter Notebook:
Now, from a web browser on your local machine, open the Jupyter Notebook web interface with
- jupyter notebook
http://localhost:8000(or whatever port number you chose).
SSH Tunneling with Windows and PuttyIf you are using Windows, you can also easily create an SSH tunnel using Putty as outlined in How To Use SSH Keys with PuTTY on DigitalOcean Droplets (Windows users).
First, enter the server URL or IP address as the hostname as shown:
Next, click SSH on the bottom of the left pane to expand the menu, and then click Tunnels. Enter the local port number to use to access Jupyter on your local machine. Choose
8000or greater (ie
8002, etc.) to avoid ports used by other services, and set the destination as
:8888is the number of the port that Jupyter Notebook is running on. Now click the Add button, and the ports should appear in the Forwarded ports list:
Finally, click the Open button to connect to the server via SSH and tunnel the desired ports. Navigate to
http://localhost:8000(or whatever port you chose) in a web browser to connect to Jupyter Notebook running on the server.
Step 5 — Using Jupyter NotebookThis section goes over the basics of using Jupyter Notebook. By this point you should have Jupyter Notebook running, and you should be connected to it using a web browser. Jupyter Notebook is very powerful and has many features. This section will outline a few of the basic features to get you started using the notebook. Automatically, Jupyter Notebook will show all of the files and folders in the directory it is run from.
To create a new notebook file, select New > Python 2 from the top right pull-down menu:
$$symbols. For example, type the following into the cell after changing it to markdown:
To turn the markdown into rich text, press
# Simple Equation Let us now implement the following equation: $$ y = x^2$$ where $x = 2$
CTRL+ENTER, and the following should be the results:
To run the code, press
x = 2 y = x*x print y
CTRL+ENTER. The following should be the results: