Learn two ways to install PyGame Python Module on Ubuntu 22.04 Jammy or 20.04 Focal using the command line through APT or PIP package manager.
Pygame is a Python module created for developers to create games and multimedia applications. It is built on top of the Simple DirectMedia Layer (SDL) with the help of that, programmers can access low-level multimedia components such as graphics, audio, and input devices. This Python can be easily installed on Linux either using the system package manager or the PIP. Once it is on the system, it provides various functionalities and tools for creating 2D games, simulations, and interactive graphical applications.
Various components of Pygame provide support for drawing shapes, images, and text; handling user inputs; playing and manipulating audio files. Apart from that additional utilities and modules simplify game development, collision detection functionality, and ofcourse support a large community of developers.
It is a cross-platform because it only needs Python language that is available to install on all popular operating systems.
Steps to install PyGame on Ubuntu 22.04 or 20.04 using PIP or APT
Given commands can be used for Debian, Linux Mint, Pop OS, and other popular similar Linux.
1. Access Command Terminal
Unlike Windows, installing software on a Linux system such as Ubuntu using the command terminal is much easier than the graphical user interface. Therefore, open the Terminal window. You can use the keyboard shortcut i.e. Ctrl+Alt+T.
2. Update APT package List
It is important to make sure our Ubuntu system is up to date and its package manager APT knows what are the new versions of packages available to install through the various system repositories. Therefore, before moving further, once execute the given command:
sudo apt update -y
3. Install PyGame on Ubuntu 22.04 or 20.04
Now, there are two ways to install the PyGame Python module on Ubuntu Linux one is using the APT package manager, and the other with the help of PIP which is Python’s package manager. Will show you the commands for both, use any of them at your convenience.
sudo apt install python3-pygame
The other way to install Pygame is using PIP:
pip3 install pygame
Note: Those who have not pip3 installed can run: sudo apt install python3-pip
However, if you are using Python2 then you can use:
pip install pygame
4. Check the Pygame Install
Once you are done with the installation either using APT or PIP, let’s confirm Pygame is successfully installed on our system using the given command or not:
pip show pygame
dpkg -L python3-pygame
5. How to use PyGame
Let’s learn a quick way to import and access Pygame to use it in our Python script or code.
For importing PyGame, first switch to the Python command line by simply typing its name in your command terminal:
Before using any Pygame functions, you need to initialize Pygame by calling the pygame.init() function:
Now, let’s say you want to create a game window or screen where your game will be displayed. Set the dimensions of the window using a pygame.display function and assign it width and height variable:
screen = pygame.display.set_mode((800, 600))
To know more you can check out the official documentation of Pygame.
6. Update Pygame
To upgrade pygame, if some new version is available, we can use the PIP:
pip install pygame --upgrade
To remove the pygame from your Ubuntu 22.04 or 20.04 system, if needed can be done using the given commands:
sudo apt remove python3-pygame