Useful snippets of code.
The function below creates a conda environment from an environment file. It will also install an ipython kernel for the environment. Place it in .zshrc or .bashrc. If you have not installed mamba then this code will work if you replace every instance of mamba with conda.
create_env() {
if [ "$#" -ne 1 ]; then
echo "Usage: create_env <path/to/environment.yml>"
return 1
fi
local env_file="$1"
local env_name
# Extract the environment name from the file, if specified
env_name=$(grep "name:" "$env_file" | awk '{print $2}')
if [ -z "$env_name" ]; then
env_name=$(basename "$env_file" .yml)
fi
# Create the Conda environment
mamba env create --file "$env_file" --name "$env_name"
# Activate the Conda environment
mamba activate "$env_name"
# Install ipykernel into the environment if not specified in the file
if ! mamba list --name "$env_name" ipykernel | grep ipykernel &>/dev/null; then
mamba install -y ipykernel
fi
# Install the IPython kernel for Jupyter
python -m ipykernel install --user --name "$env_name" --display-name "$env_name"
# Deactivate the Conda environment
mamba deactivate
}
This function removes a conda environment and a kernel of the same name.
remove_env() {
if [ "$#" -ne 1 ]; then
echo "Usage: remove_env <environment_name>"
return 1
fi
local env_name="$1"
mamba activate "$env_name"
# Uninstall the IPython kernel for Jupyter
jupyter kernelspec uninstall -y "$env_name"
# Deactivate the Conda environment
mamba deactivate
# Remove the Conda environment
mamba env remove --name "$env_name"
}
The code below was extracted from my bash history so is not a carefully crafted script. Run line-by-line and not all at once!
# Download and extract
wget https://zlib.net/current/zlib.tar.gz
tar -xvzf zlib.tar.gz
rm zlib.tar.gz
# check for the latest version of hdf5 and netcdf-c!
wget https://github.com/HDFGroup/hdf5/archive/refs/tags/hdf5_1.14.4.2.tar.gz
tar -xvzf hdf5_1.14.4.2.tar.gz
rm hdf5_1.14.4.2.tar.gz
mv hdf5-hdf5_1.14.4.2/ hdf5_1.14.4.2
wget https://github.com/Unidata/netcdf-c/archive/refs/tags/v4.9.2.tar.gz
tar -xvzf v4.9.2.tar.gz
rm v4.9.2.tar.gz
# Install zlib
cd zlib-1.3.1/
ZDIR=/usr/local/
./configure --prefix=${ZDIR}
make check
sudo make install
# Install hdf5
cd ../hdf5_1.14.4.2/
H5DIR=/usr/local
./configure --with-zlib=${ZDIR} --prefix=${H5DIR} --enable-hl
make check
sudo make install
# Install netcdf
cd ../netcdf-c-4.9.2/
NCDIR=/usr/local
sudo apt install m4
sudo apt-get install libxml2-dev libxml2-doc
sudo apt-get install libxml2
CPPFLAGS='-I${H5DIR}/include -I${ZDIR}/include' LDFLAGS='-L${H5DIR}/lib -L${ZDIR}/lib' ./configure --prefix=${NCDIR}
make check
sudo make install