How to set up Anaconda and Jupyter Notebook the right way | by Justin Güse | Towards Data Science

If Anaconda (conda) and Jupyter Notebook (Jupyter Lab) are set up the right way the combination of them can become the perfect team, where you are able to easily switch between Deep Learning conda environments.
Some programs requiring Tensorflow 1.15, others Tensorflow 2.0? No problem! Just switch environments and Tensorflow versions with a simple click.
Also, did you ever install Jupyter Notebook extensions in each conda environment? Do not worry anymore, we are going to install the extensions once, and have them available in each environment!
Install Anaconda or MinicondaInstall Jupyter Notebook / Lab in the base environmentInstall a new environmentActivate the environment for Jupyter Notebook
Install Anaconda or Miniconda
Install Jupyter Notebook / Lab in the base environment
Install a new environment
Activate the environment for Jupyter Notebook
Anaconda is a nice package containing a lot of Python packages already and allows for an easy start into the world of Python. Additionally, it allows creating environments in python, which contain different versions of your Python packages. E.g. if a program only runs with Python 2.7 or older versions of Matplotlib, you can create an own workspace for this program and switch back to Python 3 with a click of a button. Furthermore switching between Tensorflow 2.0 and Tensorflow 1.15 becomes easy as well, finally allowing you to switch between versions easily (which can be quite a headache otherwise).
Miniconda is a barebones version of Anaconda and can be nice if you are e.g. working on a server, where disk space is limited.
To install Anaconda or Miniconda head over to their website (https://www.anaconda.com/products/individual#Downloads), or if you are using Linux just copy the following commands.
The first link crawls the website for the newest version and writes it to the LATEST_ANACONDA variable.
cd ~/DownloadsLATEST_ANACONDA=$(wget -O - https://www.anaconda.com/distribution/ 2>/dev/null | sed -ne 's@.*\(https:\/\/repo\.anaconda\.com\/archive\/Anaconda3-.*-Linux-x86_64\.sh\)\">64-Bit (x86) Installer.*@\1@p')wget $LATEST_ANACONDAchmod +x Anaconda3*.sh # make it executable./Anaconda3*.sh # execute the installer
Follow the dialogue, and just agree on the defaults.
If conda is installed correctly (might need a logout and login, or restart), you should be able to see the output when typingcondainto your terminal.
To list the currently installed environments just typeconda env list
It should currently just show the “base” environment installed.
Switching between environments works as simply as typingconda activate [NAME]and if done with it deactivating it (and going back to the base environment) withconda deactivate.
The base environment is activated by default.
Jupyter Notebook can easily be installed using conda. Our plan is to only install it in the base environment, and then just switch between sub-environments to avoid setting up Jupyter Lab in each environment.
conda install -c conda-forge notebookconda install -c conda-forge nb_conda_kernels
conda install -c conda-forge jupyterlabconda install -c conda-forge nb_conda_kernels
I really like Jupyter Notebook extensions, which support a lot of autocompletion, additional information, and in general things that make your life easier. A good default setting is included with the following install command:
conda install -c conda-forge jupyter_contrib_nbextensions
A good overview over other extensions: https://towardsdatascience.com/jupyter-notebook-extensions-517fa69d2231
In my opinion it is a good idea to add the pip package manager to the base (and each sub-) environment, as not all packages are supported by conda install. Also, if pip is not installed in each sub-environment the package might just be installed in the “base” conda environment, causing an error where the package is not found in your sub-environment.
conda install pip
Let us say you want to install both Tensorflow 2.0 and Tensorflow 1.15 in Jupyter Notebook.
For this example first, agree if you want to use the GPU or CPU version of Tensorflow. For using the GPU version add “-gpu” to TensorFlow, and otherwise, just leave it as is.
To create a new conda environment we can run
conda create --name tf-2.0
If you already plan to install some packages with it just add them to the end, like:
conda create -n tf-2.0 tensorflow-gpu pip ipykernel
I recommend installingpipfor package installation, andipykernelwill be needed to switch environments using Jupyter Notebook
To install an environment using TensorFlow 1.15 use the following:
conda create -n tf-1.15 tensorflow-gpu==1.15 pip ipykernel
If done successfully, you should be able to see three environments when executing the following command:
conda env list
basetf-2.0tf-1.15
base
tf-2.0
tf-1.15
jupyter notebook
Running Jupyter Notebook in the base environment should allow you to see a tab containing “Extensions”, as well as “conda”/“environments”. Head over to Extensions and activate whichever extensions you like, and if you are ready, create a new notebook using the “new” button. Here, you should be able to choose between your base, tf-2.0 and tf-1.15 environment.
Attention: You always need to run jupyter notebook in the base environment. Runconda deactivateto leave your current environment and return to the base one.
If you need to install further packages activate an environment usingconda activate [NAME], run your commands likeconda install Xorpip install X, and leave the environment usingconda deactivate.
Let me know if this worked for you, it helped me a lot and I wished I would have known about this earlier!
Find this and other articles on my website: https://www.datafortress.cloud/blog/how-to-set-up-anaconda-and-jupyter-notebook-the-right-way/