Good Config for pipline

I want to have a good setup for a Gitlab CI pipeline.
I need a lot of apt packages to be installed and now I use a before_script to do this but then this happens on every stage in the pipeline. How can I get my pipeline to do this installation of packages just once?

I tried the following in a build stage and than add a cache on cache/apt:

apt-get -o dir::cache::archives="cache/apt" install

But that did not work the installation of the dependencies is done all over in the next stage.

Example .gitlab-ci.yml:

image: "ubuntu:16.04"
stages:
  - build
  - test

cache:
  paths:
  - cache/apt

setup:
  stage: setup
  script:
    - apt-get -o dir::cache::archives="/cache/apt" install -y -qq --force-yes list-of-packages

test:
  stage: test
  script:
    - test scripts