If you use Linux on your development machine and you always try out new technologies. It probably involves installing numerous packages and dependencies. Over time these packages pile up, making your system bulky and some dependencies might even cause conflicts with other installed packages. Making their maintenance and carrying out updates harder. I faced similar problems too, and I have tried to find solutions for it. Let me show an easy way to keep your machine clean, lean, and mean! using Linux Containers (LXC).
One of the ways to solve this problem is to use a virtualization platform; ergo, Linux Containers or LXC. LXC is an operating-system-level virtualisation technique. You can run multiple, isolated Linux systems (containers) on a host machine using a single Linux kernel.
LXC makes use of Kernel features such as cgroups and namespace isolation which offers an environment as close as possible to the one you’d get from a VM (ex. VirtualBox) but without the overhead that comes with running a separate kernel and simulating all the hardware.
lxc and some necessary dependencies such as
wget which also pulls some predefined templates for many of the well known Linux distributions. These templates can be found in
sudo apt install lxc lxc-templates wget bridge-utils
If your computer is behind a proxy server, there are a couple of things to take care of before we can move further. Firstly we must set up a proxy for
wget. wget is a free software package for retrieving files over the Web. Its configuration file is at
sudo vim /etc/wgetrc
Note: I’m using
vim here, but you can substitute that with any editor that you are familiar with like
Add your proxies to
https_proxy = https://yourproxy:port/ http_proxy = http://yourproxy:port/ ftp_proxy = ftp://yourproxy:port/
Here I am showing an example of an Ubuntu container. For the first time, this might take a few minutes depending upon your internet connection. Any future creations are going to much faster as the templates are cached. Note the username and password displayed in the terminal output (Usually, both the username and password are
sudo lxc-create -t ubuntu -n test_ubuntu
After installation you can list all available containers using:
Running our container is stupid simple!:
sudo lxc-start test_ubuntu
You can check if the container is running or not by doing:
sudo lxc-ls --fancy
Note that the above command also displays the container's IP address.
You can log into your running container using the
lxc-console command. This command attaches your current terminal to the console of the running container.
sudo lxc-console test_ubuntu
The network connection inside the container should work without any modifications. However, if you’re behind a proxy don’t forget to set the same proxy for
apt. You can do this by creating a new file:
sudo vim /etc/apt/apt.conf
Add the proxy in the following format:
Acquire::http::Proxy "https://yourproxy:80/"; Acquire::https::Proxy "http://yourproxy:80/"; Acquire::ftp::Proxy "ftp://yourproxy:80/"; Acquire::socks::Proxy "socks://yourproxy:80/";
You can check if the proxy is working by trying to update the system:
sudo apt update
That’s it! You can now set up your stacks like LAMP or MEAN or whichever you might prefer. However, we can make our container a bit more useful by adding GUI functionality to our container.
Not having a GUI significantly reduces the number of apps you can use in a container, so let’s fix it. X Window system is a framework that provides a necessary GUI environment and is used by most Linux distributions (including Ubuntu). The idea is to access the host machine’s X server from the container using SSH.
sudo apt update
sudo apt upgrade
sudo apt install xauth dbus dbus-x11
Now you can install any GUI based applications; We’ll install firefox as an example.
sudo apt install firefox
firefox also needs
canberra-gtk-module to work.
sudo apt install libcanberra-gtk-module libcanberra-gtk3-module
Open a new terminal window and SSH into your container while enabling X11 forwarding.
ssh -X firstname.lastname@example.org
Note: You can find your container’s IP address using the command
sudo lxc-ls –fancy.
We can now start using our application.
Note: You can find the location of any package using the
which command (Ex.
$ which firefox)
When we use a container to try out new things and experiment, it is likely that we mess one up in the process. Hence, it is a good idea to backup or to keep several copies of our container. So we keep one up-to-date container as a base (we will not do anything in this container except for updating it occasionally) and then, as needed we create copies (snapshots) of it.
sudo lxc-copy -n base_container -N new_container -B overlayfs -s
The snapshots can be started/stopped like any other containers. You can also destroy a snapshot or a container if you no longer need them.
sudo lxc-stop test_ubuntu
This command completely removes the container and all its data from your system.
sudo lxc-destroy test_ubuntu
That’s it now you can continue your learning process without having to worry about messing up your system.