# Environments on HPC

Information regarding how our software and package environments are handled on the HPC

# Jupyter Notebooks in Python Virtual Environments

Jupyter Notebook is a popular data science and analysis application written in Python. It is probably one of our most requested resources here at AppState Research Computing. We now have some self-service ways to run it on our compute nodes. Probably the quickest way is through [Virtual Desktop application in Open On Demand. ](https://res-wiki.appstate.edu/books/hpc-introduction/page/virtual-desktops-in-open-ondemand) Please click that link and read the documentation on how to access those if you are not already familiar.

After you've requested and launched your Virtual Desktop on the cluster open up the terminal application in XFCE and create a Python virtual environment:

```bash
[lh59281@hpc2 ~]$ python -m venv jupyter-astronomy
[lh59281@hpc2 ~]$ source ./jupyter-astronomy/bin/activate
(jupyter-astronomy) [lh59281@hpc2 ~]$
```

You can name the virtual environment anything you like and in fact have multiple virtual environments for different Jupyter installs. This is very useful for different data analysis tasks. Next install Jupyter Lab with pip<span style="color: rgb(186, 55, 42);"> **\[ NOTE: it is inadvisable install things with pip outside of a virtual environment \]:**</span>

```bash
(jupyter-astronomy) [lh59281@hpc2 ~]$ pip install jupyterlab
```

<span style="color: rgb(0, 0, 0);">You'll see a bunch of text fly past the screen and you may see a warning about pip being outdated. You can ignore that. </span>

<span style="color: rgb(0, 0, 0);">Next launch the application:</span>

```bash
(jupyter-astronomy) [lh59281@hpc1 ~]$ jupyter lab
```

Some more text will stream by and a web browser will open with the Jupyter Lab interface:

[![Screenshot 2025-11-04 at 13.14.26.png](https://res-wiki.appstate.edu/uploads/images/gallery/2025-11/scaled-1680-/screenshot-2025-11-04-at-13-14-26.png)](https://res-wiki.appstate.edu/uploads/images/gallery/2025-11/screenshot-2025-11-04-at-13-14-26.png)

You can now load in Notebook files and run them as you would on your local desktop. You'll either need to upload them via Open OnDemand or SFTP before hand. Git clone from github also works just fine!  
  
You can also install additional required pip packages in your virtual environment if needed. For example the example astronomy notebook requires numpy:

```bash
(jupyter-astronomy) [lh59281@hpc2 ~]$ pip install numpy
```

In addition to that you can load packages from a requirements.txt file if the code you're using ships one:

```
(jupyter-astronomy) [lh59281@hpc2 AstroInteractives]$ pip install -r requirements.txt
```

When you are done with your Jupyter Notebook you can deactivate the Python enivornment in the terminal (close the Jupyter browser first):

```bash
(jupyter-astronomy) [lh59281@hpc2 ~]$ deactivate
[lh59281@hpc2 ~]$
```

The prompt changes back to the standard bash prompt and you can close the window and log out of your Virtual Desktop application.

# Building Conda environments in Apptainer

Apptainer is a containerization technology forked from Singularity. It's free, open source and compatible with Docker and Singularity images. This has the advantage of keeping all of your Conda environment programs in one convienent file and avoiding conflicts and problematic entanglements with other Conda environments on the same machine.

Apptainer containers are built from a definition file, let's start with this example below:

```
Bootstrap: docker
From: continuumio/miniconda3

%post -c /bin/bash
    # Update and install system wide software here
    apt-get update -y
    # if you need apt packages install them here
    # apt-get install <package>

    # install conda stuff here, numpy just for example
    conda install -y numpy

%environment
    # Ensure Conda environment is in the PATH
     PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH"
     source /opt/conda/etc/profile.d/conda.sh
     conda activate base
```

Save this file to MyCondaEnv.def on a machine with Apptainer installed. HPC1 is a fine place to do this as we have Apptainer available there for building images. You can also do it on your local machine and upload the resulting SIF file to HPC1 at a later date.

Now is time to build the image:

```bash
apptainer build --fakeroot MyCondaEnv.sif MyCondaEnv.def
```

This may take a minute or two.

After it has successfully finished you'll be left with the MyCondaEnv.sif file. This is where the actual Conda binaries and packages are stored. Once created it cannot be modified and has to be rebuilt if you want to add a new package.

Now you can run the Apptainer image and interact with your data in one of two primary ways. Firstly through apptainer exec:

```bash
apptainer exec MyCondaEnv.sif conda list
```

Where everything after the SIF file will be run inside the container. This prints a list of installed conda packages. Alternatively you can use apptainer run and it will drop you into a shell on the container.

```bash
[lh59281@hpc2 ~]$ apptainer run MyCondaEnv.sif
(base) ls
AstroInteractives  Pictures           apptainer_test.sh                gauss_test.log       jupyter2                   jupyternoRoot.def  other_data       processing        test_mpi
Desktop            Public             conda.def                        gpu_test.bsh         jupyterhub.sqlite          lh59281_lambda     output-9604.log  shiny-server.def  test_mpi.c
(base)
```

Notice that here I just did a ls and it lists my home directory. You can any program installed in the Apptainer container on data stored in your home directory.

It is recommended to submit the actual running of the computations through Slurm or use Apptainer via the [Virtual Desktops](https://res-wiki.appstate.edu/books/hpc-introduction/page/virtual-desktops-in-open-ondemand) available in [Open OnDemand](https://res-wiki.appstate.edu/books/hpc-introduction/page/accessing-the-hpc-via-open-ondemand).  
  
Let's say you forgot a Conda package and need to add it. If you try to install it from inside the Apptainer shell you'll get an error:

```bash
EnvironmentNotWritableError: The current user does not have write permissions to the target environment.
  environment location: /opt/conda
  uid: 1002
  gid: 1002
```

These containers are create once, read only and easily rebuilt so the solution is to exit your container shell (Crtl+D) and make a new container. First let's update the DEF file:

```
Bootstrap: docker
From: continuumio/miniconda3

%post -c /bin/bash
    # Update and install system wide software here
    apt-get update -y
    # if you need apt packages install them here
    # apt-get install <package>

    # install conda stuff here, numpy just for example
    # adding astropy here for a rebuild example
    conda install -y numpy astropy

%environment
    # Ensure Conda environment is in the PATH
     PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH"
     source /opt/conda/etc/profile.d/conda.sh
     conda activate base
```

Now build a new container:

```
apptainer build --fakeroot MyCondaEnv2.sif MyCondaEnv.def
```

After that has completed, run it as before:

```bash
apptainer run MyCondaEnv2.sif
```

Verify the new packages are loaded:

```
[lh59281@hpc2 ~]$ apptainer run MyCondaEnv2.sif
(base) conda list | grep astropy
astropy                   7.0.0           py313h5eee18b_0
astropy-iers-data         0.2025.6.23.0.39.50 py313h06a4308_0
(base)
```