Blog ·
Generate UUID on the Command Line, in Python and Node.js
Generate a UUID v4 from the command line with uuidgen, or in Python with uuid.uuid4() and in Node.js with crypto.randomUUID(). No external libraries needed.
UUID Generator
Generate secure UUID v4 and time-ordered UUID v7 identifiers.
To generate a UUID on the command line, run uuidgen on macOS or most Linux distributions. It prints a random version 4 UUID such as 3f2b8c1e-9a4d-4e6f-8b7a-1c2d3e4f5a6b. In Python, use uuid.uuid4(). In Node.js, use crypto.randomUUID(). None of these need third-party packages.
If you need a quick value without touching a terminal, the UUID Generator on DevRivet produces v4 and v7 UUIDs in the browser, and it can bulk generate many at once. The rest of this post shows how to do it in your own scripts.
Generate a UUID with uuidgen
uuidgen is part of the util-linux package on most Linux distributions and ships with macOS. Run it with no arguments:
uuidgen
Sample output:
9b1d7e52-4c3a-4f8e-a2d6-0e5f7b8c9d1a
The output is uppercase on macOS and may be lowercase on Linux. UUIDs are case-insensitive, so both forms are valid, but a string comparison will fail if you mix them. Normalize with tr if needed:
uuidgen | tr 'A-Z' 'a-z'
To generate several UUIDs, loop over a sequence:
for i in $(seq 1 5); do uuidgen; done
If uuidgen is missing on a minimal Linux image, install it with your package manager (for example, the uuid-runtime package on Debian and Ubuntu). Alternatively, read from the kernel directly, which works on any Linux system:
cat /proc/sys/kernel/random/uuid
That path exists only on Linux. On macOS, use uuidgen.
Generate a UUID in Python
The standard library uuid module includes uuid4(), which returns a UUID object. Convert it to a string with str():
import uuid value = str(uuid.uuid4()) print(value)
Sample output:
6a0f3c2e-8d1b-4c7a-9e5f-2b3d4c5e6f70
Some useful variants:
- uuid.uuid4().hex returns 32 hex characters with no hyphens, such as 6a0f3c2e8d1b4c7a9e5f2b3d4c5e6f70.
- uuid.uuid4().urn returns the form urn:uuid: followed by the canonical string.
- Use str(uuid.uuid4()) when you need the standard hyphenated, lowercase form for logs, database keys or JSON.
Note that uuid.uuid4() returns a UUID object, not a string. If you concatenate it directly with a string, you get a TypeError. Wrap it in str() first.
If you need version 7 values, the standard library call depends on your Python version, so check the uuid module documentation for your interpreter. The UUID Generator also produces v7 UUIDs without any code.
Generate a UUID in Node.js
The built-in crypto module provides crypto.randomUUID(), which returns a lowercase, hyphenated version 4 UUID:
const { randomUUID } = require('crypto');
console.log(randomUUID());In an ES module, import it instead:
import { randomUUID } from 'node:crypto';
console.log(randomUUID());In browsers, crypto.randomUUID() is available only in a secure context, which means HTTPS or localhost. On a plain http:// page served from another host, the function is unavailable and calling it fails. If you hit this during local development, serve the page over localhost or HTTPS. If you must support an environment without the function, you can build a v4 UUID from crypto.getRandomValues(), but avoid Math.random() for identifiers.
Bulk generation
For a one-off batch, a short loop is enough. In Python:
import uuid
for _ in range(10):
print(uuid.uuid4())In Node.js:
const { randomUUID } = require('crypto');
const ids = Array.from({ length: 10 }, () => randomUUID());
console.log(ids.join('\n'));When you need a large list to paste into a fixture file or spreadsheet and do not want to write a script, the bulk mode in the UUID Generator covers that case. Generated test data is a good fit for this. Treat the output with care in other cases. A UUID that acts as a secret, such as a password reset link or an access token, should be handled like any other secret. Do not paste production identifiers you want to keep private into any online tool. DevRivet's tools run in your browser, but the safest habit is to generate test values rather than copy real ones.
Which version do you get?
All three methods above produce version 4 UUIDs, which are 122 random bits with fixed version and variant bits. Version 4 is a good default when you only need uniqueness. If you want values that sort roughly by creation time, which helps with database index locality, look at version 7. The UUID v4 vs v7 guide explains the trade-off.
For a sense of how unlikely collisions are with random v4 values, see UUID collision probability explained.
Common pitfalls
- Using uuidgen on Windows: it is not installed by default. Use PowerShell with [guid]::NewGuid() or run the commands above inside WSL.
- Comparing UUIDs case-sensitively: normalize to lowercase before comparing strings or using them as map keys.
- Calling crypto.randomUUID() in a non-secure browser context: serve the page over HTTPS or localhost.
- Forgetting str() in Python when building strings or JSON. json.dumps() will fail on a raw UUID object.
- Using Math.random() or other non-cryptographic generators to build identifiers that other users or systems might guess.
FAQ
Is a UUID generated by uuidgen cryptographically secure?
uuidgen output is suitable for identifiers, such as record keys, request IDs and correlation IDs. For secrets such as session tokens, API keys or password reset links, use a dedicated secure token generator with enough length and entropy rather than relying on a UUID's format.
Can I get a UUID in a single line without installing anything in Python?
Yes. Run python3 -c "import uuid; print(uuid.uuid4())". This works anywhere Python 3 is installed.
Does crypto.randomUUID() return uppercase or lowercase?
It returns lowercase hexadecimal digits with hyphens in the standard 8-4-4-4-12 layout.
Should I store UUIDs as strings or binary?
Strings are easier to read and debug. Binary storage saves space and can be faster in some databases. Pick based on your database and index needs, and be consistent across services.
UUID Generator
Generate secure UUID v4 and time-ordered UUID v7 identifiers.