Ubuntu gmsh import

Hi!
I am using Ubuntu 24.04 and encountering issues when importing the Gmsh module in Python. What steps should I follow to resolve this problem? Or would you recommend using Docker to avoid such version-related issues? I would greatly appreciate it if you could help.

This is not a GMSH forum. Please contact the Gmsh developers at:

If the problem is related to dolfinx.io.gmshio, you need to provide more context, see next paragraph.

You have not provided sufficient amount of information about your problem.
For a future post, either for the Gmsh developers or here, please consider adding the following to your post:

  1. how did you install Gmsh
  2. What error message are you getting
  3. What code are you running to get this error message

Hello again, here is more detailed information about the problem I’m experiencing. We are using WSL, so when we download GMSH, it gets installed on Windows, but our Ubuntu environment is in WSL.

I want to create a 2D hexagonal geometry and then mesh it with this code.

import gmsh
from dolfinx.io import gmshio
from mpi4py import MPI
import matplotlib.pyplot as plt
from dolfinx.plot import plot
import pyvista as pv
from dolfinx.mesh import create_mesh

gmsh.initialize()
gmsh.model.add("Hexagon")

points = [
    gmsh.model.occ.addPoint(0, 0, 0),
    gmsh.model.occ.addPoint(1, 0, 0),
    gmsh.model.occ.addPoint(1.5, 1, 0),
    gmsh.model.occ.addPoint(1, 2, 0),
    gmsh.model.occ.addPoint(0, 2, 0),
    gmsh.model.occ.addPoint(-0.5, 1, 0),
]

lines = [
    gmsh.model.occ.addLine(points[i], points[(i + 1) % len(points)])
    for i in range(len(points))
]

loop = gmsh.model.occ.addCurveLoop(lines)
surface = gmsh.model.occ.addPlaneSurface([loop])

gmsh.model.occ.synchronize()
gmsh.model.mesh.generate(2)

mesh, cell_tags, facet_tags = gmshio.model_to_mesh(
    gmsh.model, MPI.COMM_WORLD, 0, gdim=2
)

gmsh.finalize()

plotter = pv.Plotter()
plotter.add_mesh(pv.wrap(mesh), show_edges=True)
plotter.show()  

I tried to download GMSH in this way.

sudo apt install gmsh

But it didn’t work, and it gave a ‘No module named’ error.

You need to use the name of the python package used by ubuntu. The gmsh package only provides gmsh itself (the GUI client, and C libraries), not the python package.

You can search for packages at https://packages.ubuntu.com/
Can you see there which ubuntu package provides the gmsh python module?

Hi thanks, I got how to install GMSH. However, I think I need to use Dolfinx to include the files exported by GMSH into the FEniCS environment. As far as I know, the Dolfinx module is included by default in FEniCSx, but I’m encountering an error when running the code I’m currently using. What solution would you suggest in this case?

import dolfinx
import numpy as np
from mpi4py import MPI
from petsc4py import PETSc

mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)

V = dolfinx.fem.FunctionSpace(mesh, ("CG", 1))

u = dolfinx.fem.Function(V)

u.x.array[:] = 0.0

print("Function values (the function with zero values):")
print(u.x.array)
zeynep@LAPTOP-3KN21JEE:~$  cd /home/zeynep ; /usr/bin/env /bin/python3 /home/zeynep/.vscode-server/extensions/ms-python.debugpy-2024.14.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher 53555 -- /home/zeynep/FEM/fem1.py 
/home/zeynep/FEM/fem1.py:2: SyntaxWarning: invalid escape sequence '\l'
  '''
/home/zeynep/FEM/fem1.py:204: SyntaxWarning: invalid escape sequence '\l'
  '''
/home/zeynep/FEM/fem1.py:380: SyntaxWarning: invalid escape sequence '\l'
  '''
Traceback (most recent call last):
  File "/home/zeynep/FEM/fem1.py", line 575, in <module>
    V = dolfinx.fem.FunctionSpace(mesh, ("CG", 1))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: FunctionSpace.__init__() missing 1 required positional argument: 'cppV'

This is outdated api. The new API is dolfinx.fem.functionspace, see:
https://jsdokken.com/dolfinx-tutorial/Changelog.html
and

1 Like

Report which package you needed to fix the installation, in case anyone else has the same problem and is searching for help in the future.

Yes sure. First, I installed the python3-pygmsh package. You can find the necessary information for this package at this link (Ubuntu – Details of package python3-pygmsh in oracular). To install it, run the following commands in the terminal:

sudo apt update
sudo apt install python3-pygmsh

After that, i download GMSH for Linux from the official GMSH website (https://gmsh.info/).
Once the installation is complete, you can open the GMSH interface page by typing gmsh in the Ubuntu terminal.

hmm, that doesn’t sound right. python3-pygmsh provides the pygmsh module, not gmsh. It’s a different project (different API for handling gmsh files).

python3-gmsh is the one that provides the gmsh python module.

Note you already had the gmsh GUI installed from the gmsh package. No need to install it again from the GMSH website.

After installing this library, my ‘import gmsh’ error was resolved, and I also installed the python3-gmsh package additionally.

python3-pygmsh depends on python3-gmsh, so it installs the gmsh python module when you install it. It doesn’t do any harm to install pygmsh, it’s just not needed if you’re only using gmsh. In some cases the pygmsh API might be easier to use than the gmsh API for generating meshes.

Thank you for your responses, you’ve been really helpful.