{"record":{"id":"8bbd91d3d6790c54","repo":"rohitg00/agentmemory","slug":"could-not-update-agentmemory-env-set-agentmem","errorCode":null,"errorMessage":"Could not update ~/.agentmemory/.env. Set AGENTMEMORY_INJECT_CONTEXT=true there to enable it.","messagePattern":"Could not update ~/\\.agentmemory/\\.env\\. Set AGENTMEMORY_INJECT_CONTEXT=true there to enable it\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/cli/onboarding.ts","lineNumber":298,"sourceCode":"  });\n\n  if (p.isCancel(enable)) {\n    p.cancel(\"Setup cancelled. Re-run any time with: agentmemory --reset\");\n    process.exit(0);\n  }\n\n  p.log.info(\n    \"Cost note: injection spends session tokens proportional to tool-call frequency. Default is off.\",\n  );\n\n  writePrefs({ injectContextChosen: true });\n\n  if (enable === true) {\n    const wrote = enableInjectContextInEnv(envPath);\n    if (wrote) {\n      p.log.success(\"Context injection enabled (AGENTMEMORY_INJECT_CONTEXT=true).\");\n    } else {\n      p.log.warn(\n        \"Could not update ~/.agentmemory/.env. Set AGENTMEMORY_INJECT_CONTEXT=true there to enable it.\",\n      );\n    }\n  } else {\n    p.log.info(\"Context injection left off. Set AGENTMEMORY_INJECT_CONTEXT=true later to enable.\");\n  }\n}\n\nasync function wireSelectedAgents(agents: string[]): Promise<void> {\n  p.note(\"Wire selected agents now?\", \"next step\");\n  const confirmed = await p.confirm({\n    message: \"Run `agentmemory connect <agent>` for each selected agent now? [Y/n]\",\n    initialValue: true,\n  });\n\n  if (p.isCancel(confirmed) || confirmed === false) {\n    const cmds = agents.map((a) => `  agentmemory connect ${a}`);\n    p.note([\"Wire later with:\", ...cmds].join(\"\\n\"), \"later\");","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/rohitg00/agentmemory/blob/e04ba88819c365c9acf9d6661ea802143e728bd6/src/cli/onboarding.ts#L280-L316","documentation":"During onboarding, when the user opts into context injection, agentmemory tries to write AGENTMEMORY_INJECT_CONTEXT=true into ~/.agentmemory/.env via enableInjectContextInEnv(). If that file cannot be read or written (permissions, lock, or malformed content), onboarding warns that it could not persist the setting and context injection remains off. The feature is not enabled until the variable exists in that env file.","triggerScenarios":"Running `agentmemory` onboarding (runOnboarding → maybePromptContextInjection) and answering yes to enable context injection while enableInjectContextInEnv() returns false — e.g. ~/.agentmemory/.env is read-only, owned by another user, or unwritable.","commonSituations":"Home directory permissions changed after running the installer with sudo; .env file owned by root; disk full; .agentmemory directory created by a different user; corporate-managed machines with restricted write access.","solutions":["Manually add AGENTMEMORY_INJECT_CONTEXT=true to ~/.agentmemory/.env (or create the file if missing).","Fix permissions on ~/.agentmemory/.env (e.g. chown to your user, chmod u+w) and re-run onboarding.","Re-run onboarding without sudo so the file is written as the normal user.","Export AGENTMEMORY_INJECT_CONTEXT=true in your shell profile as a fallback if the env file cannot be used."],"exampleFix":"// before: onboarding fails to write\nsudo agentmemory onboarding  # .env owned by root\n// after\nsudo chown $(whoami) ~/.agentmemory/.env\nagentmemory onboarding  # writes AGENTMEMORY_INJECT_CONTEXT=true successfully","handlingStrategy":"validation","validationCode":"import { accessSync, constants } from \"node:fs\";\nconst envPath = `${process.env.HOME}/.agentmemory/.env`;\ntry {\n  accessSync(envPath, constants.W_OK);\n} catch {\n  console.warn(\"~/.agentmemory/.env not writable; set AGENTMEMORY_INJECT_CONTEXT=true manually.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never run onboarding with sudo; keep ~/.agentmemory owned by your user.","Check .env writability before enabling context injection.","Keep AGENTMEMORY_INJECT_CONTEXT=true in shell profile as backup."],"tags":["onboarding","env-file","permissions","configuration"],"backgroundTag":"env-file-not-writable","analyzedSha":"e04ba88819c365c9acf9d6661ea802143e728bd6","analyzedAt":"2026-08-30T01:07:40.754Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}