Skip to main content

Setting Environment Variables and the PATH on MacOS

Time Was setting environment variables and the PATH on a Mac running OSX was just like any other *IX.  However with successive OS releases Apple have changed how this works (more than once) and generally made it more difficult 😢  This article discusses how I go about setting environment variables on Mojave and Catalina.

Why does this matter?

MacOS doesn't add /usr/local/bin to the PATH by default, which is unfortunate as most *IX-style programs you build yourself will be installed in there.

If you only ever launch stuff from Terminal, all you have to do is set environment variables and the path from Shell startup files in the time immemorial fashion.

However, this doesn't help with native Mac Apps like Emacs, which aren't launched from a shell and where you may still want to access custom environment variables and programs in /usr/local/bin.

Setting the PATH

In the past you could add to the path via /etc/paths (or paths.d), however this no longer works in Mojave or Catalina.

There is the cunning exec-path-from-shell which launches a sub-shell and copies the path and environment variables from the shell into Emacs. That's great if you're just using Emacs, but obviously won't work with anything else.

I prefer a solution which will work for all apps and it is possible to set the path for the current user with the launchctl command:

$ sudo launchctl config user path /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Note that you only have to issue this command once.

Setting Environment Variables

I think the easiest way is to create a plist file in ~/Library/LaunchAgents to invoke launchctl and set environment variables on login, like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>my-env-vars</string>
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>-c</string>
<string>launchctl setenv BASH_SILENCE_DEPRECATION_WARNING 1
        </string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

Comments

Popular posts from this blog

Renaming Files in Git

Renaming Files I recently had to rename a lot of files in brf-mode for submission into MELPA . These files have 10+ years of version control history I wanted to keep. In the process I realised retaining all that history in Git isn't as simple as I thought 😀 I thought it was as simple as using git mv rather than filesystem  mv and Git would know everything had been renamed. It turns out I was wrong and I should have known that from my knowledge of how Git works 😖 In reality, Git works by storing snapshots rather than file or directory metadata and git mv is just a convenience shortcut for typing: $ mv <old name> <new name> $ git rm <old name> $ git add <new name> That's all there is to it! Now the "magic" happens when you git log or git blame a file and Git works out the file was renamed by diffing the contents.  If files have the same contents (within a certain threshold %) it thinks a rename happened. See here in the  Gi

More Axe Birdwatching

We went back to the Seaton bird hide the next day, there were plenty of birds but the weather was poor. Cold and very dark and not at all suited to photography. Here's the best I managed (a ll taken with Nikon D700, 70-200 f/2.8G VR II and TC-2E III converter (400mm focal length equivalent): Male Pheasant Enjoying Leftovers from Bird Feeder Little Grebe  (I got quite close to this, but it's a very small bird!) Cormorants Flying along the Axe