2020-06-23 00:09:42 +05:30
---
stage: Package
group: Package
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#designated-technical-writers
---
2020-10-24 23:57:45 +05:30
# GitLab Conan Repository
2019-12-21 20:55:43 +05:30
2020-10-24 23:57:45 +05:30
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/8248) in [GitLab Premium](https://about.gitlab.com/pricing/) 12.6.
> - [Moved](https://gitlab.com/gitlab-org/gitlab/-/issues/221259) to GitLab Core in 13.3.
2019-12-21 20:55:43 +05:30
With the GitLab Conan Repository, every
project can have its own space to store Conan packages.
![GitLab Conan Repository ](img/conan_package_view.png )
## Enabling the Conan Repository
NOTE: **Note:**
This option is available only if your GitLab administrator has
2020-10-24 23:57:45 +05:30
[enabled support for the Conan Repository ](../../../administration/packages/index.md ).
2019-12-21 20:55:43 +05:30
2020-10-24 23:57:45 +05:30
After the Conan Repository is enabled, it's available for all new projects
2019-12-21 20:55:43 +05:30
by default. To enable it for existing projects, or if you want to disable it:
2020-07-28 23:09:34 +05:30
1. Navigate to your project's **Settings > General > Visibility, project features, permissions** .
2019-12-21 20:55:43 +05:30
1. Find the Packages feature and enable or disable it.
1. Click on **Save changes** for the changes to take effect.
2020-05-24 23:13:21 +05:30
You should then be able to see the **Packages & Registries** section on the left sidebar.
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
## Getting started
2019-12-21 20:55:43 +05:30
2020-10-24 23:57:45 +05:30
This section covers installing Conan and building a package for your C/C++ project. This is a quickstart if you're new
2020-03-13 15:44:24 +05:30
to Conan. If you already are using Conan and understand how to build your own packages, move on to the [next section ](#adding-the-gitlab-package-registry-as-a-conan-remote ).
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
### Installing Conan
Follow the instructions at [conan.io ](https://conan.io/downloads.html ) to download the Conan package manager to your local development environment.
Once installation is complete, verify you can use Conan in your terminal by running
```shell
conan --version
```
You should see the Conan version printed in the output:
2020-04-08 14:13:33 +05:30
```plaintext
2020-03-13 15:44:24 +05:30
Conan version 1.20.5
```
### Installing CMake
When developing with C++ and Conan, you have a wide range of options for compilers. This tutorial walks through using the cmake
compiler. In your terminal, run the command
```shell
cmake --version
```
You should see the cmake version printed in the output. If you see something else, you may have to install cmake.
On a Mac, you can use [homebrew ](https://brew.sh/ ) to install cmake by running `brew install cmake` . Otherwise, follow
instructions at [cmake.org ](https://cmake.org/install/ ) for your operating system.
### Creating a project
2020-10-24 23:57:45 +05:30
Understanding what is needed to create a valid and compilable C++ project is out of the scope of this guide, but if you're new to C++ and want to try out the GitLab
2020-03-13 15:44:24 +05:30
package registry, Conan.io has a great [hello world starter project ](https://github.com/conan-io/hello ) that you can clone to get started.
2020-10-24 23:57:45 +05:30
Clone the repository and it can be used for the rest of the tutorial if you don't have your own C++ project.
2020-03-13 15:44:24 +05:30
### Building a package
In your terminal, navigate to the root folder of your project. Generate a new recipe by running `conan new` and providing it with a
package name and version:
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
```shell
conan new Hello/0.1 -t
```
2020-10-24 23:57:45 +05:30
Next, create a package for that recipe by running `conan create` providing the Conan user and channel:
2020-03-13 15:44:24 +05:30
```shell
2020-11-24 15:15:51 +05:30
conan create . mycompany/beta
2020-03-13 15:44:24 +05:30
```
2020-07-28 23:09:34 +05:30
NOTE: **Note:**
2020-11-24 15:15:51 +05:30
If you are using the [instance level remote ](#instance-level-remote ), a specific [naming convention ](#package-recipe-naming-convention-for-instance-level-remote ) must be followed.
2020-03-13 15:44:24 +05:30
2020-11-24 15:15:51 +05:30
These two example commands generate a final package with the recipe `Hello/0.1@mycompany/beta` .
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
For more advanced details on creating and managing your packages, refer to the [Conan docs ](https://docs.conan.io/en/latest/creating_packages.html ).
2019-12-21 20:55:43 +05:30
2020-10-24 23:57:45 +05:30
You are now ready to upload your package to the GitLab registry. To get started, first you need to set GitLab as a remote. Then you need to add a Conan user for that remote to authenticate your requests.
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
## Adding the GitLab Package Registry as a Conan remote
2019-12-21 20:55:43 +05:30
2020-11-24 15:15:51 +05:30
You can add the GitLab Package Registry as a Conan remote at the project or instance level.
### Project level remote
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/11679) in GitLab 13.4.
The project level remote allows you to work with packages within a given project.
The advantage of using the project level remote is there are no restrictions to your
package name, however all GitLab Conan packages require a full recipe
with the user and channel (`package_name/version@user/channel`).
To add the remote:
```shell
conan remote add gitlab https://gitlab.example.com/api/v4/projects/< project_id > /packages/conan
```
Once the remote is set, you can use the remote when running Conan commands by adding `--remote=gitlab` to the end of your commands.
For example:
```shell
conan search Hello* --all --remote=gitlab
```
### Instance level remote
The instance level remote allows you to use a single remote to access packages accross your entire
GitLab instance. However, when using this remote, there are certain
[package naming restrictions ](#package-recipe-naming-convention-for-instance-level-remote )
that must be followed.
2020-03-13 15:44:24 +05:30
Add a new remote to your Conan configuration:
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
```shell
2019-12-21 20:55:43 +05:30
conan remote add gitlab https://gitlab.example.com/api/v4/packages/conan
```
2020-03-13 15:44:24 +05:30
Once the remote is set, you can use the remote when running Conan commands by adding `--remote=gitlab` to the end of your commands.
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
For example:
```shell
2020-11-24 15:15:51 +05:30
conan search 'Hello*' --remote=gitlab
2019-12-21 20:55:43 +05:30
```
2020-11-24 15:15:51 +05:30
#### Package recipe naming convention for instance level remote
The standard Conan recipe convention looks like `package_name/version@user/channel` ,
but if you're using the [instance level remote ](#instance-level-remote ), the recipe
`user` must be the plus sign (`+`) separated project path.
The following table shows some example recipes you can give your package based on
the project name and path.
| Project | Package | Supported |
| ---------------------------------- | ----------------------------------------------- | --------- |
| `foo/bar` | `my-package/1.0.0@foo+bar/stable` | Yes |
| `foo/bar-baz/buz` | `my-package/1.0.0@foo+bar-baz+buz/stable` | Yes |
| `gitlab-org/gitlab-ce` | `my-package/1.0.0@gitlab-org+gitlab-ce/stable` | Yes |
| `gitlab-org/gitlab-ce` | `my-package/1.0.0@foo/stable` | No |
NOTE: **Note:**
[Project level remotes ](#project-level-remote ) allow for more flexible package names.
2020-03-13 15:44:24 +05:30
## Authenticating to the GitLab Conan Repository
2019-12-21 20:55:43 +05:30
2020-10-24 23:57:45 +05:30
You need a personal access token or deploy token.
2020-05-24 23:13:21 +05:30
For repository authentication:
- You can generate a [personal access token ](../../../user/profile/personal_access_tokens.md ) with the scope set to `api` .
- You can generate a [deploy token ](./../../project/deploy_tokens/index.md ) with the scope set to `read_package_registry` , `write_package_registry` , or both.
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
### Adding a Conan user to the GitLab remote
2020-10-24 23:57:45 +05:30
Once you have a personal access token and have [set your Conan remote ](#adding-the-gitlab-package-registry-as-a-conan-remote ), you can associate the token with the remote so you don't have to explicitly add them to each Conan command you run:
2020-03-13 15:44:24 +05:30
```shell
2020-05-24 23:13:21 +05:30
conan user < gitlab_username or deploy_token_username > -r gitlab -p < personal_access_token or deploy_token >
2020-03-13 15:44:24 +05:30
```
2020-07-28 23:09:34 +05:30
NOTE: **Note:**
2020-03-13 15:44:24 +05:30
If you named your remote something other than `gitlab` , your remote name should be used in this command instead of `gitlab` .
2020-10-24 23:57:45 +05:30
From now on, when you run commands using `--remote=gitlab` , your username and password is automatically included in the requests.
2020-03-13 15:44:24 +05:30
2020-07-28 23:09:34 +05:30
NOTE: **Note:**
2020-10-24 23:57:45 +05:30
The personal access token is not stored locally at any moment. Conan uses JSON Web Tokens (JWT), so when you run this command, Conan requests an expirable token from GitLab using your token. The JWT does expire on a regular basis, so you need to re-enter your personal access token when that happens.
2020-03-13 15:44:24 +05:30
Alternatively, you could explicitly include your credentials in any given command.
For example:
```shell
2020-11-24 15:15:51 +05:30
CONAN_LOGIN_USERNAME=< gitlab_username or deploy_token_username > CONAN_PASSWORD=< personal_access_token or deploy_token > conan upload Hello/0.1@mycompany/beta --all --remote=gitlab
2020-03-13 15:44:24 +05:30
```
### Setting a default remote to your project (optional)
If you'd like Conan to always use GitLab as the registry for your package, you can tell Conan to always reference the GitLab remote for a given package recipe:
```shell
2020-11-24 15:15:51 +05:30
conan remote add_ref Hello/0.1@mycompany/beta gitlab
2020-03-13 15:44:24 +05:30
```
2020-07-28 23:09:34 +05:30
NOTE: **Note:**
2020-03-13 15:44:24 +05:30
The package recipe does include the version, so setting the default remote for `Hello/0.1@user/channel` will not work for `Hello/0.2@user/channel` .
This functionality is best suited for when you want to consume or install packages from the GitLab registry without having to specify a remote.
2020-10-24 23:57:45 +05:30
The rest of the example commands in this documentation assume that you've added a Conan user with your credentials to the `gitlab` remote and will not include the explicit credentials or remote option. With that said, be aware that any of the commands could be run without having added a user or default remote:
2020-03-13 15:44:24 +05:30
```shell
2020-05-24 23:13:21 +05:30
`CONAN_LOGIN_USERNAME=< gitlab_username or deploy_token_username > CONAN_PASSWORD=< personal_access_token or deploy_token > < conan command > --remote=gitlab
2020-03-13 15:44:24 +05:30
```
2019-12-21 20:55:43 +05:30
## Uploading a package
2020-11-24 15:15:51 +05:30
First you need to [create your Conan package locally ](https://docs.conan.io/en/latest/creating_packages/getting_started.html ).
NOTE: **Note:**
If you are using the [instance level remote ](#instance-level-remote ), a specific [naming convention ](#package-recipe-naming-convention-for-instance-level-remote ) must be followed.
2019-12-21 20:55:43 +05:30
2020-10-24 23:57:45 +05:30
Ensure you have a project created on GitLab and that the personal access token you're using has the correct permissions for write access to the container registry by selecting the `api` [scope ](../../../user/profile/personal_access_tokens.md#limiting-scopes-of-a-personal-access-token ).
2019-12-21 20:55:43 +05:30
You can upload your package to the GitLab Package Registry using the `conan upload` command:
2020-03-13 15:44:24 +05:30
```shell
2020-11-24 15:15:51 +05:30
conan upload Hello/0.1@mycompany/beta --all
2019-12-21 20:55:43 +05:30
```
## Installing a package
2020-03-13 15:44:24 +05:30
Conan packages are commonly installed as dependencies using the `conanfile.txt` file.
In your project where you would like to install the Conan package as a dependency, open `conanfile.txt` or create
an empty file named `conanfile.txt` in the root of your project.
Add the Conan recipe to the `[requires]` section of the file:
```ini
[requires]
2020-11-24 15:15:51 +05:30
Hello/0.1@mycompany/beta
2020-03-13 15:44:24 +05:30
[generators]
cmake
```
2020-04-08 14:13:33 +05:30
Next, create a build directory from the root of your project and navigate to it:
2020-03-13 15:44:24 +05:30
```shell
mkdir build & & cd build
```
Now you can install the dependencies listed in `conanfile.txt` :
```shell
conan install ..
```
NOTE: **Note:**
2020-10-24 23:57:45 +05:30
If you're trying to install the package you just created in this tutorial, not much will happen since that package
2020-03-13 15:44:24 +05:30
already exists on your local machine.
2019-12-21 20:55:43 +05:30
## Removing a package
There are two ways to remove a Conan package from the GitLab Package Registry.
- **Using the Conan client in the command line:**
2020-03-13 15:44:24 +05:30
```shell
conan remove Hello/0.2@user/channel --remote=gitlab
2019-12-21 20:55:43 +05:30
```
2020-10-24 23:57:45 +05:30
You need to explicitly include the remote in this command, otherwise the package is only removed from your
2020-03-13 15:44:24 +05:30
local system cache.
2019-12-21 20:55:43 +05:30
NOTE: **Note:**
2020-10-24 23:57:45 +05:30
This command removes all recipe and binary package files from the Package Registry.
2019-12-21 20:55:43 +05:30
- **GitLab project interface**: in the packages view of your project page, you can delete packages by clicking the red trash icons.
## Searching the GitLab Package Registry for Conan packages
The `conan search` command can be run searching by full or partial package name, or by exact recipe.
2020-10-24 23:57:45 +05:30
To search using a partial name, use the wildcard symbol `*` , which should be placed at the end of your search (for example, `my-packa*` ):
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
```shell
conan search Hello --all --remote=gitlab
conan search He* --all --remote=gitlab
2020-11-24 15:15:51 +05:30
conan search Hello/0.1@mycompany/beta --all --remote=gitlab
2019-12-21 20:55:43 +05:30
```
2020-10-24 23:57:45 +05:30
The scope of your search includes all projects you have permission to access, this includes your private projects as well as all public projects.
2019-12-21 20:55:43 +05:30
2020-04-22 19:07:51 +05:30
## Fetching Conan package information from the GitLab Package Registry
2019-12-21 20:55:43 +05:30
2020-10-24 23:57:45 +05:30
The `conan info` command returns information about a given package:
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
```shell
2020-11-24 15:15:51 +05:30
conan info Hello/0.1@mycompany/beta
2019-12-21 20:55:43 +05:30
```
2020-03-13 15:44:24 +05:30
## List of supported CLI commands
The GitLab Conan repository supports the following Conan CLI commands:
- `conan upload` : Upload your recipe and package files to the GitLab Package Registry.
- `conan install` : Install a conan package from the GitLab Package Registry, this includes using the `conanfile.txt` file.
- `conan search` : Search the GitLab Package Registry for public packages, and private packages you have permission to view.
2020-04-22 19:07:51 +05:30
- `conan info` : View the information on a given package from the GitLab Package Registry.
2020-03-13 15:44:24 +05:30
- `conan remove` : Delete the package from the GitLab Package Registry.
## Using GitLab CI with Conan packages
2020-06-23 00:09:42 +05:30
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/11678) in [GitLab Premium](https://about.gitlab.com/pricing/) 12.7.
2020-03-13 15:44:24 +05:30
2020-04-22 19:07:51 +05:30
To work with Conan commands within [GitLab CI/CD ](./../../../ci/README.md ), you can use
2020-03-13 15:44:24 +05:30
`CI_JOB_TOKEN` in place of the personal access token in your commands.
2020-10-24 23:57:45 +05:30
It's easiest to provide the `CONAN_LOGIN_USERNAME` and `CONAN_PASSWORD` with each
Conan command in your `.gitlab-ci.yml` file. For example:
2020-03-13 15:44:24 +05:30
2020-05-24 23:13:21 +05:30
```yaml
2020-03-13 15:44:24 +05:30
image: conanio/gcc7
create_package:
stage: deploy
script:
- conan remote add gitlab https://gitlab.example.com/api/v4/packages/conan
2020-10-24 23:57:45 +05:30
- conan new < package-name > /0.1 -t
- conan create . < group-name > +< project-name > /stable
- CONAN_LOGIN_USERNAME=ci_user CONAN_PASSWORD=${CI_JOB_TOKEN} conan upload < package-name > /0.1@< group-name > +< project-name > /stable --all --remote=gitlab
2020-03-13 15:44:24 +05:30
```
You can find additional Conan images to use as the base of your CI file
in the [Conan docs ](https://docs.conan.io/en/latest/howtos/run_conan_in_docker.html#available-docker-images ).