Install LXD pure-container hypervisor on Ubuntu 18.04 LTS

How can I install LXD pure-container hypervisor on Ubuntu 18.04 LTS to runs unmodified Debian/Ubuntu/CentOS Linux operating systems VM at incredible speed? How do I setup and use LXD on Ubuntu Linux server?

Introduction: The LXD container hypervisors are supported and created by Ubuntu team. It provides 25% faster performance than ESX. 10 times the density with zero latency. One can move Linux VMs straight to containers using LXD without modifying the apps or administration processes. Ubuntu’s LXD is a pure-container hypervisor that runs unmodified Linux guest operating systems with VM-style operations at incredible speed. You can run CentOS, Arch Linux, Fedora Linux, OpenSUSE, Gentoo, Ubuntu, Debian and other Linux distro in containers. This page shows how to install and configure LXD pure-container hypervisor on Ubuntu Linux 18.04 LTS.

Why use LXD?

LXD is a container hypervisor providing a ReST API to manage LXC containers. One can do the following things with LXD:

  1. Run containers
  2. Update containers
  3. Clustering support
  4. Install different Linux distro inside containers
  5. Manage container resources, like storage volumes and networks

Setting up LXD on Ubuntu 18.04 LTS

The procedure to set up and use LXD on Ubuntu Linux 18.04 LTS is as follows:

  1. Install lxd
  2. Setup and configure LXD networking and storage
  3. Create your first container
  4. Launch a container

Let us see all steps in details.

Install LXD pure-container hypervisor on Ubuntu 18.04 LTS

Type the following apt-get command or apt command to LXD:
$ sudo sh -c 'apt update && apt upgrade'
$ sudo apt install lxd

How to install LXD pure-container hypervisor on Ubuntu 18.04 LTS

How to add user to the LXD group for management purpose

No need to be the root user to manage LXD daemon. To manage LXD server add your username to lxd group using the adduser command on Ubuntu Linux:
$ sudo adduser {USERNameHere} lxd
$ sudo adduser vivek lxd

Please note that group membership is only applied at login or new ssh session, you then either need to close and re-open your user/ssh session or use the following command to avoid problems:
$ newgrp lxd
Verify it with the id command:
$ id

Setup ZFS (optional)

I suggest you use ZFS and along with DHCP based IP networking for ease of setup for each VM. The ZFS offers quick option to make snapshots and other advanced features:
$ sudo apt install zfsutils-linux
See how to install ZFS on Ubuntu server for more information.

Configure LXD storage and networking option

It is time to set up the LXD server. You must configure networking and storage option such as directory, ZFS, Btrfs and more:
$ sudo lxd init
You must answer a series of questions on how to configure the LXD server. Here is a sample session:
Setup LXD
LXD is now set up and configured. You can verify it with the following command:
$ lxc list
$ lxc info | more

Verify the LXD server running

Creating your first Linux container

To list all LXD images run:
$ lxc image list images:
$ lxc image list images: | grep -i centos
$ lxc image list images: | grep -i ubuntu
$ lxc image list images: | grep -i debian

LXD list images

How create and use your first container on Ubuntu 18.04 LTS running LXD

To create and start containers from images use the launch command:
lxc launch images:{distro}/{version}/{arch} {container-name-here}
Let us see some examples to create and start containers from various Linux distro images as per your needs.

Alpine Linux 3.8 VM

$ lxc launch images:alpine/3.8/amd64 alpine-c1
LXD running Alpine Linux 3.8

CentOS Linux 7 container

$ lxc launch images:centos/7/amd64 cenots-c2

Ubuntu Linux 16.04 or 18.04 or 20.04 LTS container

$ lxc launch images:ubuntu/bionic/amd64 ubuntu-nginx-c3
$ lxc launch images:ubuntu/eoan/amd64 ubuntu-nginx-c4
$ lxc launch images:ubuntu/focal/amd64 ubuntu-nginx-c5

Debian Linux 9.x “stretch” or 10.x container

$ lxc launch images:debian/stretch/amd64 debian9-c4
$ lxc launch images:debian/10/amd64 debian10-www01

Arch Linux container

$ lxc launch images:archlinux/amd64 arch-c5

Gentoo Linux container

$ lxc launch images:gentoo/amd64 gentoo-c6

Fedora Linux 30 container

$ lxc launch images:fedora/30/amd64 fedora30-c7

OpenSUSE Linux 15.0 container

$ lxc launch images:opensuse/15.0/amd64 opensuse-stable-c8

Oracle Linux 7.x VM

$ lxc launch images:oracle/7/amd64 oracle7-c9

How do I list all my containers?

List the existing containers:
$ lxc list --fast
$ lxc list | grep RUNNING
$ lxc list | grep STOPPED
$ lxc list | grep -i ubuntu
$ lxc list "*c1*"
$ lxc list "*c2*"
$ lxc list

LXD list containers using lxc command

Click to enlarge it

How to execute/run the specified command in a container

To run or execute commands in containers use exec command:
lxc exec containerName -- command
lxc exec containerName -- /path/to/script
lxc exec containerName --env EDITOR=/usr/bin/vim -- command
### run date, ip a, ip rm and other commands on various containers ###
$ lxc exec cenots-c2 -- date
$ lxc exec cenots-c2 -- ip a
$ lxc exec ubuntu-nginx-c3 -- ip r
$ lxc exec fedora28-c7-- dnf update
$ lxc exec debian9-c4 -- cat /etc/debian_version

How to get the bash shell access in a container

To gain login and gain shell access in a container named file-server , enter:
$ lxc exec debian9-c4 bash
Now you can run commands or install packages:
# cat /etc/*issue*
# apt-get update
# apt-get upgrade

To exit simply from container simply type exit:
# exit

How do I start my containers?

Start containers using the following cli:
$ lxc start containerName
$ lxc start foo
$ lxc start cenots-c2 opensuse-stable-c8

How do I stop my containers?

Stop containers using the following syntax:
$ lxc stop containerName
$ lxc stop foo
$ lxc stop cenots-c2 opensuse-stable-c8

How do I restart my containers?

Want to restart your containers for any reasons? Try:
$ lxc restart containerName
$ lxc restart foo
$ lxc restart cenots-c2 opensuse-stable-c8

How do I delete my containers?

The command is (be careful as the LXD containers are deleted immediately without any confirmation prompt i.e. keep backups):
$ lxc delete containerName
$ lxc delete foo
$ lxc delete cenots-c2

You may get the following error while deleting the container:

The container is currently running, stop it first or pass –force.

To fix this:
$ lxc stop cenots-c2 && lxc delete cenots-c2

How do I show information on LXD servers and containers?

Type the following command:
$ lxc info
$ lxc info containerName
$ lxc info opensuse-stable-c8


And, there you have install lxd on Ubuntu 18.04, up and running with Linux distro of your choice. See the following urls for more info:

This entry is 13 of 20 in the LXD Tutorial series. Keep reading the rest of the series:
  1. Install LXD container hypervisor on Ubuntu 16.04 LTS
  2. How to install and setup LXC (Linux Container) on Fedora Linux 26
  3. Set up LXD container under KVM or Xen virtual machine
  4. List VM images in LXD (Linux Containers)
  5. Upgrade LXD containers powered by Ubuntu/Debian or CentOS Linux
  6. Auto start LXD containers at boot time in Linux
  7. Command to rename LXD / LXC container
  8. Run commands on Linux Container (LXD) instance at provision launch time
  9. Use LXD (Linux containers) in a shell script to create VM when the cloud instance launches
  10. Move/migrate LXD VM to another host on Linux
  11. Fedora install and set up LXD
  12. CentOS 7.x install and set up LXD server
  13. Install LXD pure-container hypervisor on Ubuntu 18.04 LTS
  14. Create snapshots with lxc command for LXD
  15. Set up and install LXD on CentOS/RHEL 8
  16. Ubuntu 20.04 LTS install and set up LXD
  17. Full backup and restore LXD containers
  18. Disable firewall and NAT rules on the LXD bridge
  19. Delete or remove LXD container using the lxc
  20. Linux Filesystem Error: Transaction failed solution

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum