Skip to content

Getting Started with Python and Artificial Intelligence on Ubuntu

Artificial Intelligence and Machine Learning are definitely making the headlines again, and developers are starting to make use of it in applications. Getting started with AI is not as easy as it should be.

Setting up a development environment in order to run through a number of tutorials available is also a difficult task for a lot of developers especially if those developers are not familiar with Python and its ever-expanding ecosystem.

How to develop AI enabled applications with Python on Ubuntu 18.04

If you are taking the time to learn how to develop in Python, then there is a good chance that you will enevitably be interested in developing applications for large-scale data processing, predictive analytics, scientific computing, Machine Learning (ML) or Artificial Intelligence (AI).

These types of projects will typically require the creation of a number of different operating environments using possibly different versions of Python or even library versions to work.

For instance, TensorFlow, which is a popular machine learning library, is not compatible with Python 3.7 and in most cases it is preferred to use Python 3.5, which can be quite troublesome to always re-configure your development workstation every time you're moving between projects.

This is where Virtual Environments are ideal. Python comes prepackaged with a Virtual Environment tool, called venv, which works great for creating lightweight virtual environments.

What is Anaconda

Anaconda is an open-source package manager, environment manager, and distribution of the Python and R programming languages. It is commonly used for large-scale data processing, scientific computing, and predictive analytics, serving data scientists, developers, business analysts, and those working in DevOps.

Package versions are managed by the package management system called conda.

The Anaconda distribution is used by over 6 million users and includes more than 250 popular data-science packages suitable for Windows, Linux, and Mac.

Install Anaconda

The first step to installing Anaconda on Ubuntu 18.04 is to download the Anaconda installer for linux. Typically this will download to your Downloads folder. After download has completed, open a terminal window and change into your Downloads folder cd ~/Downloads.

The name of the file will typically start with Anaconda but will then include a version number and environment i.e. Anaconda3-2018.12-Linux-x86_64 we will just use the descriptor Anaconda-latest-Linux-x86_64.sh to refer to file

Depending  on how you have your Ubuntu configured you can install Anaconda with or without sudo.

To install anaconda with sudo

sudo bash Anaconda-latest-Linux-x86_64.sh

or without sudo

bash Anaconda-latest-Linux-x86_64.sh

After the install is complete, you should see a new folder in your  Home directory : anaconda3

Anaconda Navigator

Once the Anaconda installation has completed you can now start using it. To verify that anaconda has been installed, you can simply use the command python in a terminal window and see a response similar too :

You'll notice that Anaconda is now included within your python version information. You can use quit() to exit out.

To check which packages are installed you use the command conda list which will list all the libraries available.

If you would like a friendly user interface to anaconda simply use the command anaconda-navigator the Anaconda Navigator will be displayed

anaconda navigator

Using Conda

Conda is package and environment management system that helps to quickly install run and update packages and their dependencies.

Conda also helps to easily create, save, load and switch between different environments on your local computer. It was specifically created for Python but is flexible enough to package and distribute software for any language.

Conda can be combined with continuous integration systems such as Travis CI and AppVeyor to provide frequent, automated testing of your code.

If you have followed the steps detailed above you should have conda installed, to double check this you can check which version of conda is installed by using the following terminal command

conda --version 

To check if there are any updates to your conda environment you can use the following command to update

conda update conda

Create an environment using Conda

Conda makes it super easy to create separate environments containing files, packages and dependencies which are isolated and won't interfere with other environments.

When you installed Conda a default environment was created called base. Ideally, you shouldn't create or put applications into your base environment, you should create separate environments to keep your programs isolated from each other.

If you installed Anaconda using the instructions above, you'll have noticed we installedPython 3.7 which is the latest version of Python, but what if you wanted to develop an application using a specific version of Python, for instance maybe an AI or machine learning application making use of TensorFlow which may restrict you to using Python 3.6

You can easily create an environment, in my case, I will call it threenine and explicitly set the Python version to 3.5.

conda create --name threenine python=3.5 anaconda

I can then activate that environment

source activate threenine

Then I can install Tensorflow to that environment

conda install -n threenine tensorflow

In 1 simple line of code we have now created a python environment already configured with TensorFlow and we can now start developing straight away.

Let's build on that environment. Let's say we would like to develop an Artificial Intelligence or Machine Learning application that needs to extract information from a number of documents i.e. Word Documents, Excel Spreadsheets and Pdf's, which typically called Dark Data because the data is not stored in Database environment i.e. Relational Database or Object Database.

A popular python library called Textract  provides a single interface for extracting content from any type of file, without any irrelevant markup.

We can install textract to our environment quite easily using

conda install -n threenine textract

We can now start our development environment, typically a Text Editor or an Integrated Development Environment.

Update a Conda Environment

You will routinely want to update the packages in your Conda Environment, in order to ensure you application makes use of the latest packages etc. Fortunately, conda makes this really easy to do so. The following are some commands you can use to do so.

# Update all the packages in an environment
conda update --all

# update all packages unprompted
conda update --all -y

# list packages that can be updated
conda search --outdated

Conclusion

Anaconda is the fastest and easiest way to do Python and R data science and machine learning on Linux. It's the defacto industry standard for developing, testing, and training on a single machine.

Conda is an open source package management system and environment management system that runs on Linux. Conda quickly installs, runs and updates packages and their dependencies.

Conda easily creates, saves, loads and switches between environments on your local computer. It was created for Python programs, but it can package and distribute software for any language.

Gary Woodfine
Latest posts by Gary Woodfine (see all)
Tags: