Setup Jupyter with Py 2, 3, and R
Note: this is out of date and due to new licensing terms, I no longer suggest using Anaconda.
Jupyter / Jupyter Notebook is a great tool for teaching code and exploratory, iterative coding.
You can quickly demo it online at Try Jupyter!
Jupyter was originally developed for Python, but it now supports kernels for many languages. With multiple kernels installed, you will be given a choice to launch notebooks using any of those languages. Thus, Notebook becomes even more useful with both Python 2 and 3 installed (keep in mind Python 3 is current, 2 is legacy). And you may as well add R while you’re at it!
1. Install Jupyter via Anaconda
First, Jupyter project and I suggest you install Python 3 via the Anaconda distribution.
Anaconda gives you a huge package of scientific Python libraries pre-installed, plus conda
, a very handy management and virtual environment tool (also comes with Anaconda Navigator GUI, which is kind of annoying, and Spyder IDE).
Once you install Anaconda Python 3, you automagically have Jupyter with the IPython3 kernel ready to go.
- Download latest version of Anaconda Python 3
- Run installer, but be sure to say yes to “add Anaconda to my PATH”.
If you already have Anaconda installed, be sure to update before adding the additional kernels.
Anaconda can be completely upgraded using conda update conda
, then conda update anaconda
.
2. Add Python 2 kernel to Jupyter
Second, add the Python 2 kernel using a virtual environment:
- open a terminal and create a new Python 2 environment:
conda create -n py27 python=2.7
. This will take a minute to install the Python 2 dependency. - activate the environment: linux
source activate py27
or windowsactivate py27
- install the kernel in the env:
conda install ipykernel
- close the env:
source deactivate
3. Add R kernel to Jupyter
Third, add R via Conda. Conda R-essentials package includes a bunch of popular R packages, including the Notebook IRKernel.
Note: Anaconda gives you the choice of using MRO or normal R. Use conda packages
mro-base
orr
respectively.
If you want R to be generally available, simply open a terminal and install R, R-essentials, and RStudio (optional, more notes): conda install -c r r-essentials rstudio
If you would rather keep R in a environment, open a terminal and create a new R env: conda create -n mro_env r-essentials mro-base
.
The R installed in this env will be available to Jupyter Notebook.
Start Notebook
Open a terminal and type: jupyter notebook
The Notebook interface will open in your browser (ignore the server back end running in the terminal). On the right side of the Notebook, click the “New” button. You should have the options for Python [default] (i.e. conda py 3), Python 2, and R. Pretty handy for teaching and learning!
To shut down Jupyter, close the browser window, then Ctrl + C
in the terminal host.
Extensions
Conda has a few other addons available for Jupyter. Check the extension documentation to learn more.
- BeakerX (newish project recently converted into an extension of Jupyter allowing multiple languages in a single notebook and other enhancements. Can be installed via
conda
)
Sharing on the web
- nbviewer (easy way to share a static rendered version of a notebook from GitHub)
- binder (open a GitHub repo of notebooks in a temporary executable environment, docs)
JupyterLab
JupyterLab is the next generation of Notebook that has been rolling out since about Feb 2018 (news). Designed as a more complete IDE-like environment, Lab still fully supports notebooks, but provides more advanced features. If you installed Anaconda, you already have Lab ready to go, and all your environments will be available.
Open a terminal and type jupyter lab
to start it up.
JupyterLab is actually available when you run
jupyter notebook
too, from the base url, something likehttp://localhost:8888/tree
, replace the directory with/lab
.
Other Notebooks
Notebooks are gaining attention for integrating code and publication, opening possibilities for interactive sharing and visualization.
- SageMath (math focused notebook platform, an “open source alternative to Magma, Maple, Mathematica, and MATLAB”. A solid project that has been around fsince 2005, but hasn’t gained much popularity.)
- Apache Zepplin (Java based notebook particularly useful for dashboards and Spark cluster integration, i.e. big data)
- nteract (desktop app or Atom package built on top of Jupyter kernels to improve/simplify notebook experience)
Others are running in the cloud to lower barriers to getting started and promote reproducibility:
- Colaboratory (Jupyter environment hosted by Google designed to run TensorFlow without installing anything, with your notebooks stored in Google Drive)
- Observable (JavaScript based data visualization for the web)
- Code Ocean (a variety of notebooks and IDE available)
- COCALC (was SageMath Cloud, now has Jupyter Notebook with lots of kernels, a LaTeX editor, and more)
- Iodide and Pyodide (web focused notebooks implemented in browser, in alpha development)
- Stencila (word processor + spreadsheet + code)
- Azure Notebooks
Issues on Linux
When installing on Fedora I have run into issues where some applications no longer work because they are trying to use Anaconda rather than the default system Python (I haven’t had this happen on Ubuntu).
The installer adds a line to your .bashrc
in order to make Anaconda available on your path.
Open sudo nano ~/.bashrc
, and look for a line like:
# added by Anaconda3 installer
export PATH="/home/username/anaconda3/bin:$PATH"
One quick fix is to flip PATH and Anaconda, so your system Python will be found first by default.
Change the .bashrc
line above to export PATH="$PATH:/home/edog/anaconda3/bin"
, then reboot.
Your applications will correctly find the system Python, but you can still easily access Anaconda’s conda
, ipython
, and jupyter
.
Keep in mind, if you type python
in the terminal, it will be the system Python, not Anaconda.