Install and Configure Jupyter Lab using Miniconda on Ubuntu 18.04
By: Faisal Malik Widya Prasetya
Tags: Miniconda, Jupyterlab, Ubuntu

Install and Configure Jupyter Lab using Miniconda on Ubuntu 18.04

Personally, Jupyter Lab is one of the best tools for learning data science. It’s a simple yet powerful tool to perform data science experiments, developing applications, even though it is not a complete IDE. The best thing for me, that it’s web-based, so when I have this on a server, I can always access it anywhere I want using any device I got (even mobile is possible but, I’m not really want to code in my phone). In this tutorial, I want to show how to install Jupyter Lab step by step.

Download Miniconda

Why miniconda? Miniconda is a smaller version of Anaconda and I think there are some packages in Anaconda that are not necessary for now and as a package manager, Conda can install any package easily. Run the following command to download the miniconda installer.

Install Miniconda

After you finish downloading the miniconda installer, run the installer using the bash command.

bash Miniconda3-latest-Linux-x86_64.sh

Follow the Instructions

Just hit enters and yes to all, unless you really wanna read them. The document mainly explains the terms and conditions, oh ya and there are some packages that contain cryptographic and the document explains about it.

Go to the interactive bash

After the installation, the base conda environment will not be activated directly. So you need to activate it by pressing Ctrl + D or run the following command. Once you activate the conda environment the next time you open the terminal, the base environment will be activated automatically.

. ~/.bashrc

Add Conda forge channel to get the latest update of Conda packages

The default channel used to download packages is the anaconda channel, but the official anaconda package is somehow quite slow in terms of package updates. It’ll be better to use a conda-forge channel because it’s managed by the community and some packages updated faster than the anaconda channel. Here’s the command to add that channel.

conda config --add channels conda-forge

Update all packages in the base environment

After the channel to download the package is added, you can update your package to the latest using the following command.

conda update --all -y

Create a new Conda environment

You may install your packages (JupyterLab) directly to the base environment, but it’s not recommended as it may cause dependency issues, but if you are okay with that you can skip and jump to the next two steps. You can name your environment with any name, but I recommend a short but intuitive name like growth, test, tutorial, or anything. Here’s the command and don’t forget to change the [environment-name] with the desired name for your environment, oh ya and you can remove the brackets, I use them to identify that it is a variable.

conda create -n [environment-name] -y

Activate newly created Conda environment

Once you create the environment, you can activate it using the following command. Usually, the command to activate the newly created environment is displayed in the previous command output.

conda activate [environment-name]

Install Jupyter Lab

As I mentioned above, conda as a package manager can install packages easily including jupyter lab. Below is the command to install the jupyter lab, you can also install other packages using the same command and if you want to do it with multiple packages at once, you can separate each package name with space ( ). You can search for the package name and the channel the package exists from the anaconda.org website.

conda install jupyterlab -y

Generate config file for Jupyter Lab

Once you finish with the jupyter lab installation, you can directly run the jupyter lab from the terminal, but if you run it on the server, it would be better if you set up some configuration for implementation and security purposes. Here’s the command to generate configuration files for the JupyterLab (it’s actually for the jupyter notebook but it also works with the jupyter lab).

jupyter notebook --generate-config

Generate Password for Jupyter Lab

In the configuration file, you’ll configure a password to access the jupyter lab, it’s important as the jupyter lab can access the server file and run ssh through its built-in terminal. As usual, the password has to be hashed, so even if someone can access that configuration file, the plain password can remain unknown. You will generate your hashed password in python. So you can run python and start writing a simple script to generate a password hash.

python

Once you run the python command on the terminal, the terminal will display the python console that you can run a python script for each line of it. Below is the python script to get the password. As usual, the text inside the brackets is the password you want to use, don’t forget to remove the brackets unless you intended with the password inside the brackets. The console will display the output of the hashed password, copy that hashed password as you will have to use it on the configuration step. Once you’ve done, exit the python console using the quit() function.

from notebook.auth import passwd

passwd('[your-password]')

quit()

Open Jupyter Lab config file

As you already created the configuration file, now you have to open it so you can add some configurations to it. You can use any text editor, if you’re using the desktop you may use the GUI text editor, but if you’re using a server, you may have to use the available text editor on the terminal. I personally use nano but some hardcore programmer prefer vim but it’s up to you, as long as you can save and quit the text editor :D.

[text-editor-command-call] ~/.jupyter/jupyter_notebook_config.py

Note: Some people have some difficulties to quit from vim text editors.

Add configuration into the config file

After you open the config file with the text editor, you have to add the following script to the config file. You can put them anywhere in the file as long as it’s not in the comment, but I recommend to put them on top of the file, to make it easier to find when you want to modify it again. As usual again, put your hashed password inside the brackets, if you ask what is the function of the prefix “u” before the password hash, I can tell you that it’s necessary so that the password hash is read as a Unicode and not a literal string.

c.NotebookApp.ip = '*'

c.NotebookApp.password = u'[sha1:hashed-password]'

c.NotebookApp.port = 8888

c.NotebookApp.open_browser = False

Note: If you’re using a desktop, you may want to change the last row into True, as you might want the desktop to open the browser directly after you run the jupyter lab. If you’re using server, you might prefer not to open the browser directly as your server doesn’t have a GUI browser (or at least you cannot open it using your device)

(Optional) Add SSL certificate to the config file

you might want to use an SSL certificate so that you can access the jupyter lab using HTTPS protocol. The certificate will be self-signed, so it might not be detected as a valid certificate by the browser. This part of the tutorial doesn’t provide a way to make the certificate valid, so even if you have set up all the things when you open the jupyter lab from HTTPS, you still face the problem of invalid cert. To create a valid certificate, I recommend you to use Let’sEncrypt, it’s free and trusted, but you need to have a valid domain name in order to get the SSL certificate. Below are the commands to create a self-signed SSL certificate. After you finished with the certificate creation, you have to open the config file again.

mkdir certs

cd certs

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout [key-file].key -out [cert-file].pem

cd ~

[text-editor-command-call] ~/.jupyter/jupyter_notebook_config.py

Add the following line of code to the config file. You may locate the certificate path to the safer place and change the permission, but I’ll keep it that way to make things simple. Don’t forget to save it.

c.NotebookApp.certfile = '/home/[username]/certs/[cert-name].pem'

c.NotebookApp.keyfile = '/home/[username]/certs/[key-name].key'

Run Jupyter Lab

After the installation is completed and the configuration is all set up, you can simply run the following command to run the jupyter lab. Once it runs without an error, you can proceed to the next step, but if it returns an error, you must go back and check-in which part you did something wrong, it can be the configuration, or it also can be the installation.

jupyter lab

Open Jupyter lab on https://[your-domain]:8888

As shown in the config file, the port is set up on port 8888 to access the Jupyter lab. you can change the port with any number you want (as long as the system doesn’t restrict them), 8888 is just the default port. Your domain can be localhost or your public IP address. If you skip the SSL part you may open it using HTTP and not HTTPS so, try to open it by removing the “s”.

Access without specifying the port

When you access a website without specifying the port, it doesn’t mean that the service is not located in any port, it means that the service is located in the default HTTP or HTTPS port. The default HTTP port is 80 and the default HTTPS port is 443. But the system usually restricts these ports for non-root users and I don’t recommend you install and run the jupyter lab inside the root user. So, to tackle this problem, we can use authbind to allow a non-root program to run on a restricted port. Below is the list of commands to install authbind and setup port 80 and 443 to get accessed by it.

sudo apt-get install authbind

sudo touch /etc/authbind/byport/80

sudo touch /etc/authbind/byport/443

sudo chmod 500 /etc/authbind/byport/80

sudo chmod 500 /etc/authbind/byport/443

sudo chown $USER /etc/authbind/byport/80

sudo chown $USER /etc/authbind/byport/443

Change port number on the config file

Of course, after you allow the port 80 and 443 to be accessed you need to change the default port used by the jupyter. So you need to open the config file.

[text-editor-command-call] ~/.jupyter/jupyter_notebook_config.py

And edit the following line of code

c.NotebookApp.port = 8888

Into

c.NotebookApp.port = 80

or

c.NotebookApp.port = 443

Run Jupyter lab

As you use authbind to access the 80 and 443 ports, you have to use the authbind command before you execute the jupyter lab command.

authbind --deep jupyter lab

Open JupyterLab on http://[your-domain] or https://[your-domain]

Tara… now open your browser and see that you can access the jupyter lab directly using your domain name without specifying the port number. You may have to open the jupyter lab using a password that you’ve set before.

Keep The JupyterLab running without Terminal on

Sometimes you want to access the jupyter lab anywhere without performing SSH to the server and run the jupyter lab directly. To keep the jupyter lab running without the terminal supervision, you have to “record” the process using a screen. The screen is usually already installed in ubuntu, but for minimal Ubuntu installation, it’s not installed by default, so you have to install it using the following command.

sudo apt-get update

sudo apt-get install screen

Once you have run the Jupyter lab, you can close the terminal and the JupyterLab should keep running. The screen can also be resumed, meaning that when you open your terminal and you want to do something with your recorded terminal (let say shut down the JupyterLab) you can run the following command and it’ll resume the recorded terminal.

screen

So, you’ll need to activate the environment that you used to install jupyter lab. Here are the commands.

conda activate [environment-name]

authbind --deep jupyter lab

Once you have run the Jupyter lab, you can close the terminal and the Jupyter Lab should keep running. The screen can also be resumed, mean that when you open your terminal and you want to do something with your recorded terminal (let say shut down the Jupyter Lab) you can run the following command and it’ll resume the recorded terminal.

screen -r