Skip to content

Instantly share code, notes, and snippets.

@wdullaer
Last active March 17, 2024 08:12
Show Gist options
  • Save wdullaer/f1af16bd7e970389bad3 to your computer and use it in GitHub Desktop.
Save wdullaer/f1af16bd7e970389bad3 to your computer and use it in GitHub Desktop.
Install Latest Docker and Docker-compose on Ubuntu
# Ask for the user password
# Script only works if sudo caches the password for a few minutes
sudo true
# Install kernel extra's to enable docker aufs support
# sudo apt-get -y install linux-image-extra-$(uname -r)
# Add Docker PPA and install latest version
# sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
# sudo sh -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
# sudo apt-get update
# sudo apt-get install lxc-docker -y
# Alternatively you can use the official docker install script
wget -qO- https://get.docker.com/ | sh
# Install docker-compose
COMPOSE_VERSION=`git ls-remote https://github.com/docker/compose | grep refs/tags | grep -oE "[0-9]+\.[0-9][0-9]+\.[0-9]+$" | sort --version-sort | tail -n 1`
sudo sh -c "curl -L https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose"
sudo chmod +x /usr/local/bin/docker-compose
sudo sh -c "curl -L https://raw.githubusercontent.com/docker/compose/${COMPOSE_VERSION}/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose"
# Install docker-cleanup command
cd /tmp
git clone https://gist.github.com/76b450a0c986e576e98b.git
cd 76b450a0c986e576e98b
sudo mv docker-cleanup /usr/local/bin/docker-cleanup
sudo chmod +x /usr/local/bin/docker-cleanup
@Jarzka
Copy link

Jarzka commented Sep 11, 2016

I wonder why there is no easier way than searching a custom script from GitHub to install the latest version of Docker Compose...

@wting
Copy link

wting commented Sep 13, 2016

FYI I've found docker-gc a more robust substitute for docker-cleanup.

@algotrader-dotcom
Copy link

curl -sSL https://get.docker.com/ | sudo bash

@ivorscott
Copy link

@Jarzka because its bleeding edge

@devilrancy
Copy link

Thank you buddy for this script.

@xtdwps
Copy link

xtdwps commented Jan 25, 2017

Regular expression for latest docker-compose releases (X.XX.X) should be fixed like:
"[0-9]+\.[0-9][0-9]+\.[0-9]+$"

@wdullaer
Copy link
Author

wdullaer commented Mar 3, 2017

@xtdwps thanks, updated

@DerekRoberts
Copy link

DerekRoberts commented Mar 4, 2017

That regex requires two digits in the centre position. Here's a more flexible solution, but it's a mess. Anything better?

LIST=$( git ls-remote https://github.com/docker/compose | grep "refs/tags" | grep -oP "[0-9]+(\.[0-9]+)+$" )
VERSION=""
while [ $( echo "${LIST}" | wc -l ) -gt 1 ]
do
    DIGIT=$( echo "${LIST}" | grep -oP "^[0-9]+" | sort -g | tail -n 1 )
    VERSION="${VERSION}"."${DIGIT}"
    LIST=$( echo "${LIST}" | sed -e "/^${DIGIT}/!d" -e "s/^${DIGIT}.//" )
done
VERSION=$( echo "${VERSION}" | sed "s/^\.//" )

@felipebizz
Copy link

Thanks a lot!

@patocardo
Copy link

¡¡Thank you very, very much!!

@MohamedHegab
Copy link

Thanks for this great script 👍

@n0ts
Copy link

n0ts commented Apr 26, 2017

Hi, I write a more simple script.

VERSION=$(curl -s -o /dev/null -I -w "%{redirect_url}\n" https://github.com/docker/compose/releases/latest | grep -oP "[0-9]+(\.[0-9]+)+$")
curl -L https://github.com/docker/compose/releases/download/$COMPOSE_VER/docker-compose-$(uname -s)-$(uname -m) > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version

@rachelcarmena
Copy link

Thanks @n0ts!

There is an small typo:
COMPOSE_VER=$(curl -s -o /dev/null -I -w "%{redirect_url}\n" https://github.com/docker/compose/releases/latest | grep -oP "[0-9]+(\.[0-9]+)+$")

and second command could be:

curl -o /usr/local/bin/docker-compose -L https://github.com/docker/compose/releases/download/$COMPOSE_VER/docker-compose-$(uname -s)-$(uname -m)

I hope it's useful!

@EgorOmelyanenko
Copy link

Thank you very much!

@alexanderadam
Copy link

As the current installation process of docker compose is pretty creepy you may be interested in these issues: 2235 and 24210.
But at least there's a Docker / Moby PPA for Docker / Moby itself…

@natgho-reidin
Copy link

natgho-reidin commented Aug 1, 2017

@madtechsupport here;

#!/bin/bash

# Uninstall Script

if [ "${USER}" != "root" ]; then
    echo "$0 must be run as root!"
    exit 2
fi

while true; do
  read -p "Remove all Docker Machine VMs? (Y/N): " yn
  case $yn in
    [Yy]* ) docker-machine rm -f $(docker-machine ls -q); break;;
    [Nn]* ) break;;
    * ) echo "Please answer yes or no."; exit 1;;
  esac
done

echo "Removing Applications..."
rm -rf /Applications/Docker

echo "Removing docker binaries..."
rm -f /usr/local/bin/docker
rm -f /usr/local/bin/docker-machine
rm -r /usr/local/bin/docker-machine-driver*
rm -f /usr/local/bin/docker-compose

echo "Removing boot2docker.iso"
rm -rf /usr/local/share/boot2docker

echo "All Done!"

@joeyhub
Copy link

joeyhub commented Aug 25, 2017

This is not a recommended way to install if it you can avoid it. If you want to install it on debian you should package it into a deb. You can look at the source deb for it and try to up date that. The project might also contain a build script for deb somewhere.

@cpeeyush
Copy link

Awesome Thanks.

@komputronika
Copy link

Thanks for this script 👍

@Nihamavasa
Copy link

Nihamavasa commented Oct 19, 2017

To overwrite exisiting docker-compose version one may use
COMPOSE_VER=$(curl -s -o /dev/null -I -w "%{redirect_url}\n" https://github.com/docker/compose/releases/latest | grep -oP "[0-9]+(\.[0-9]+)+$")
&&
curl -o $(which docker-compose) -L https://github.com/docker/compose/releases/download/$COMPOSE_VER/docker-compose-$(uname -s)-$(uname -m)
Just if there allready is a docker-compose binary
But this is not fully tested.

@chiluap
Copy link

chiluap commented Nov 29, 2017

Hi there, thank you for taking the pain away from installing the latest docker version.

One little improvement though:
The script is dependent on git being installed.
Maybe add something like:
hash git 2>/dev/null || { echo >&2 "I require git but it's not installed. Aborting..."; exit 1;}

@andersonpem
Copy link

Oh my dayum! This script is delicious :)
Thank you sir.

@jseguillon
Copy link

A little warn here : please consider using -E (extended regex) instead of -P (Perl regex) in your grep expression. => Some grep may not support -P : busybox for example does not support this flag.

@hgraca
Copy link

hgraca commented Mar 4, 2020

Nice script, tkx.

However, sometimes the versions of docker-compose are not ordered and so the version downloaded is not the last one.

This fixes it:

COMPOSE_VERSION=`git ls-remote https://github.com/docker/compose | grep refs/tags | grep -oP "[0-9]+\.[0-9][0-9]+\.[0-9]+$" | sort --version-sort | tail -n 1`

@wdullaer
Copy link
Author

wdullaer commented Mar 5, 2020

I haven't updated this in a long time. I'm happy to see it still works.

Thanks for the feedback, I'll add your fix in.

@hophamlam
Copy link

Very useful script, thanks for the job, hope you have a good day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment