I’ve just been introduced to Jupyter Notebook by a coding buddy. I’d heard about it, it’s used extensively on my campus, but since I wasn’t into Python, it never kept it on my radar.
Until NOW!
I decided to spin up an instance in AWS so I could practice Python from any computer. Here goes.
Log into the AWS Management Console. Select your region. Under Compute, click on EC2. On the EC2 dashboard, click on launch instance. This wizard will walk you through spinning up an Amazon Linux 2 AMI that we’ll install Jupyter Notebook on.
Launch An Instance
Step 1: Select the Amazon Linux 2 AMI
Step 2: Choose a free tier eligible instance type (t2.micro) and click Next: Configure Instance Details
Step 3: Select the VPC you wish to install your instance in. I’ve chosen the default_vpc for my installation as well as no preference for my subnet. Click Next: Add Storage
Step 4: Set the root volume size to 15GB. Click Next: Add Tags
Step 5: Add Tags. Assign meaningful tags to your instance. Click Next: Configure Security Group.
Step 6: Select ‘create a new security group’ and give it a name and a meaningful description. Add a custom TCP rule to allow port 8888 and HTTPS. Change the source on the SSH rule to your IP by clicking on the drop-down under the source and selecting My IP. Click Review and Launch.
Create a key pair or select one that you already have. Check the box to confirm. Click launch.
Click view instances to see when your instance comes up. Once status checks are 2/2, copy the public IP of your instance and SSH into your instance using the pem you have or the one you created. If you’ve just created a key pair, select the check box next to your instance, click on Connect. This will instruct you on how to log into your instance.
Connect to Instance and run updates
Update the server with the latest patches and install vim and wget if they’re not already installed on your instance.
sudo yum update –y
sudo yum install vim wget –y
Install Anaconda
Head over the Anaconda website and download it with wget. To download it to your instance, right click on the download button for Python 3.7 (the latest) and copy the link location to your clipboard.
From your instance, type wget and paste the URL from your clipboard.
wget https://repo.anaconda.com/archive/Anaconda3-2019.07-Linux-x86_64.sh
This is the current version at this time, but your version may differ. Once the script is downloaded, install it by running:
bash Anaconda3-2019.07-Linux-x86_64.sh
Press ENTER to continue.
Press ENTER several times read and get to the end of the EULA.
Type YES to accept.
The installer will tell you where it’s going to install. Keep the default location and press ENTER. The installer will begin unpacking and installing new packages. Once the install is finished, it’ll ask you if you’d like to initialize Anaconda3, type YES.
Configure your environment
Edit your .bashrc and add anaconda to your path. From your home directory, open .bashrc with vim.
vim .bashrc
Add the following to the top of the file.
# Jupyter Notebook
export PATH=/home/ubuntu/anaconda3/bin:$PATH
Save and exit the file. To reload your shell, do this:
source .bashrc
Your prompt should change to (base). Check to see if python is in your path by running:
which python
If Anaconda was able to configure your environment while it was doing the installation, you may be pointing to anther path for python. Instead of /home/ubuntu/anaconda3/bin/python, your python path may be ~/yes/bin/python. Your mileage may vary.
Configure Jupyter Notebook
First, we’ll create a password for Jupyter Notebook by using the IPython library and importing a module called passwd.
ipython
Then, import the module. Watch the case.
from IPython.lib import passwd
passwd()
Enter a password, then confirm it.
You’ll get a sha1 hash of your password, copy it to your clipboard as you will need it in the next step.
Next, we will generate a Jupyter Notebook config file. This is where you’ll put configuration settings for your installation. Run:
jupyter notebook --generate-config
This will create a config file in /home/ec2-user/.jupyter/jupyter_notebook_config.py. Open the config file using vim. At the top of the file enter the following:
vim .jupyter/jupyter_notebook_config.py
conf = get_config()
conf.NotebookApp.certfile = u’/home/ec2-user/certs/mycert.pem’
conf.NotebookApp.keyfile = u’/home/ec2-user/certs/mykey.key’
conf.NotebookApp.ip = ‘0.0.0.0’
conf.NotebookApp.password = u’sha1:701b3d3e0043:2aae91606d863cbe6b27b736′
conf.NotebookApp.port = 8888
conf.NotebookApp.open_browser = False
This does a few things:
- It assigns the get_config() function to a variable called conf.
- Now that you have a reference to the configuration object, you can set some attributes that Jupyter Notebook will need
- Set the location of your self-signed cert. We’ll do that next.
- Configure the IP to 0.0.0.0.
- Set your password to log in using the hash generated by running passwd()
- Configure port that the app will listen on.
- Tell the app not to try to open a browser when you launch.
Save the file and exit.
Create a self-signed certificate
In the root of your home directory, create a folder called certs.
cd ~
mkdir certs
cd certs
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mykey.key -out mycert.pem
Feel free to call your key and pem whatever you like, but remember, you’ll have to edit the config file to match it. Follow the prompts and enter whatever you like.
Create folders for your notebooks
From the root of your home directory, create a folder and name it whatever you like. I’m naming my folder, Notebooks.
cd ~
mkdir Notebooks
cd Notebooks
Run your Jupyter Notebook
From within your Notebook directory:
jupyter notebook
Your server will start. The output from the terminal gives you information about how to connect to your server. Open a browser and point to the public DNS name of your instance. Append the port number to the end.
https://ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com:8888/
*NOTE* This URL will change every time you stop and start your instance.
Since this is a self-signed cert, you may get warnings. This is expected. Log in with the password you chose during set up.
Now I have to learn how to use it. Wish me luck!