uuidgen on macOS

Warning: This post is over a year old. The information may be out of date.

Thanks to David McKay, I learned about a really cool command for generating UUIDs that I hadn’t heard of before, but on macOS it wasn’t quite right, so I resorted to using Docker.

Credit to David McKay first

After seeing the following tweet from David McKay and learning about uuidgen - I tried it out on macOS (Sierra 10.12.1 is what I had)

uuidgen worked! Thanks, David!

brunty@Nova [~]
> uuidgen
8F0D36C0-24EC-4539-99D9-B214B2160958

But what version of a UUID is this?

According to the UUID generated, it’s a Version 4 UUID:

xxxxxxxx-xxxx-Vxxx-xxxx-xxxxxxxxxxxx

V indicates the version of the UUID - see the Wikipedia article above for more info, alternatively, check out the RFC (4122) here.

You can also verify this (as well as create UUIDs on the command line of various types) with ramsey/uuid-console

brunty@Nova [~]
> uuid decode 8F0D36C0-24EC-4539-99D9-B214B2160958
 ========= ========== =================================================
  encode:   STR:       8f0d36c0-24ec-4539-99d9-b214b2160958
            INT:       190148213729756816266206310110370072920
  decode:   variant:   RFC 4122
            version:   4 (random data based)
            content:   8f:0d:36:c0:24:ec:45:39:99:d9:b2:14:b2:16:09:58
                       (no semantics: random data only)
 ========= ========== =================================================

But I want it lower case!

If you want it lower case, you can do the following:

brunty@Nova [~]
> uuidgen | awk '{print tolower($0)}'
73476378-dfa6-44e0-bfbb-b2d9c53fc421

So what options are available for uuidgen?

I went to check a manual page for uuidgen:

Note: the OS X link is for an older version (before it was referred to as macOS) - but the funcionality seems to still be the same on the current version.

They appear to be separate things, and what if you wanted that -r flag, which OS X / macOS didn’t have?

-r
    Generate a random-based UUID. This method creates a UUID consisting mostly of random bits.
    It requires that the operating system have a high quality random number generator, such as /dev/random.

I don’t have access to uuidgen

If you don’t have access, you could craete an alias after installing the ramsey/uuid-console library above:

brunty@Nova [~]
> composer global require ramsey/uuid-console

brunty@Nova [~]
> alias uuidgen='uuid generate 4'

brunty@Nova [~]
> uuidgen
efd0159c-fd03-4ea2-a2d5-634ef731336c

But that requires having PHP (and the bcmath extension) installed and working locally.

Docker to the rescue?

I discovered an image andyneff/uuidgen that gave me uuidgen

brunty@Nova [~]
> docker run --rm andyneff/uuidgen
7dfedeab-63ff-4ef2-a186-07cccc039427

So I ran the following:

brunty@Nova [~]
> docker run --rm andyneff/uuidgen uuidgen -r
0d18834d-0e3a-42d7-bee2-8c5021c01b79

That seems to work! (though I have no real way to verify if it’s using something like /dev/random in the box under the surface - any ideas anyone?)

Now I can alias that to uuidgen. If you want to specify -r yourself:

brunty@Nova [~]
> alias uuidgen='docker run --rm andyneff/uuidgen uuidgen'

brunty@Nova [~]
> uuidgen -r
5a1c74f9-b144-4888-acad-092c127e2516

Or if you just want -r to be a part of the alias without any extra options:

brunty@Nova [~]
> alias uuidgen='docker run --rm andyneff/uuidgen uuidgen -r'

brunty@Nova [~]
> uuidgen
228a0451-19cb-4392-81f3-0633cdcbf064