Ruby gems in the package registry
- Tier: Free, Premium, Ultimate
- Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated
- Status: Experiment
The availability of this feature is controlled by a feature flag. For more information, see the history. This feature is available for testing, but not ready for production use.
You can publish Ruby gems to your project’s package registry. Then, you can download them from the UI or with the API.
This feature is an experiment. For more information about the development of this feature, see epic 3200.
Authenticate to the package registry
Before you can interact with the package registry, you must authenticate to it.
To do this, you can use:
- A personal access token
with the scope set to
api. - A deploy token with the scope set to
read_package_registry,write_package_registry, or both. - A CI/CD job token.
For example:
To authenticate with an access token:
Create or edit your
~/.gem/credentialsfile, and add:--- https://gitlab.example.com/api/v4/projects/<project_id>/packages/rubygems: '<token>'
In this example:
<token>must be the token value of either your personal access token or deploy token.<project_id>is displayed on the project overview page.
To authenticate with a CI/CD job token:
Create or edit your
.gitlab-ci.ymlfile, and add:# assuming a my_gem.gemspec file is present in the repository with the version currently set to 0.0.1 image: ruby run: before_script: - mkdir ~/.gem - echo "---" > ~/.gem/credentials - | echo "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/rubygems: '${CI_JOB_TOKEN}'" >> ~/.gem/credentials - chmod 0600 ~/.gem/credentials # rubygems requires 0600 permissions on the credentials file script: - gem build my_gem - gem push my_gem-0.0.1.gem --host ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/rubygemsThe
gemclient uses theGEM_HOST_API_KEYenvironment variable before it reads~/.gem/credentials. If a CI/CD variable namedGEM_HOST_API_KEYexists, for example one inherited from a group,gem pushignores the job token and fails with401 Unauthorized. Remove that variable from the job, or setGEM_HOST_API_KEYto${CI_JOB_TOKEN}.You can also use
CI_JOB_TOKENin a~/.gem/credentialsfile you check in to GitLab:--- https://gitlab.example.com/api/v4/projects/${env.CI_PROJECT_ID}/packages/rubygems: '${env.CI_JOB_TOKEN}'
Push a Ruby gem
Prerequisites:
- You must authenticate to the package registry.
- Your Ruby gem must be 3 GB or less.
To do this:
Run a command like:
gem push my_gem-0.0.1.gem --host <host>In this example,
<host>is the URL you used when setting up authentication. For example:gem push my_gem-0.0.1.gem --host https://gitlab.example.com/api/v4/projects/1/packages/rubygems
When a gem is published successfully, a message like this is displayed:
Pushing gem to https://gitlab.example.com/api/v4/projects/1/packages/rubygems...
{"message":"201 Created"}The gem is published to your package registry, and is shown on the Packages and registries page. It can take up to 10 minutes before GitLab processes and displays your gem.
Protect gems
To control who can push or delete specific gems, add a package protection rule with the package type RubyGems.
If you push a protected gem without the required role, or with a deploy token,
the gem push command still returns 201 Created, because GitLab processes gems after upload.
GitLab then marks the new package with an error: Gem '<name>' with version '<version>' is protected.
Duplicate gems
By default, you can’t push a gem if a gem with the same name and version already exists in the project.
The gem push command still returns 201 Created, because GitLab processes gems after upload.
GitLab then marks the new package with an error: Gem '<name>' with version '<version>' already exists.
Prerequisites:
- You must have the Owner role for the group.
To allow duplicate gems for a group:
- In the top bar, select Search or go to and find your group.
- In the left sidebar, select Settings > Packages and registries.
- In the RubyGems row of the Duplicate packages table, turn on the Allow duplicates toggle. This toggle is off by default.
- Optional. In the Exceptions text box, enter a regular expression that matches the names and versions of gems.
- When Allow duplicates is off, gems that match the regex can be duplicated.
- When Allow duplicates is on, gems that match the regex can’t be duplicated.
Your changes are automatically saved.
You can also change this behavior with the
updateNamespacePackageSettings GraphQL mutation:
- To allow duplicate gems, set
rubygemsDuplicatesAllowedtotrue. - To make exceptions, set
rubygemsDuplicateExceptionRegexto a regular expression. The regex is matched against the gem name and version.- When duplicates are not allowed, gems that match the regex can be duplicated.
- When duplicates are allowed, gems that match the regex can’t be duplicated.
The setting applies to projects that belong directly to the group.
Delete a Ruby gem
Prerequisites:
- You must have the Maintainer or Owner role.
Before you delete a package, make sure you understand the associated security risks.
To delete a package, you can either:
Install a Ruby gem
Prerequisites:
- You must authenticate to the package registry.
To install a gem, pass your credentials in the source URL as HTTP Basic authentication.
Use __token__ as the username and your personal access token, deploy token, or CI/CD job
token as the password.
In the following examples:
<token>is your personal access token, deploy token, or CI/CD job token.<project_id>is displayed on the project overview page.
Run a command like:
gem install my_gem --source "https://__token__:<token>@gitlab.example.com/api/v4/projects/<project_id>/packages/rubygems"In your Gemfile, add a source block:
source "https://__token__:<token>@gitlab.example.com/api/v4/projects/<project_id>/packages/rubygems" do
gem "my_gem"
endDownload a gem file
To download a gem file for local use without installing it:
- In the top bar, select Search or go to and find your project.
- In the left sidebar, select Deploy > Package registry.
- Select the package name and version.
- Under Assets, select the Ruby gem you want to download.
To download a gem file, you can also use the API.