Permissions dfx cli on OSX

I followed article below to install the SDK on my Macbook (OSX Monterey v12.4).

> dfx --version
dfx 0.12.1

But cannot run successfully the dfx CLI without prefixing it with sudo.

> yarn start
Password:
yarn run v1.22.18
warning package.json: No license field
$ npm run generate

> hello_frontend@0.1.0 generate
> dfx generate hello_backend

Error: Failed to create AgentEnvironment.
Caused by: Failed to create AgentEnvironment.
  Failed to create AgentEnvironment for network 'local'.
    Failed to instantiate selected identity.
      Failed to instantiate identity with name 'USER_NAME'.
        Failed to load identity 'USER_NAME'.
          Failed to load pem file /Users/CURRENT_USER/.config/dfx/identity/USER_NAME/identity.pem.encrypted.
            Failed to read /Users/CURRENT_USER/.config/dfx/identity/USER_NAME/identity.pem.encrypted.
              Permission denied (os error 13)
error Command failed with exit code 255.

And I found discussions below.

I solved this problem with chown .config/dfx.

> ls -l ~/.config
total 0
drwxr-xr-x  4 root    128 Jan 27 17:47 dfx

> sudo chown -R $(whoami) ~/.config/dfx

FYI dfx needs permission to generate identity.

> dfx identity new USER_NAME
Please enter a passphrase for your identity: [hidden]
Encryption complete.
Error: Failed to create new identity 'USER_NAME'.
Caused by: Failed to create new identity 'USER_NAME'.
  Failed to write pem file.
    Failed to create /Users/CURRENT_USER/.config/dfx/identity/___temp___USER_NAME.
      Permission denied (os error 13)