How to Generate UUIDs in Ruby

Generate UUIDs in Ruby with SecureRandom.uuid for v4 and a dedicated gem when you need UUID v7.

Recommendation: use UUID v4 for broad compatibility, and add UUID v7 through a gem when chronological ordering matters.

How to generate UUID v4 in Ruby

require 'securerandom'

id = SecureRandom.uuid
puts id

How to generate UUID v7 in Ruby

# gem install uuid7
require 'uuid7'

id = UUID7.generate
puts id

Native support notes

  • Ruby commonly uses UUID naming in Rails and backend service code.
  • UUID v4 is simple through the standard library.
  • UUID v7 support usually comes from a gem rather than the stdlib.

Practical notes

  • Keep one gem version pinned if multiple services generate UUID v7.
  • UUID v4 remains the lower-risk option for broad interoperability.
  • Check UUID v7 if you are optimizing for sortability.

Frequently Asked Questions

Yes. In common Ruby usage, SecureRandom.uuid is the standard random UUID choice.

In most Ruby environments, UUID v7 is added through a gem rather than through the standard library.

Learn more

These articles expand on related concepts, formats and practical considerations.

By using this site, you agree to our Privacy Policy and Terms of Service. You are not permitted to use the GUIDs (also known as UUIDs) generated by this site or use any other content, services and information available if you do not agree to these terms.
Disclaimer: All information is provided for general educational and technical reference only. While we aim to keep the content accurate, current and aligned with published standards, no guarantees are made regarding completeness, correctness or suitability for any specific use case.
GUID specifications, best practices, security guidance, database behavior and ecosystem conventions (including cloud platforms and identifier formats) may change over time or differ by implementation. Examples, recommendations and comparisons are illustrative and may not apply universally.
This content should not be considered legal, security, compliance or architectural advice. Before making critical design, security or production decisions, always consult the latest official standards and vendor-specific documentation.
Always evaluate behavior in your own environment.
Standards Compliance: The GUIDs generated by this site conform to RFC 4122 and RFC 9562 specifications whenever possible, using cryptographically secure random number generation.