You can install any available Python packages you need with either pip and/or conda. However, due to technical reasons the default environment will always reset between sessions, so we need take extra care to ensure that packages are installed on your own persistent storage instead of the default environment.
pip
pip has a built-in '--user' flag that will automatically install new packages to the user environment. It is very simple to use, just include the flag in your pip install command;
pip install --user package_name
This will work both in the terminal and with a magic command within a notebook.
Conda
The setup for installing packages with conda is slightly more complicated, as we need to create a new kernel to run your notebooks with. You can also install your packages to the 'base' environment, however everything you install there will be removed between sessions.
You should run all the following commands in the terminal.
NOTE: The package manager installed on the hubs is micromamba, instead of conda. microamba is a lighter (and slightly faster) implementation of the conda package manager. All the commands below (and almost any other mamba/conda commands) are completely interchangeable with conda; just change 'mamba' to 'conda' and the commands will work just the same.
First we need to initialize the terminal to use conda by running these two commands. This step we ever only need to do once.
mamba init mv .bashrc .profile
For the changes to the take effect you must restart your terminal. After restarting, you will know that everything worked correctly if you see something like this in your terminal with your own username:
If initializing conda worked correctly, we can then create a new conda environment. With the '--prefix' flag we're creating an environment by specifying a location for instead of a name - named environments will get removed between sessions. You can change the location '~/myenv' to anything you might want.
mamba create --prefix ~/myenv mamba activate ~/myenv
Then we install the kernel and the packages we want to use. For this simple example I'm only installing pytorch. You must always make sure to include ipykernel in the environment, no matter which other packages you want to install!
mamba install ipykernel pytorch
Finally we need to register the kernel, so that Jupyter can use it. You can change the name and display name to anything you might want. You can also completely omit the display name if you prefer.
ipython kernel install --user --name=mykernel --display-name="Kernel with pytorch"
The new kernel with the installed packages should now be available for you to use for your notebooks.