Now we're going to dive out to our command line of our Ubuntu 20.04
system, creating a Python virtual environment and then going through
and installing Ansible, a configuration management product, so we're
able to see how and why we use Python virtual environments to sandbox
our applications.
So here we are working on our Ubuntu 20.04 system.
Now we know that when we're working with our software installer,
apt, we should go through and make sure that the metadata is up to date.
So here when we run our sudo apt update,
we go through and check the online repositories and get a new list of
packages that are available. Even though we did do an upgrade yesterday, we
can see there are packages that can be upgraded.
I am going to check this because we're going to be working with Python, and if
there is an upgrade to Python, we might as well take it.
So sudo apt and list ‑‑ and then ‑‑upgradable,
we should be able to then go through and see all of the packages that are
available within our repository system that could be upgraded.
Now there is our Python script,
python3 command not found, but we don't really need that
as far as running Python is concerned.
But if there was an update to the main Python program,
I would probably install that, as we're going to be working with Python.
So I'm happy to let this go and clear my screen.
If I go through and take a look at Python at the moment and then ‑‑version,
we can see that we've got Python 3.8.10. When I go
ahead and take a look at which python3,
it will give me the path to the python interpreter, and we can see that
this is in the normal /usr/bin/python3 directory. And we'll have a whole
load of libraries that are associated with underneath our /usr/lib. If we
install everything into the system, then we have to be root to run the
installs, and we are adding to the overhead of Python on our system, and as
we said in the slides,
giving a possibility of a clash of different library versions in our
system that can clash between different applications, application 1,
needing maybe version 1 of the library, application 2, needing version
1.1 of the library file. To overcome this,
we can go through and run a sudo apt install ‑y, so it doesn't
prompt us, and then python3‑virtualenv. With that done and dusted,
we can allow that to go through and install.
Now we can see there are a whole load of modules that are being installed.
These are being installed into the system, and in
general, this is what we want to avoid.
But by setting up a virtual environment and having the modules to create
a virtual environment, it's going to allow us to avoid the problem where
we install everything into the system.
If I go through and create a top‑level directory to house all of my virtual
environments and I can move into that directory, and now if I go through and
run the command virtualenv, and we'll create an environment for Ansible. This
will have created our whole structure.
If I do an ls ‑r from here, we can see that it's created a whole
structure of directories, and this has a complete and fresh
install of Python 3.8 in this environment.
It's not active at the moment, so nothing is effective.
Well, we can go through and activate the environment by
reading in using the source command,
taking a look in that ansible/bin directory and then the activate script.
And we can see now that we have a change in our prompt.
Also, if I go through and take a look at which python3, we'll find
that the path has changed. And of course, all of the associated
libraries will have changed as well.
Now that does mean then I can run pip3,
the Python installer, to do a pip3 install ansible. Ansible is a
configuration management product that is entirely Python‑based. Now I
can install it from the standard system repositories, but I can also
install it directly using pip.
This is a little bit like grabbing our source code and installing our
source code, but it's just the Python way of doing it when we get the
latest and greatest. So we can see then that we download a 37 MG
zipped up file. And pretty much then, we will go through and expand
that and run the setup programs to have Ansible installed in this
environment.
We can allow it to run through for a little while, and
we'll continue once it's installed. Again,
consider about 3 or 4 minutes for the installation.
Now, with Ansible installed,
we, of course, are going to be able to run the program. We always
go through and check the version of Ansible that we've got, but
also print a heap of extra information.
So as I use Ansible ‑‑version, we can see that we've got Ansible 2.12,
so actually 212.1. This is the latest and greatest.
But importantly,
when we go through and take a look at the ansible python module
location, the location of the Python libraries being used are coming
from our virtual environment, so
/home/vagrant/python/ansible/lib/python3.8/site‑packages/ansible, as
opposed to /usr/lib/python3.8/site‑packages and so forth.
So this way, we've got a clean install of our Python in the virtual
environment, and we've installed all of our Ansible modules into the
virtual environment, so we're not affecting anything else.
If we want to deactivate the environment,
then of course, we can, just the deactivate command. We can
always go back and activate it when we need it.
As I try and access Ansible now,
it's not available to the system, only to our virtual environment,
and this is really our sandboxed environment.
It is available as a package to be installed from our standard repositories, and
we can see there the suggestion that we might like to install Ansible, but if we
did so, we'll be getting a much earlier version.
We can see that we've got version 2.12 here,
but if we go through and check the version from the repositories using
apt and search, and then we know to look for just the word Ansible, so we
can use then beginning and ending with ansible, and we can see there it's
version 2.9.6, so a very much earlier version and a version that we might
prefer not to use,
especially when we think that all of this would be installed into
our system environment and perhaps give us headaches trying to
manage other Python applications that need access to the same Python modules or libraries, but different versions.