diff --git a/platforms/common/spack_utils.sh b/platforms/common/spack_utils.sh
index e441424149e79c491b1061b9c78d00aa9dac0dc8..0976792d521b368a7a239d6a9a1bba9477c42056 100644
--- a/platforms/common/spack_utils.sh
+++ b/platforms/common/spack_utils.sh
@@ -6,26 +6,19 @@ function _install_spack() {
 
   _init_spack "$SPACK_INIT_CMD" "$SPACK_ROOT"
 
-  # Adding a lock file to prevent race conditions.
-  UPSTREAMS_LOCK=${SPACK_ROOT}/etc/spack/upstreams.lock
-  while [ -f "${UPSTREAMS_LOCK}" ]; do
-    sleep 1
-  done
-
   if [ ! -z "${SPACK_UPSTREAMS}" ]; then
-    touch ${UPSTREAMS_LOCK}
     UPSTREAMS_CONFIG=${SPACK_ROOT}/etc/spack/upstreams.yaml
-    echo "upstreams:" > $UPSTREAMS_CONFIG
+    tmpfile=$(mktemp)
+
+    echo "upstreams:" > $tmpfile
     i=0
     for UP in ${SPACK_UPSTREAMS}; do
       echo "Adding upstream repo to config $UPSTREAMS_CONFIG <= $UP"
       i=$((i+1))
-      cat << EOF >> $UPSTREAMS_CONFIG
-  spack-instance-$i:
-      install_tree: $UP
-EOF
+      echo "  spack-instance-$i:" >> $tmpfile
+      echo "    install_tree: $UP" >> $tmpfile
     done
-    rm ${UPSTREAMS_LOCK}
+    mv $tmpfile $UPSTREAMS_CONFIG
   fi
 
   if [ ! -z "${SPACK_EXTERNALS}" ] ; then