India English
Kenya English
United Kingdom English
South Africa English
Nigeria English
United States English
United States Español
Indonesia English
Bangladesh English
Egypt العربية
Tanzania English
Ethiopia English
Uganda English
Congo - Kinshasa English
Ghana English
Côte d’Ivoire English
Zambia English
Cameroon English
Rwanda English
Germany Deutsch
France Français
Spain Català
Spain Español
Italy Italiano
Russia Русский
Japan English
Brazil Português
Brazil Português
Mexico Español
Philippines English
Pakistan English
Turkey Türkçe
Vietnam English
Thailand English
South Korea English
Australia English
China 中文
Canada English
Canada Français
Somalia English
Netherlands Nederlands

Install Python 3 and Set up Development Environment on Ubuntu Server 18.04

Introduction

The flexibility and versatility of Python language than can be leveraged for inso many cases. Scripting, data analysis, backend development, machine learning, automation, are just some cases where one can use the language. With its easy set up process and compatibility and gives it a upper hand over other languages.  Its also a very beginner friendly language since its code syntax is so straight forward making it gradual and smooth to learn.

In this guide, we’ll install Python 3 and set up a development environment on an Ubuntu 18.04 server.

Programming on a server has many advantages and supports collaboration across development project.

*The general principles of this tutorial will apply to any distribution of Debian Linux.

 

Prerequisites.

Internet connection

A server or virtual machine runnig Ubuntu 18.04 Server

 

Setting Up Python 3

Ubuntu 18.04 and other versions of Debian Linux ship with both Python 3 and Python 2 pre-installed. To make sure that our versions are up-to-date, let’s update and upgrade the system with the apt command to work with Ubuntu’s Advanced Packaging Tool:

sudo apt update

sudo apt -y upgrade

By default python 3 comes installed in Ubuntu 18.04

Run the command below to see installed version

python3 --version


To manage software packages for Python, install pip, a tool that will install and manage programming packages we may want to use in our development projects.

sudo apt install -y python3-pip

After the installation is complete, you can download packages by running

pip3 install package_name

Here, package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install Scipy, you can do so with the command pip3 install scipy.

To ensure you have a robust setup for your development environment, install a few more packages with this command.

sudo apt install build-essential libssl-dev libffi-dev python3-dev

Once Python is set up, and pip and other tools are installed, you can set up a virtual environment for your development projects.

Set Up a Virtual Environment

Virtual environments enable you to have an isolated space on your server for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects.

Setting up a programming environment provides you with greater control over your Python projects and over how different versions of packages are handled. This is especially important when working with third-party packages.

You can set up as many Python programming environments as you want. Each environment is basically a directory or folder on your server that has a few scripts in it to make it act as an environment.

Install venv

While there are a few ways to achieve a programming environment in Python, we’ll be using the venv module here, which is part of the standard Python 3 library. Install venv by typing:

sudo apt install -y python3-venv
Upon completion, you are now ready to create a virtual environment.

To create the virtual environment, run:

mkdir environments cd environments

Once you are in the directory where you would like the environments to live, you can create an environment by running the following command:

python3.6 -m venv sample

Essentially, pyvenv sets up a new directory that contains a few items which we can view with the ls command:

ls my_env
Output
bin include lib lib64 pyvenv.cfg share

Together, these files work to make sure that your projects are isolated from the broader context of your local machine, so that system files and project files don’t mix. This is good practice for version control and to ensure that each of your projects has access to the particular packages that it needs. Python Wheels, a built-package format for Python that can speed up your software production by reducing the number of times you need to compile, will be in the Ubuntu 18.04 share directory.

To use this environment, you need to activate it, which you can achieve by typing the following command that calls the activate script:

source sample/bin/activate

Your command prompt will now be prefixed with the name of your environment, in this case it is called my_env. Depending on what version of Debian Linux you are running, your prefix may appear somewhat differently, but the name of your environment in parentheses should be the first thing you see on your line:

(sample) rawle@vps648174:~/environments$

Creating a “Hello, World” Program

Now that we have our virtual environment set up, let’s create a traditional “Hello, World!” program. This will let us test our environment and provides us with the opportunity to become more familiar with Python if we aren’t already.

To do this, we’ll open up a command-line text editor such as nano and create a new file:

nano hello.py

Once the text file opens up in the terminal window we’ll type out our program:

print("Hello, World!")

Exit nano by typing the CTRL and X keys, and when prompted to save the file press y.

Once you exit out of nano and return to your shell, let’s run the program:

python hello.py

The hello.py program that you just created should cause your terminal to produce the following output:

Output
Hello, World!

To leave the environment, simply type the command deactivate and you will return to your original directory.

× WhatsApp us