DEV Community

Full Stack Hacker
Full Stack Hacker

Posted on • Edited on

1 1

Install Python 3 and set up a programming environment on Ubuntu

Step 1 — Install Python 3

Step 2 — Install pip

To manage software packages for Python, install pip, a tool that will help you 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

Step 3 — 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 4 — 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 5 — Create a Virtual Environment

python3 -m venv my_env

Step 6 — 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:

(my_env) ubuntu@ubuntutech:~/environments$

Step 7 — Test Virtual Environment

Open the Python interpreter:

(my_env) ubuntu@ubuntutech:~/environments$ 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.8.2 (default, Mar 13 2020, 10:14:16) 
[GCC 9.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!")

Output: Hello, World!

Step 8 — Deactivate Virtual Environment

Quit the Python interpreter: quit()

Then exit the virtual environment:

(my_env) ubuntu@ubuntutech:~/environments$ deactivate

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay