.. _reference.create_bluesky_enviroment: Create bluesky environment ========================== Describes the steps to create a virtual environment for a bluesky instrument (see `setup a bluesky instrument `__). A bluesky instrument uses many Python packages not included in the Python Standard Library. Rather than add these directly into the system Python installation, it is recommended to create a Python virtual environment which has the suite of packages (and specific versions) required. .. raw:: html
More about conda virtual environments A virtual environment gives you control over the suite of installed packages and insulates you from any system software updates. It is likely that you will have more than one conda environment available. Each of these has a complete installation of Python with a suite of packages. The suite may differ between the environments, as well as the package versions. Even the Python version itself may be different. The base environment refers to the default environment created when you install conda, which is a package manager and environment management system for Python. The base environment is created automatically during the installation process, and it serves as the starting point for managing and creating additional environments. When you install packages or libraries using conda without specifying a specific environment, the packages are installed into the base environment by default. The base environment is often used for general-purpose Python development or as a fallback option when you haven't explicitly created any other environments. However, it is recommended to create separate environments for different projects or applications to avoid conflicts between package versions. This allows you to isolate the dependencies of each project and maintain consistent environments.
Why conda? Here, we describe the creation of a Python virtual environment managed by conda. While opinions may vary, we see some advantages to using conda over venv. Consequently, we use conda to install most packages, falling back to pip to install any remaining packages. We use YAML files to describe these conda environments. See this article for more instruction about conda environment YAML files.
Activate conda -------------- **IMPORTANT**: In Linux, use the ``bash`` command shell. For more info see `what is bash? `__ .. tabs:: .. tab:: At APS On an APS machine with access to APSshare, run this command from a terminal session: .. raw:: html
         $ source /APSshare/miniconda/x86_64/bin/activate
         
If you are getting an error, contact the Bluesky support team. .. tab:: Not at APS On a workstations with no access to APSshare, type the following command: .. raw:: html
         $ which conda
         
If the output prints the path to conda, you can activate it by using: .. raw:: html
         $ source /PATH/TO/CONDA/bin/activate
         
However, if the command ``which conda`` does not return anything, or if you are getting an error message (``bash: conda: command not found`` or ``bash: activate: No such file or directory``), conda is not installed on your computer or it is not added to the system's ``PATH`` environment variable. You can install conda by following the installation instructions for your operating system. You can find the instructions for Windows, macOS, and Linux on the official conda documentation `website `__. Note: Alternatively, you can install `miniconda `__, an ideal base environment since it installs a minimal suite of Python packages, useful only the most basic tasks, such as creating local environments for user. For Linux, the recommended installation is described in details below: .. raw:: html
Recommended Miniconda installation for Linux To prevent users from modifying the conda base environment by accident, it is recommanded to install it as read-only. This can be achieved by installing miniconda with elevated privileges (this type of account refers to a user account that has administrative rights, also known as the root account or the superuser account).
The installation steps are described in the following bash script:

         # pick the installer script
         INSTALLER=Miniconda3-latest-Linux-x86_64.sh
         # INSTALLER=Miniconda3-py310_23.3.1-0-Linux-x86_64.sh

         # pick the installation location for your system
         # INSTALL_DIR=/opt/miniconda3
         INSTALL_DIR=/APSshare/miniconda/x86_64

         # download the installer script
         wget "https://repo.anaconda.com/miniconda/${INSTALLER}"

         # install Miniconda
         bash ${INSTALLER} -b -p "${INSTALL_DIR}"

         # install libmamba, mamba, & micromamba
         source "${INSTALL_DIR}/bin/activate"
         conda update -y -n base conda
         conda install -y -n base conda-libmamba-solver
         # conda install -y -n base -c conda-forge mamba --solver=libmamba
         conda install -y -n base -c conda-forge micromamba --solver=libmamba

         # set some defaults (can override in local settings)
         CONFIG_FILE="${INSTALL_DIR}/condarc"
         echo "channels:" > "${CONFIG_FILE}"
         echo "  - defaults" >> "${CONFIG_FILE}"
         echo "  - conda-forge" >> "${CONFIG_FILE}"
         echo "  - apsu" >> "${CONFIG_FILE}"
         echo "  - aps-anl-tag" >> "${CONFIG_FILE}"
         echo "channel_priority: flexible" >> "${CONFIG_FILE}"
         echo "solver: libmamba" >> "${CONFIG_FILE}"

         # update ~/.bashrc to activate base environment on login
         conda init
         

If you still encounter the same error message after installing conda or miniconda, you may need to add the conda installation directory to your system's ``PATH`` environment variable manually. You can find instructions on how to do this in the `conda documentation `__. When ``conda`` is activated, the prompt changes to display ``(base)``. Now you can use ``conda env list`` to see the environments you have and the directories in which they are installed. .. raw:: html
   $ conda env list
   # conda environments:
   #
   bluesky_2022_2           /home/username/.conda/envs/bluesky_2022_2
   bluesky_2022_3           /home/username/.conda/envs/bluesky_2022_3
   bluesky_2023_1           /home/username/.conda/envs/bluesky_2023_1
   base                  *  /opt/miniconda3
   
The environment with the ``*`` is the active one. The command prompt is also prefixed with the environment name, as mentioned above. .. raw:: html
conda channels Channel refers to a repository or a source from which Conda packages can be downloaded and installed. To see your default channels:
    $ conda config --show channels
    
To add more channels:
    $ conda config --env --append channels conda-forge 
    $ conda config --env --append channels apsu
    
When you use Conda to install a package, it will search for the package in the specified channel or channels. If the package is found, Conda will download and install it along with its dependencies. By default, Conda will search for packages in the default channels, but you can also specify additional channels to search in using the -c or --channel option when using the conda install command.
libmamba solver In 2022, a significant performance enhancement was made available to conda by inclusion of the conda-libmamba-solver package. If you have installed miniconda using the instructions for Linux above, this package is already installed and configured as the default solver. You can check your default solver with the following command:
    $ conda config --show solver
    
If the above command returns classic, follow those steps:
  • Install the libmamba solver:
  •     $ conda install -c conda-forge libmamba
        
  • Set libmamba as the default solver:
  •     $ conda config --set solver libmamba
        
  • Confirm libmanda is now your default solver:
  •     $ conda config --show solver
        
Install the bluesky environment ------------------------------- The following commands install the ``bluesky_2023_3`` environment inside the ``bluesky`` directory that was created when installing a new bluesky instrument (see `setup a bluesky instrument `__). Note that the installation takes several minutes. .. raw:: html
    $ cd ~/bluesky
    $ conda env create \
        --force \
        -n bluesky_2023_3 \
        -f ./environments/environment_2023_3.yml \
        --solver=libmamba
    
Details
  • The --force option will replace any existing environment by this name without asking for confirmation. Remove this option if you wish.
  • The -n bluesky_2023_3 sets the name of the conda environment to be created.
  • The -f ./environments/environment_2023_3.yml option names the YAML file to be used. We create different versions of the YAML file, named for the APS operating cycle (2021-1, 2023-2, …), as the suite of packages for a working installation may change over time. By keeping all these files in the environments subdirectory, we can restore any of these environments with a simple command.
  • The --solver=libmamba option will use the conda-libmamba-solver. The --solver option can be removed but its use results in a much faster installation of the bluesky environment. The libmamba installation is described in the previous section.
Once finished, the installer will report the commands to manage the new environment: .. raw:: html
    #
    # To activate this environment, use
    #
    #     $ conda activate bluesky_2023_3
    #
    # To deactivate an active environment, use
    #
    #     $ conda deactivate
    
.. _reference.create_conda-bluesky_alias: Create an alias to activate the bluesky environment --------------------------------------------------- Creating a bash alias, an optional step, is like creating a custom shortcut. You can do this by editing the ``~/.bashrc`` and ``~/.bash_aliases`` files, which are configuration files for your bash shell. Here's a simple step-by-step guide: .. raw:: html
  1. Open a terminal.
  2. Open the ~/.bashrc and ~/.bash_aliases files with your prefered text editor, e.g.:
       $  gedit ~/.bashrc ~/.bash_aliases 
       
    If any of those files do not exist, this command will create blank ones.
  3. In ~/.bashrc, scroll down to the end of the file or find a suitable place to add the following lines:
       source ~/.bash_aliases
       
    Note: this line may already be included in your ~/.bashrc.
  4. In ~/.bash_aliases, scroll down to the end of the file or find a suitable place to add your alias. On a new line, type:
       export BLUESKY_CONDA_ENV=bluesky_2023_3
       alias become_bluesky='conda activate ${BLUESKY_CONDA_ENV}'
       
  5. Save your changes.
  6. Type bash and press enter, or open a new terminal windows to make the new alias available.
You can now use the alias become_bluesky to activate the bluesky environment. Other reading ------------- - `Getting started with conda `__ - `conda environments - User Guide `__ - `conda environments - Independent Guide `__ - `conda cheat sheet `__ - `Managing conda environments `__ - `difference between base environment and no environment at all `__ - ``venv``: `Python virtual environments `__ - `micromamba environments `__