2020-05-20 05:08:11 -04:00
---
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-09-28 20:09:59 -04:00
# Conan packages in the Package Registry
2019-10-11 14:06:15 -04:00
2020-07-21 14:09:45 -04:00
> - [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-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
Publish Conan packages in your project’ s Package Registry. Then install the
packages whenever you need to use them as a dependency.
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
To publish Conan packages to the Package Registry, add the
Package Registry as a remote and authenticate with it.
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
Then you can run `conan` commands and publish your package to the Package Registry.
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
## Build a Conan package
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
This section explains how to install Conan and build a package for your C/C++ project.
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
If you already use Conan and know how to build your own packages, go to the [next section ](#add-the-package-registry-as-a-conan-remote ).
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
### Install Conan
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
Download the Conan package manager to your local development environment by following
the instructions at [conan.io ](https://conan.io/downloads.html ).
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
When installation is complete, verify you can use Conan in your terminal by running:
2019-12-19 10:07:55 -05:00
2020-01-30 10:09:15 -05:00
```shell
2019-12-19 10:07:55 -05:00
conan --version
```
2020-09-28 20:09:59 -04:00
The Conan version is printed in the output:
2019-12-19 10:07:55 -05:00
2020-02-28 22:07:51 -05:00
```plaintext
2019-12-19 10:07:55 -05:00
Conan version 1.20.5
```
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
### Install CMake
When you develop with C++ and Conan, you have a range of compilers to choose from.
This example uses the CMake compiler.
To install CMake:
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
- For Mac, use [homebrew ](https://brew.sh/ ) and run `brew install cmake` .
- For other operating systems, follow the instructions at [cmake.org ](https://cmake.org/install/ ).
When installation is complete, verify you can use CMake in your terminal by running:
2019-10-11 14:06:15 -04:00
2020-01-30 10:09:15 -05:00
```shell
2019-12-19 10:07:55 -05:00
cmake --version
```
2020-09-28 20:09:59 -04:00
The CMake version is printed in the output.
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
### Create a project
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
To test the Package Registry, you need a C++ project. If you don't already have one, you can clone the
Conan [hello world starter project ](https://github.com/conan-io/hello ).
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
### Build a package
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
To build a package:
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
1. Open a terminal and navigate to your project's root folder.
1. Generate a new recipe by running `conan new` with a package name and version:
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
```shell
conan new Hello/0.1 -t
```
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
1. Create a package for the recipe by running `conan create` with the Conan user and channel:
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
```shell
conan create . mycompany/beta
```
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
NOTE: **Note:**
If you use an [instance remote ](#add-a-remote-for-your-instance ), you must follow a specific [naming convention ](#package-recipe-naming-convention-for-instance-remotes ).
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
A package with the recipe `Hello/0.1@mycompany/beta` is created.
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
For more details on creating and managing Conan packages, see the [Conan docs ](https://docs.conan.io/en/latest/creating_packages.html ).
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
## Add the Package Registry as a Conan remote
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
To run `conan` commands, you must add the Package Registry as a Conan remote for your project or instance.
2020-09-15 17:09:35 -04:00
2020-09-28 20:09:59 -04:00
### Add a remote for your project
2020-09-15 17:09:35 -04:00
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/11679) in GitLab 13.4.
2020-09-28 20:09:59 -04:00
Set a remote so you can work with packages in a project without
having to specify the remote name in every command.
When you set a remote for a project, there are no restrictions to your package names.
However, your commands must include the full recipe, including the user and channel,
for example, `package_name/version@user/channel` .
2020-09-15 17:09:35 -04:00
To add the remote:
2020-09-28 20:09:59 -04:00
1. In your terminal, run this command:
2020-09-15 17:09:35 -04:00
2020-09-28 20:09:59 -04:00
```shell
conan remote add gitlab https://gitlab.example.com/api/v4/projects/< project_id > /packages/conan
```
2020-09-15 17:09:35 -04:00
2020-09-28 20:09:59 -04:00
1. Use the remote by adding `--remote=gitlab` to the end of your Conan command.
2020-09-15 17:09:35 -04:00
2020-09-28 20:09:59 -04:00
For example:
2020-09-15 17:09:35 -04:00
2020-09-28 20:09:59 -04:00
```shell
conan search Hello* --all --remote=gitlab
```
2020-09-15 17:09:35 -04:00
2020-09-28 20:09:59 -04:00
### Add a remote for your instance
2020-09-15 17:09:35 -04:00
2020-09-28 20:09:59 -04:00
Use a single remote to access packages across your entire GitLab instance.
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
However, when using this remote, you must follow these
[package naming restrictions ](#package-recipe-naming-convention-for-instance-remotes ).
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
To add the remote:
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
1. In your terminal, run this command:
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
```shell
conan remote add gitlab https://gitlab.example.com/api/v4/packages/conan
```
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
1. Use the remote by adding `--remote=gitlab` to the end of your Conan command.
2020-09-15 17:09:35 -04:00
2020-09-28 20:09:59 -04:00
For example:
```shell
conan search 'Hello*' --remote=gitlab
```
#### Package recipe naming convention for instance remotes
The standard Conan recipe convention is `package_name/version@user/channel` ,
but if you're using an [instance remote ](#add-a-remote-for-your-instance ), the recipe
2020-09-15 17:09:35 -04:00
`user` must be the plus sign (`+`) separated project path.
2020-09-28 20:09:59 -04:00
Example recipe names:
2020-09-15 17:09:35 -04:00
| 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 |
2020-09-28 20:09:59 -04:00
[Project remotes ](#add-a-remote-for-your-project ) have a more flexible naming convention.
## Authenticate to the Package Registry
To authenticate to the Package Registry, you need either a personal access token or deploy token.
2020-09-15 17:09:35 -04:00
2020-09-28 20:09:59 -04:00
- If you use a [personal access token ](../../../user/profile/personal_access_tokens.md ), set the scope to `api` .
- If you use a [deploy token ](./../../project/deploy_tokens/index.md ), set the scope to `read_package_registry` , `write_package_registry` , or both.
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
### Add your credentials to the GitLab remote
2020-05-15 20:08:12 -04:00
2020-09-28 20:09:59 -04:00
Associate your token with the GitLab remote, so that you don't have to explicitly
add a token to every Conan command.
2020-05-15 20:08:12 -04:00
2020-09-28 20:09:59 -04:00
Prerequisites:
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
- You must have an authentication token.
- The Conan remote [must be set ](#add-the-package-registry-as-a-conan-remote ).
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
In a terminal, run this command. In this example, the remote name is `gitlab` . Use the name of your remote.
2019-12-19 10:07:55 -05:00
2020-01-30 10:09:15 -05:00
```shell
2020-05-15 20:08:12 -04:00
conan user < gitlab_username or deploy_token_username > -r gitlab -p < personal_access_token or deploy_token >
2019-12-19 10:07:55 -05:00
```
2020-09-28 20:09:59 -04:00
Now when you run commands with `--remote=gitlab` , your username and password are automatically included in the requests.
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
Alternately, you can explicitly include your credentials in any given command. For example:
2019-12-19 10:07:55 -05:00
2020-01-30 10:09:15 -05:00
```shell
2020-09-15 17:09:35 -04:00
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
2019-12-19 10:07:55 -05:00
```
2020-09-28 20:09:59 -04:00
NOTE: **Note:**
Your authentication with GitLab expires on a regular basis,
so occasionally you may need to re-enter your personal access token.
### Set a default remote for your project (optional)
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
If you want to interact with the GitLab Package Registry without having to specify a remote,
you can tell Conan to always use the Package Registry for your packages.
In a terminal, run this command.
2019-12-19 10:07:55 -05:00
2020-01-30 10:09:15 -05:00
```shell
2020-09-15 17:09:35 -04:00
conan remote add_ref Hello/0.1@mycompany/beta gitlab
2019-12-19 10:07:55 -05:00
```
2020-07-15 05:09:34 -04:00
NOTE: **Note:**
2020-09-28 20:09:59 -04:00
The package recipe includes the version, so the default remote for `Hello/0.1@user/channel` does not work for `Hello/0.2@user/channel` .
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
If you do not set a default user or remote, you can still include the user and remote in your commands:
2019-12-19 10:07:55 -05:00
2020-01-30 10:09:15 -05:00
```shell
2020-05-15 20:08:12 -04:00
`CONAN_LOGIN_USERNAME=< gitlab_username or deploy_token_username > CONAN_PASSWORD=< personal_access_token or deploy_token > < conan command > --remote=gitlab
2019-12-19 10:07:55 -05:00
```
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
## Publish a Conan package
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
Publish a Conan package to the Package Registry, so that anyone who can access the project can use the package as a dependency.
2020-09-15 17:09:35 -04:00
2020-09-28 20:09:59 -04:00
Prerequisites:
To publish a Conan package, you need:
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
- The Package Registry [set as a remote ](#add-the-package-registry-as-a-conan-remote ).
- [Authentication ](#authenticate-to-the-package-registry ) set up with the Package Registry.
2020-10-08 05:08:40 -04:00
- A local [Conan package ](https://docs.conan.io/en/latest/creating_packages/getting_started.html ).
2020-09-28 20:09:59 -04:00
- For an instance remote, the package must meet the [naming convention ](#package-recipe-naming-convention-for-instance-remotes ).
- A project ID, which is on the project's homepage.
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
To publish the package, use the `conan upload` command:
2019-10-11 14:06:15 -04:00
2020-01-30 10:09:15 -05:00
```shell
2020-09-15 17:09:35 -04:00
conan upload Hello/0.1@mycompany/beta --all
2019-10-11 14:06:15 -04:00
```
2020-09-28 20:09:59 -04:00
## Publish a Conan package by using CI/CD
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/11678) in [GitLab Premium](https://about.gitlab.com/pricing/) 12.7.
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
To work with Conan commands in [GitLab CI/CD ](./../../../ci/README.md ), you can use
`CI_JOB_TOKEN` in place of the personal access token in your commands.
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
You can provide the `CONAN_LOGIN_USERNAME` and `CONAN_PASSWORD` with each
Conan command in your `.gitlab-ci.yml` file. For example:
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
```yaml
image: conanio/gcc7
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
create_package:
stage: deploy
script:
- conan remote add gitlab https://gitlab.example.com/api/v4/packages/conan
- 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
2019-12-19 10:07:55 -05:00
```
2020-09-28 20:09:59 -04:00
Additional Conan images to use as the basis of your CI file are available
in the [Conan docs ](https://docs.conan.io/en/latest/howtos/run_conan_in_docker.html#available-docker-images ).
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
## Install a Conan package
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
Install a Conan package from the Package Registry so you can use it as a dependency.
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
Conan packages are often installed as dependencies by using the `conanfile.txt` file.
Prerequisites:
To install a Conan package, you need:
- The Package Registry [set as a remote ](#add-the-package-registry-as-a-conan-remote ).
- [Authentication ](#authenticate-to-the-package-registry ) set up with the Package Registry.
1. In the project where you want to install the package as a dependency, open `conanfile.txt` .
Or, in the root of your project, create a file called `conanfile.txt` .
1. Add the Conan recipe to the `[requires]` section of the file:
2020-10-08 05:08:40 -04:00
```plaintext
[requires]
Hello/0.1@mycompany/beta
[generators]
cmake
2020-09-28 20:09:59 -04:00
```
1. At the root of your project, create a `build` directory and change to that directory:
```shell
mkdir build & & cd build
```
1. Install the dependencies listed in `conanfile.txt` :
```shell
conan install < options >
```
2019-12-19 10:07:55 -05:00
NOTE: **Note:**
2020-09-28 20:09:59 -04:00
If you try to install the package you just created in this tutorial, the package
already exists on your local machine, so this command has no effect.
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
## Remove a Conan package
2019-10-11 14:06:15 -04:00
There are two ways to remove a Conan package from the GitLab Package Registry.
2020-09-28 20:09:59 -04:00
- From the command line, using the Conan client:
2019-10-11 14:06:15 -04:00
2020-01-30 10:09:15 -05:00
```shell
2019-12-19 10:07:55 -05:00
conan remove Hello/0.2@user/channel --remote=gitlab
2019-10-11 14:06:15 -04:00
```
2020-01-07 22:08:05 -05:00
2020-09-28 20:09:59 -04:00
You must explicitly include the remote in this command, otherwise the package is only removed from your
2019-12-19 10:07:55 -05:00
local system cache.
2019-10-11 14:06:15 -04:00
NOTE: **Note:**
2020-08-09 20:10:20 -04:00
This command removes all recipe and binary package files from the Package Registry.
2019-10-11 14:06:15 -04:00
2020-10-08 05:08:40 -04:00
- From the GitLab user interface:
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
Go to your project's **Packages & Registries > Package Registry** . Remove the package by clicking the red trash icon.
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
## Search for Conan packages in the Package Registry
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
To search by full or partial package name, or by exact recipe, run the `conan search` command.
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
- To search for all packages with a specific package name:
```shell
conan search Hello --remote=gitlab
```
- To search for a partial name, like all packages starting with `He` :
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
```shell
conan search He* --remote=gitlab
```
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-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
## Fetch Conan package information from the Package Registry
2019-10-11 14:06:15 -04:00
2020-09-28 20:09:59 -04:00
The `conan info` command returns information about a package:
2019-10-11 14:06:15 -04:00
2020-01-30 10:09:15 -05:00
```shell
2020-09-15 17:09:35 -04:00
conan info Hello/0.1@mycompany/beta
2019-10-11 14:06:15 -04:00
```
2019-12-19 10:07:55 -05:00
2020-09-28 20:09:59 -04:00
## Supported CLI commands
2019-12-19 10:07:55 -05:00
The GitLab Conan repository supports the following Conan CLI commands:
2020-09-28 20:09:59 -04:00
- `conan upload` : Upload your recipe and package files to the Package Registry.
- `conan install` : Install a Conan package from the Package Registry, this includes using the `conanfile.txt` file.
- `conan search` : Search the Package Registry for public packages, and private packages you have permission to view.
- `conan info` : View the information on a given package from the Package Registry.
- `conan remove` : Delete the package from the Package Registry.