diff --git a/dedal/spack_factory/SpackOperation.py b/dedal/spack_factory/SpackOperation.py
index ca7175721040e77429f232852b5bcb469f70251f..8eaf3ef54a584147a81377ac5d0f20f9aedf97e0 100644
--- a/dedal/spack_factory/SpackOperation.py
+++ b/dedal/spack_factory/SpackOperation.py
@@ -389,20 +389,21 @@ class SpackOperation:
         else:
             self.logger.debug("Spack already cloned.")
 
-        # ensure the file exists before opening it
-        if not os.path.exists(bashrc_path):
-            open(bashrc_path, "w").close()
-        # add spack setup commands to .bashrc
-        with open(bashrc_path, "a") as bashrc:
-            bashrc.write(f'export PATH="{self.spack_dir}/bin:$PATH"\n')
-            spack_setup_script = f"source {self.spack_dir / 'share' / 'spack' / 'setup-env.sh'}"
-            bashrc.write(f"{spack_setup_script}\n")
-        self.logger.info("Added Spack PATH to .bashrc")
+        if bashrc_path:
+            # ensure the file exists before opening it
+            if not os.path.exists(bashrc_path):
+                open(bashrc_path, "w").close()
+            # add spack setup commands to .bashrc
+            with open(bashrc_path, "a") as bashrc:
+                bashrc.write(f'export PATH="{self.spack_dir}/bin:$PATH"\n')
+                spack_setup_script = f"source {self.spack_dir / 'share' / 'spack' / 'setup-env.sh'}"
+                bashrc.write(f"{spack_setup_script}\n")
+            self.logger.info("Added Spack PATH to .bashrc")
         if user:
             run_command("chown", "-R", f"{user}:{user}", self.spack_dir, check=True, logger=self.logger,
                         info_msg='Adding permissions to the logged in user')
         self.logger.info("Spack install completed")
-        if self.spack_config.use_spack_global is True:
+        if self.spack_config.use_spack_global is True and bashrc_path is not None:
             # Restart the bash only of the spack is used globally
             self.logger.info('Restarting bash')
             run_command("bash", "-c", f"source {bashrc_path}", check=True, logger=self.logger, info_msg='Restart bash')