# Install, Create and Manage LXC in Ubuntu/Debian

<https://www.tecmint.com/install-lxc-ubuntu-debian/>

Over the last decade, the open-source community has seen a steady shift to containerization as the preferred way of deploying applications thanks to the numerous benefits it offers such as portability, flexibility, increased security, and easier management of applications. Popular containerization technologies include [Docker](https://www.tecmint.com/install-docker-and-learn-containers-in-centos-rhel-7-6/), **Podman**, and **LXD**.

Written in Go language, [LXD](https://linuxcontainers.org/lxd/) (pronounced as **Lekseed**) is described as the next generation system container and virtual machine manager that allows you to manage your containers and virtual machines from the command line, or by leveraging a **REST API** or other third party tools. **LXD** is an open-source project and is an extension of **LXC** (**Linux Containers**) which is OS-level virtualization technology.

**LXC** came into the picture around 2008, and **LXD** was launched 7 years later in 2015 with the same building blocks as **LXC**. **LXD** came to make containers more user-friendly and easy to manage.

Being an extension of **LXC**, **LXD** provides advanced features such as snapshots and live migration. It also provides a daemon that lets you easily manage containers and virtual machines. It is not intended to supplant **LXC**, rather, it is aimed at improving the usability and handling of LXC-based containers

In this guide, we will demonstrate how you can create and manage **LXC** containers using **LXD** on **Debian/Ubuntu**.

### Step 1: Install LXD on Ubuntu

The first step is to install **LXD**. There are two ways of doing this, you can install from Ubuntu’s repository using the [APT package manager](https://www.tecmint.com/apt-advanced-package-command-examples-in-ubuntu/) or you can use [snap](https://www.tecmint.com/install-snap-in-linux/).

Using **APT**, first update the system:

```
$ sudo apt update

```

Then install the **LXD** system container hypervisor as follows.

```
$ sudo apt install lxd

```

![](https://296194292-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLoAqAoOfr7XVUQw7Gff8%2Fuploads%2Fgit-blob-8939148c62df5f01c38e7a9d2802e2eca28e3288%2FInstall-LXD-in-Ubuntu.png?alt=media)

Install LXD in Ubuntu

Using **snap**, you can install the latest version of **LXD**.

```
$ sudo snap install lxd

```

In addition, you can install the latest LTS release which is **LXD 4.0** as follows:

```
$ sudo snap install lxd --channel=4.0/stable

```

You can verify the version of **LXD** installed as shown:

```
$ lxd --version

```

![](https://296194292-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLoAqAoOfr7XVUQw7Gff8%2Fuploads%2Fgit-blob-335250676054bf3c4e75b84e4d2315278d932c13%2FCheck-LXD-Version.png?alt=media)

Check LXD Version

If you used to **snap**, you can verify that the **LXD** snap package was installed as shown:

```
$ snap list

```

![](https://296194292-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLoAqAoOfr7XVUQw7Gff8%2Fuploads%2Fgit-blob-c4fef99945ee5429b996330b6fbfe6d7c5043404%2FList-LXD-Snap-Package.png?alt=media)

List LXD Snap Package

### Step 2: Initializing LXD Service

To initialize or start the **LXD** container hypervisor, run the command:

```
$ sudo lxd init

```

The command presents you with a set of questions on how to configure **LXD**. The defaults work just fine, however, you are at liberty to specify your own settings as per your requirements.

In this example, we have created a storage pool called **tecmint\_pool** with the **ZFS** file system and volume manager. For the rest of the questions, we have chosen to go with the default options. An easy way to accept the default selection is to press the ENTER button on your keyboard.

![](https://296194292-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLoAqAoOfr7XVUQw7Gff8%2Fuploads%2Fgit-blob-0cd9d86e78705885676a1a7f20e731a356ea6c92%2FInitializing-LXD-Service.png?alt=media)

Initializing LXD Service

Confirm the information provided by running the command:

```
$ sudo lxc profile show default

```

![](https://296194292-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLoAqAoOfr7XVUQw7Gff8%2Fuploads%2Fgit-blob-87cf817ef24b43b4f51f4c4276fa16e05d4286e7%2FConfirm-LXD-Profile.png?alt=media)

Confirm LXD Profile

You can further narrow it down to the storage pool created. The commands below display details of the current storage pools.

```
$ sudo lxc storage list
$ sudo lxc storage show tecmint_pool

```

![](https://gitlab.com/johnmkane/tech-recipe-book/-/blob/main/Book/Architect/LXC/Install,%20Create%20and%20Manage%20LXC%20in%20Ubuntu%20Debian/Untitled)

List LXD Storage Pools

You can also display information about the network interface in use by **LXD**, in this case, **lxdbr0**, which is the default selection.

```
$ sudo lxc network show lxdbr0

```

![](https://296194292-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLoAqAoOfr7XVUQw7Gff8%2Fuploads%2Fgit-blob-0132fae7d9104335e47543032e7efb0bf1fa0b5f%2FList-LXD-Network-Configuration.png?alt=media)

List LXD Network Configuration

### Step 3: Creating LXD Containers in Ubuntu

Now, let’s switch gears and create Linux containers. You can list all the prebuilt containers that are available for download using the command:

```
$ sudo lxc image list images:

```

This populates a huge list of all the containers in various operating systems such as Ubuntu, CentOS, Debian, and AlmaLinux, to mention a few.

You can narrow it down to a specific distribution as follows:

```
$ sudo lxc image list images: | grep -i centos
$ sudo lxc image list images: | grep -i debian

```

In this example, we are listing the available containers.

```
$ sudo lxc image list images: | grep -i ubuntu

```

Now, we are going to create our first container. The syntax for creating a container is as follows:

```
$ sudo lxc launch images:{distro}/{version}/{arch} {container-name}

```

Now we are going to create two containers from **Ubuntu 20** and **Debian 10** respectively:

```
$ sudo lxc launch images:ubuntu/focal tecmint-con1
$ sudo lxc launch images:debian/10 tecmint-con2

```

In the above examples, we have created two containers: **tecmint-con1** and **tecmint-con2**.

To list the containers created, run the command:

```
$ sudo lxc list

```

From the output, we can see our two containers listed.

To gain shell access to a **LXC** container run the command:

```
$ sudo lxc exec tecmint-con1 bash

```

Once you have gained shell access, notice that the prompt changes to indicate that you are running as the root user.

![](https://296194292-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLoAqAoOfr7XVUQw7Gff8%2Fuploads%2Fgit-blob-f34fd622fcaf8d321a975bd0d74b5d2456c00948%2FAccess-LXC-Container-Shell.png?alt=media)

Access LXC Container Shell

To exit the container, run the command:

```
$ exit

```

### Step 4: Managing LXD Containers in Ubuntu

Now, let’s check out some of the commands you can use to manage **LXD** containers.

To list all the running containers, run the command:

```
$ sudo lxc list

```

To display detailed information about an **LXC** container, use the syntax:

```
$ sudo lxc info container-name

```

This will provide you with information such as the container’s name, architecture, creation date, status network interfaces, bandwidth, CPU, memory, and disk usage to mention a few metrics.

![](https://296194292-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLoAqAoOfr7XVUQw7Gff8%2Fuploads%2Fgit-blob-b5447938fadca2ad6d2bc25f2138e5b92009603e%2FGet-LXC-Container-Info.png?alt=media)

Get LXC Container Info

To stop an **LXC** container, use the syntax:

```
$ sudo lxc stop container-name

```

For example, to stop container **tecmint-con1**, execute the command:

```
$ sudo lxc stop  tecmint-con1

```

Again, list the containers to confirm that the container has been stopped.

```
$ sudo lxc list

```

Alternatively, you can list either running or stopped containers as follows:

```
$ sudo lxc list | grep -i STOPPED
$ sudo lxc list | grep -i RUNNING

```

To start an **LXC** container, use the syntax:

```
$ sudo lxc start container-name

```

For example, to start container **tecmint-con1** run the command:

```
$ sudo lxc start tecmint-con1

```

You can start or stop containers bypassing them in one command separated by a space using the following syntax:

```
$ sudo lxc stop container1 container2
$ sudo lxc start container1 container2

```

For example, to stop all containers, run:

```
$ sudo lxc stop tecmint-con1 tecmint-con2

```

To restart an **LXC** container, use the syntax:

```
$ sudo lxc restart container-name

```

For example, to restart container **tecmint-con1** run the command:

```
$ sudo lxc restart tecmint-con1

```

Alternatively, you can pass multiple containers in a single command:

```
$ sudo lxc start container1 container2

```

For example, to restart all containers, run:

```
$ sudo lxc restart tecmint-con1 tecmint-con2

```

To delete an **LXC** container, first, stop it, then delete it. For example, to delete container **tecmint-con2**, run the command:

```
$ sudo lxc stop tecmint-con2
$ sudo lxc delete tecmint-con2

```

This guide has provided you with a solid foundation about **LXD** containers and how you can launch, create and manage containers. It’s our hope that you can now comfortably launch and manage your containers without much difficulty.
