When working with Jupyter Notebook/Lab I found Conda to be one of the best ways to manage my virtual environments.
Python and package version differences can be breaking and so it is essential to have some method of managing different environments. My only issue with Conda is that the default install and settings seem suboptimal and incomplete so every time I need to set Conda up I need to search through multiple different resources. I hope this article could be an answer to all those questions for anyone else having the same problems as I was.
While I highly recommend using Conda this article also provides instructions for virtualenv in the rare cases when it is more convenient.
Install Conda
Conda
is an free and open-source package and environment management toolAnaconda
is the company that originally developed Conda and provides thedefaults
channel for downloading packages
At the time of writing, Anaconda has recently changed their licensing terms regarding the usage of their defaults
channel to require organizations (including non-profit and government) with over 200 employees to purchase a paid license.
Educational institutions are exempt, if their usage is for ‘‘curriculum-based courses’’.
These changes only apply to the defaults
channel and the Anaconda Distribution (as it comes with a bunch of packages from the defaults
channel).
Conda itself is still free and unless you have a good reason to I would recommend moving over exclusively to the conda-forge
channel (not only is it free but it has more packages and is usually more up to date).
Just note that conda-forge
is mainly hosted by Anaconda and you will probably only want the defaults
if you work in an industry that is more strict on security and stability.
Miniforge (Recommended)
Miniforge is a minimal install for Conda with the following features:
- Base environment packages are only from
conda-forge
- The default (and only) channel is
conda-forge
wget "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh" -O miniforge.sh
bash miniforge.sh
Then follow the installation prompts (I usually install to /home/<user>/.conda
) and after you are done feel free to delete the installer with rm miniforge.sh
.
Close and re-open your terminal window, then if you are getting a Command Not Found
when running conda
you might need to run
/home/<user>/.conda/bin/conda init
Anaconda or Miniconda
- Anaconda: installs conda as well as Anaconda Navigator and a bunch of packages for data science
- Miniconda: minimal installer for conda
I would recommend Anaconda if you are new to Python or Conda and Miniconda if you know a what you are doing as you can just install what you actually need later.
- To install Anaconda download from here
- To install Miniconda run
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
Whichever you download run
bash <conda-installer>.sh
Then follow the installation prompts (I usually install to /home/<user>/.conda
) and after you are done feel free to delete the installer with rm <conda-installer>.sh
.
Close and re-open your terminal window, then if you are getting a Command Not Found
when running conda
you might need to run
/home/<user>/.conda/bin/conda init
Install Jupyter
The most basic method is to simply install Jupyter to every Conda environment and to activate that specific environment before running Jupyter every time.
The better method is to have Jupyter dynamically find the kernels in each Conda environment so we only need one install of Jupyter and can switch kernels as we please.
Method 1: Jupyter outside Conda
If you want to be able to run Jupyter without activating any conda environments you can use your package manager to install Jupyter then use the following instructions to create an expose your an environment.
conda create -n myenv ipykernel
conda activate myenv
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
sudo apt install python3-jupyterlab-server
jupyterlab
Now in the kernel selection you should see myenv
.
To see what kernels are available we can list them with
jupyter kernelspec list
To uninstall the kernel we can run
jupyter kernelspec uninstall myenv
Method 2: Jupyter inside Conda (Recommended)
Using nb_conda_kernels Jupyter can see the kernels in the other Conda environments (I personally like using base
as my notebook env).
conda create -n notebook_env nb_conda_kernels jupyterlab
conda create -n myenv ipykernel
conda activate notebook_env
jupyterlab
Now in the kernel selection you should see myenv
.
To see what kernels are available you can list them with
python -m nb_conda_kernels list
To uninstall a kernel we just need to remove the ipykernel
from the environment.
Other
Conda Tips
Don’t start conda when logging into the shell
conda config --set auto_activate_base false
List all explictly installed packages to conda (does not include packages installed via pip
inside the env)
conda env export --from-history -n myenv
Delete a conda env
conda remove -n myenv --all
Virtualenv
If you prefer to use Virtualenv/venv you can install it for a certain version of Python (in this case Python 3.9) and create an venv by running.
sudo apt install python3.9 python3.9-venv
python3.9 -m venv venv
source venv/bin/activate
Note that 3.9
can be omitted if the version of Python doesn’t matter.
Now we can test that are are indeed in the venv by checking which executable we are running when we type python
or pip
.
which python
# /absolute/path/to/venv/bin/python
which pip
# /absolute/path/to/venv/bin/pip
- To deactivate the venv run
deactivate
- To delete it we just delete the entire folder with
rm -r venv