These instructions explain how to install a python2 and python3 kernel in separate virtual environments for non-anaconda users. If you are using anaconda, please find my other answer for a solution directly tailored to anaconda.
I assume that you already have jupyter notebook installed.
First make sure that you have a python2 and a python3 interpreter with pip available.
On ubuntu you would install these by:
sudo apt-get install python-dev python3-dev python-pip python3-pip
Next prepare and register the kernel environments
python -m pip install virtualenv --user # configure python2 kernel python -m virtualenv -p python2 ~/py2_kernel source ~/py2_kernel/bin/activate python -m pip install ipykernel ipython kernel install --name py2 --user deactivate # configure python3 kernel python -m virtualenv -p python3 ~/py3_kernel source ~/py3_kernel/bin/activate python -m pip install ipykernel ipython kernel install --name py3 --user deactivate
To make things easier, you may want to add shell aliases for the activation command to your shell config file. Depending on the system and shell you use, this can be e.g. ~/.bashrc, ~/.bash_profile or ~/.zshrc
alias kernel2='source ~/py2_kernel/bin/activate' alias kernel3='source ~/py3_kernel/bin/activate'
After restarting your shell, you can now install new packages after activating the environment you want to use.
kernel2 python -m pip install <pkg-name> deactivate
or
kernel3 python -m pip install <pkg-name> deactivate