How To Install Python 3 and Set Up a Programming Environment on Ubuntu 18.04

  • 2019-03-11 03:17 AM
  • 39

This tutorial will walk you through installing Python and setting up a programming environment on an Ubuntu 18.04 server.

Introduction

Python is a flexible and versatile programming language, with strengths in scripting, automation, data analysis, machine learning, and back-end development.

This tutorial will walk you through installing Python and setting up a programming environment on an Ubuntu 18.04 server.

Step 1 — Update and Upgrade

Logged into your Ubuntu 18.04 server as a sudo non-root user, first update and upgrade your system to ensure that your shipped version of Python 3 is up-to-date.

sudo apt update
sudo apt -y upgrade

Confirm installation if prompted to do so.

Step 2 — Check Version of Python

Check which version of Python 3 is installed by typing:

python3 -V

You’ll receive output similar to the following, depending on when you have updated your system.

OutputPython 3.6.5

Step 3 — Install pip

To manage software packages for Python, install pip, a tool that will install and manage libraries or modules to use in your projects.

sudo apt install -y python3-pip

Python packages can be installed by typing:

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 NumPy, you can do so with the command pip3 install numpy.

Step 4 — Install Additional Tools

There are a few more packages and development tools to install to ensure that we have a robust set-up for our programming environment:

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

Step 5 — Install venv

Virtual environments enable you to have an isolated space on your server for Python projects. We’ll use venv, part of the standard Python 3 library, which we can install by typing:

sudo apt install -y python3-venv

Step 6 — Create a Virtual Environment

You can create a new environment with the pyvenv command. Here, we’ll call our new environment my_env, but you can call yours whatever you want.

python3.6 -m venv my_env

Step 7 — Activate Virtual Environment

Activate the environment using the command below, where my_env is the name of your programming environment.

source my_env/bin/activate

Your command prompt will now be prefixed with the name of your environment:

Step 8 — Test Virtual Environment

Open the Python interpreter:

python

Note that within the Python 3 virtual environment, you can use the command python instead of python3, and pip instead of pip3.

You’ll know you’re in the interpreter when you receive the following output:

Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Now, use the print() function to create the traditional Hello, World program:

print("Hello, World!")

OutputHello, World!

Step 9 — Deactivate Virtual Environment

Quit the Python interpreter:

quit()

Then exit the virtual environment:

deactivate

Further Reading

An A-Z of useful Python tricks
A Complete Machine Learning Project Walk-Through in Python
Machine Learning: how to go from Zero to Hero
Learning Python: From Zero to Hero
Automated Machine Learning on the Cloud in Python
Introduction to PyTorch and Machine Learning
Python Tutorial for Beginners (2019) - Learn Python for Machine Learning and Web Development
How to get started with Python for Deep Learning and Data Science
How To Use Vuls as a Vulnerability Scanner on Ubuntu 18.04
Complete Python Bootcamp: Go from zero to hero in Python 3
Machine Learning A-Z™: Hands-On Python & R In Data Science
Complete Python Masterclass
Python and Django Full Stack Web Developer Bootcamp

*Originally published by Lisa Tagliaferri at https://www.digitalocean.com

Suggest