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

Merging Git Repositories

No project of significant size that I've ever seen has retained its initial structure. Restructuring projects is a fact of life, but unfortunately Git doesn't make it easy. Fundamentally this stems from the way Git works, treating changes as a succession of snapshots and not storing any other metadata. Of course this is part of what makes Git fast and efficient, but at the expense of making some common operations more difficult for users. Git really is a perfect 21st century illustration of the classic  "Worse Is Better"  paradigm of successful software 😀 Previously I discussed how to split a Git project apart into separate repositories . Now I'm going to discuss how to do the opposite and merge separate repositories into one. On the face of it, this would seem a simpler task as Git has powerful support for merging... Let's take the opposite example to my splitting apart article - say you have a main Git repo (ProjA) and a second repo (ProjB) in...

Black Hole Marsh

We also visited Black Hole Marsh nature reserve, which is a large saltwater lagoon having two large bird hides, one in the middle of the lagoon  (Island Hide) and the other overlooking both the lagoon and the Axe Estuary (Tower Hide). There were many birds to be seen, but just as at the Seaton hide, a 400mm lens was not really enough to get frame-filling shots of the birds. All the following taken with Nikon D700, 70-200 f/2.8G VR II and TC-2E III converter (400mm focal length equivalent): Heron Fishing on the Lagoon with the Island Hide in the background Axe Estuary from the Tower Hide Pair of Bar-tailed Godwits Little Egret Mallards on the Lagoon Lapwing Fallow Deer  and Egret on the banks of the Axe Pied Wagtail on the thick green algae of Black Hole Marsh. Rat enjoying leftovers from the bird feeder

My Work in new Top Trumps Birds of Prey Pack

The new Top Trumps "Birds of Prey" pack has my picture illustrating the Secretary Bird card 😀 Here's the original picture: From Flickr