Cyberithub

How to Install Pygame on Ubuntu 20.04 LTS (Focal Fossa)

Advertisements

In this article, we will see how to install Pygame on Ubuntu 20.04 LTS (Focal Fossa). Pygame is a freely available set of Python modules designed for writing video games on almost every platform and operating system. Pygame provides best gaming features by adding excellent functions on top of the SDL Library. This allows developers to create a full feature equipped games and multimedia programs in Python Language. More on official website. It is also very easy to install and use the module just like any other python modules. Here we will see the steps to install pygame on Ubuntu 20.04 LTS (Focal Fossa).

 

How to Install Pygame on Ubuntu 20.04 LTS (Focal Fossa)

How to Install Pygame on Ubuntu 20.04 LTS (Focal Fossa)

Also Read: How to uninstall zsh shell from Linux in 2 Easy Steps

Step 1: Prerequisites

a) You should have a running Ubuntu 20.04 LTS Server.

b) You should have sudo or root access to run privileged commands.

c) You should have either python3 or python2 installed in your Server.

 

Step 2: Update Your Server

Before installing pygame, it is recommended to first update and sync all the installed packages with the latest available versions from default Ubuntu repo using sudo apt update && sudo apt upgrade command as shown below.

cyberithub@ubuntu:~$ sudo apt update && sudo apt upgrade
[sudo] password for cyberithub:
Hit:1 https://downloads.plex.tv/repo/deb public InRelease
Hit:2 https://dl.google.com/linux/chrome/deb stable InRelease
Get:3 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Hit:4 http://in.archive.ubuntu.com/ubuntu focal InRelease
Get:5 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:6 http://security.ubuntu.com/ubuntu focal-security/main i386 Packages [530 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:8 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1,894 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [2,267 kB]
Get:10 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [40.8 kB]
Get:11 http://security.ubuntu.com/ubuntu focal-security/main amd64 c-n-f Metadata [11.5 kB]
Get:12 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [777 kB]
Get:13 http://security.ubuntu.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [94.1 kB]
Get:14 http://security.ubuntu.com/ubuntu focal-security/universe amd64 c-n-f Metadata [16.8 kB]
.....................................................

 

Step 3: Install Pygame

If you are using latest Python3 then to install pygame, you need to use sudo apt install python3-pygame command as shown below. In case you are using older Python2 version, then you need to use sudo apt install python-pygame command.

cyberithub@ubuntu:~$ sudo apt install python3-pygame
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libfwupdplugin1 libllvm11 libxmlb1 python-pip-whl python3-wheel zsh-common
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
libblas3 libfluidsynth2 libgfortran5 libinstpatch-1.0-2 liblapack3 libmad0 libmikmod3 libopenal-data libopenal1 libportmidi0 libsdl-image1.2
libsdl-mixer1.2 libsdl-ttf2.0-0 libsdl1.2debian libsdl2-2.0-0 libsndio7.0 python3-numpy timgm6mb-soundfont
Suggested packages:
libportaudio2 sndiod gfortran python-numpy-doc python3-pytest python3-numpy-dbg python-pygame-doc timidity fluid-soundfont-gm fluidsynth musescore
The following NEW packages will be installed:
libblas3 libfluidsynth2 libgfortran5 libinstpatch-1.0-2 liblapack3 libmad0 libmikmod3 libopenal-data libopenal1 libportmidi0 libsdl-image1.2
libsdl-mixer1.2 libsdl-ttf2.0-0 libsdl1.2debian libsdl2-2.0-0 libsndio7.0 python3-numpy python3-pygame timgm6mb-soundfont
0 upgraded, 19 newly installed, 0 to remove and 29 not upgraded.
Need to get 13.7 MB of archives.
After this operation, 38.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
...............................................

Alternatively, if you have pip3 utility available in your Server then you can also install Pygame by running pip3 install pygame command as shown below. In case, you are using older python2 version, then you need to use pip install pygame command.

cyberithub@ubuntu:~$ pip3 install pygame
Defaulting to user installation because normal site-packages is not writeable
Collecting pygame
Downloading pygame-2.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (21.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 21.8/21.8 MB 1.7 MB/s eta 0:00:00
Installing collected packages: pygame
Successfully installed pygame-2.1.2

 

Step 4: Verify Installation

After successful installation, you can list the path of installed files by using dpkg -L python3-pygame command as shown below.

cyberithub@ubuntu:~$ dpkg -L python3-pygame
/.
/usr
/usr/include
/usr/include/python3.8m
/usr/include/python3.8m/pygame
/usr/include/python3.8m/pygame/_camera.h
/usr/include/python3.8m/pygame/_pygame.h
/usr/include/python3.8m/pygame/_surface.h
/usr/include/python3.8m/pygame/bitmask.h
/usr/include/python3.8m/pygame/camera.h
/usr/include/python3.8m/pygame/fastevents.h
/usr/include/python3.8m/pygame/font.h
/usr/include/python3.8m/pygame/freetype.h
/usr/include/python3.8m/pygame/mask.h
/usr/include/python3.8m/pygame/mixer.h
/usr/include/python3.8m/pygame/palette.h
/usr/include/python3.8m/pygame/pgarrinter.h
/usr/include/python3.8m/pygame/pgbufferproxy.h
/usr/include/python3.8m/pygame/pgcompat.h
/usr/include/python3.8m/pygame/pgopengl.h
/usr/include/python3.8m/pygame/pygame.h
/usr/include/python3.8m/pygame/scrap.h
/usr/include/python3.8m/pygame/surface.h
/usr/lib
/usr/lib/python3
.............................................................

 

Step 5: Using Pygame

Now that pygame is successfully installed in the system, you can import it in your python program and start using it. For example, we are using this module in below python program where we are creating and displaying a simple screen as you may notice from the below output.

cyberithub@ubuntu:~$ nano pygame_example.py
import pygame

pygame.init()
screen = pygame.display.set_mode((300,400))
status = False

while status:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            status = True
    pygame.display.flip()

Output

cyberithub@ubuntu:~$ python3 pygame_example.py

How to Install Pygame on Ubuntu 20.04 LTS (Focal Fossa) 2

Leave a Comment