Niklas
Niklas

Reputation: 4203

How to apt-get install in a GitHub Actions workflow?

In the new GitHub Actions, I am trying to install a package in order to use it in one of the next steps.

name: CI

on: [push, pull_request]

jobs:
  translations:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
      with:
        fetch-depth: 1
    - name: Install xmllint
      run: apt-get install libxml2-utils
    # ...

However this fails with

Run apt-get install libxml2-utils
  apt-get install libxml2-utils
  shell: /bin/bash -e {0}
E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
##[error]Process completed with exit code 100.

What's the best way to do this? Do I need to reach for Docker?

Upvotes: 325

Views: 136186

Answers (3)

Sanjay Bharwani
Sanjay Bharwani

Reputation: 4739

The error is hinting the problem

E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)

Its a permission issue. So nice and simple, run it with sudo.

Sample example below

jobs:
  test:
    name: Unit test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: ⚙️ Install lcov
        run: |
          sudo apt-get update
          sudo apt-get -y install lcov

Here the github action is installing lcov a tool to perform code coverage.

Upvotes: 2

congusbongus
congusbongus

Reputation: 14622

Please see this answer here: https://stackoverflow.com/a/73500415/2038264

cache-apt-pkgs-action can both install and cache the apt package, so your subsequent builds are fast. It is also easier to configure, just add the packages you want:

      - uses: awalsh128/cache-apt-pkgs-action@latest
        with:
          packages: dia doxygen doxygen-doc doxygen-gui doxygen-latex graphviz mscgen
          version: 1.0

Upvotes: 28

rmunn
rmunn

Reputation: 36658

The docs say:

The Linux and macOS virtual machines both run using passwordless sudo. When you need to execute commands or install tools that require more privileges than the current user, you can use sudo without needing to provide a password.

So simply doing the following should work:

- name: Install xmllint
  run: sudo apt-get install -y libxml2-utils

Upvotes: 457

Related Questions