Use Jupyter notebooks anywhere
We're living in the mobile age when productivity is not limited to desks. Setting-up Jupyter notebooks in the cloud can enable you to use it anywhere, including your mobile phone!
In this guide, we will be using Linode, its a great starting point for cloud beginners as it offers good value for money. Naturally, for more advanced workflows we'll be using AWS, but if you're just starting out and only plan on doing basic data visualization and analysis this won't be necessary.
Step 1: Create your account & setup Linode
1.1: Select a plan
Go to this link to start settingup your Linode.
As a beginner you should go for the $5 Nanode plan as you will not be needing a powerful server for doing basic tasks with Jupyter notebooks.
1.2: Selecting a Linux Distribution
We'll be going with whatever the latest distribution of Ubuntu is, at the time of writing this is 19.04, for you the number might be higher. (.04 releases are long-term supported by Ubuntu which means you'll continue to receive updates for at least 2 years)
1.3: Add a label and create a password
Select the region that is the closest to you geographically, this will make a big difference on slower internet connections, as its faster to connect to a local server. For me this is London.
Add a label to help you remember the contents of your Linode (good if you're planning on using multiple Linodes for different tasks).
Next, create a password. The default username will be root, the password you set-up here is what we'll use to access the server for the first time.
Fill in all the information on this page and hit create in the top right-hand corner.
1.4: Copy the IP address
Next, you will see a progress bar with the screen above. The second widget on the right-hand side contains the IP address (blacked out here for security) note this down as we will use it to connect to the server.
Step 2: Connecting to the server
2.1: Install Termius
Next you'll need an SSH client. This is what will allow us to connect to a remote server, macOS has terminal built-in, but on Android and iOS Termius is the best ssh client. Get Termius here.
2.2: Creating a host
Once installed open termius and select "Start without registering" next go into "Hosts" and create a new Host. Fill in the details as follows:
Alias: Jupyter_notebook
Hostname: (Enter you linode's IP address her)
Use SSH: (Toggle this option on)
Username: Root
Password: (Enter the password we created in the previous stage)
2.3: Connect to the server
Once you've entered the above details hit save and tap on the Jupyter_notebook host to connect.
Well done! We're now connected to the server.
2.4: Install updates
One of the first things we need to do is to update the server to make sure we have the latest security patches. Enter the following commands one-by-one:
apt-get update
apt-get upgrade
Once you enter apt-get upgrade
you will be prompted with a confirmation dialog just type Y
and continue.
You might also be prompted by a language selection screen for one of the updates as I was above.
Step 3: Install Python and Jupyter Notebooks
Now that the system has been updated it's finally time to install python and Jupyter notebooks.
3.1: Install Python & Python-dev
First we should install python and python-dev as these are pre-requisites for everything we will be installing later, so type:
sudo apt install python3-pip python3-dev
3.2: Update PIP
Next lets update pip since we will be using it to update some packages
pip3 install --upgrade pip
3.3: Install useful packages
I usually always begin by installing pandas, numpy, matplotlib and scrapy. But feel free to pick and choose the packages you'll find most useful
pip3 install pandas
pip3 install numpy
pip3 install matplotlib
pip3 install scrapy
3.4: Install Jupyter Notebooks
And finally lets install Jupyter notebooks
pip3 install jupyter
Step 4: Configuring jupyter notebooks
Now that jupyter notebooks has been installed we need to configure it such that we are able to access it remotely.
4.1: Create a Jupyter Notebooks config file and edit
Lets begin by generating a jupyter notebooks config file:
jupyter notebook --generate-config
This will create an editable configuration file. Lets use nano to edit this file:
sudo nano /root/.jupyter/jupyter_notebook_config.py
This will take you to the edit screen. Here you'll see that most lines are commented out, this means that these feautres are disabled in the default configuration.
Use ctrl+w
and search for c.NotebookApp.ip
once you have lcoated this line write the following under it:
c.NotebookApp.ip = '0.0.0.0'
Then type ctrl+x
followed by Y
to confirm the edit and exit
4.2: Install and configure a firewall
Next, we need to install a firewall so that we can allow external connections.
sudo apt-get install ufw
To allow connections to the Jupyter notebooks port enter:
sudo ufw allow 8888/tcp
Next, lets enable ssh, so that we can keep connecting via ssh
sudo ufw allow 22
Finally lets restart ssh to apply the changes
sudo ufw reload
4.3: Set a Jupyter notebooks password
Now that we have enabled remote connections we should setup a Jupyter notebooks password, so that we don't have to use token authentication which can be quite complex. To begin password setup type:
jupyter notebook password
You will be prompted to enter a password (note that there will be no visual feedback as you type, so just type in your password and enter).
4.4: Run Jupyter notebooks in the background
Now to keep Jupyter notebooks running in the background even if our terminal session is halted we need to use tmux
this will open a virtual terminal session so just type
tmux
If we want to return to this session after exiting the terminal we just need to type
tmux attach
4.5: Launch Jupyter notebooks
Finally, we can launch jupyter notebooks using:
jupyter notebook --allow-root --no-browser
Note: We're have been running everything as root since setting up the server which is not recomended which is why we need to use the --allow-root
flag. Likewise since jupyter notebooks is typically run locally we need to use the --no-browser
flag,
Finally, just navigate to http://your.ip.address.here:8888
to log into your new server.
Conclusion
Finally, we're done! At the end of this tutorial, you should have a live Jupyter notebooks server that is accessible from anywhere with an internet connection. Remember to save your ssh login details and log in regularly to update packages and Linux itself.
Keep in mind that while the above setup is quick, it is not ideal. Since we are running everything as root there is a risk some packages might become corrupted, so you might want to consider setting up a virtual environment. You might also want to add an SSL certificate to encrypt your connections. But since this tutorial is targetted at beginners I felt it was necessary to keep it as simple as possible. We will be adding more advanced tutorials in the near future.